feat: Add IsValid method to validate CustomProfileAttributesSelectOptions

This commit is contained in:
Julien Tant (aider)
2025-02-12 12:46:06 -07:00
parent 4531710f56
commit 3263b04478
2 changed files with 90 additions and 0 deletions
@@ -64,3 +64,20 @@ func (c CustomProfileAttributesSelectOption) IsValid() error {
}
type CustomProfileAttributesSelectOptions []CustomProfileAttributesSelectOption
func (c CustomProfileAttributesSelectOptions) IsValid() error {
seenNames := make(map[string]bool)
for _, option := range c {
if err := option.IsValid(); err != nil {
return err
}
if seenNames[option.Name] {
return errors.New("duplicate option name found")
}
seenNames[option.Name] = true
}
return nil
}
@@ -91,3 +91,76 @@ func TestCustomProfileAttributeSelectOptionIsValid(t *testing.T) {
})
}
}
func TestCustomProfileAttributesSelectOptionsIsValid(t *testing.T) {
tests := []struct {
name string
options CustomProfileAttributesSelectOptions
wantErr string
}{
{
name: "empty options",
options: CustomProfileAttributesSelectOptions{},
wantErr: "",
},
{
name: "valid options",
options: CustomProfileAttributesSelectOptions{
{
ID: NewId(),
Name: "Option 1",
Color: "#FF0000",
},
{
ID: NewId(),
Name: "Option 2",
Color: "#00FF00",
},
},
wantErr: "",
},
{
name: "invalid option",
options: CustomProfileAttributesSelectOptions{
{
ID: NewId(),
Name: "Option 1",
Color: "#FF0000",
},
{
ID: "",
Name: "Option 2",
Color: "#00FF00",
},
},
wantErr: "id cannot be empty",
},
{
name: "duplicate names",
options: CustomProfileAttributesSelectOptions{
{
ID: NewId(),
Name: "Option 1",
Color: "#FF0000",
},
{
ID: NewId(),
Name: "Option 1",
Color: "#00FF00",
},
},
wantErr: "duplicate option name found",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := tt.options.IsValid()
if tt.wantErr != "" {
assert.EqualError(t, err, tt.wantErr)
} else {
assert.NoError(t, err)
}
})
}
}