mirror of
https://github.com/mattermost/mattermost.git
synced 2026-09-24 16:05:00 +08:00
feat: Add IsValid method to validate CustomProfileAttributesSelectOptions
This commit is contained in:
@@ -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)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user