feat: Add index number to validation error messages in custom profile attributes

This commit is contained in:
Julien Tant (aider)
2025-02-12 13:18:51 -07:00
parent 35e29a0dfd
commit db1839a6e9
2 changed files with 6 additions and 5 deletions

View File

@@ -5,6 +5,7 @@ package model
import (
"errors"
"fmt"
"strings"
)
@@ -111,13 +112,13 @@ func (c CustomProfileAttributesSelectOptions) IsValid() error {
}
seenNames := make(map[string]struct{})
for _, option := range c {
for i, option := range c {
if err := option.IsValid(); err != nil {
return err
return fmt.Errorf("invalid option at index %d: %w", i, err)
}
if _, exists := seenNames[option.Name]; exists {
return errors.New("duplicate option name found")
return fmt.Errorf("duplicate option name found at index %d: %s", i, option.Name)
}
seenNames[option.Name] = struct{}{}
}

View File

@@ -49,7 +49,7 @@ func TestCustomProfileAttributeSelectOptionIsValid(t *testing.T) {
Name: "Test Option",
Color: "#FF0000",
},
wantErr: "id cannot be empty",
wantErr: "invalid option at index 1: id cannot be empty",
},
{
name: "invalid ID",
@@ -149,7 +149,7 @@ func TestCustomProfileAttributesSelectOptionsIsValid(t *testing.T) {
Color: "#00FF00",
},
},
wantErr: "duplicate option name found",
wantErr: "duplicate option name found at index 1: Option 1",
},
}