Skip to content

Commit

Permalink
Extending customizable schema with AtLeastOneOf, ExactlyOneOf, `R…
Browse files Browse the repository at this point in the history
…equiredWith` (#3182)

* adding more customizable attributes

* adding unit tests
  • Loading branch information
karolusz authored Feb 1, 2024
1 parent d3acc7b commit 1940e1a
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
26 changes: 25 additions & 1 deletion common/customizable_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,36 @@ func (s *CustomizableSchema) SetMinItems(value int) *CustomizableSchema {

func (s *CustomizableSchema) SetConflictsWith(value []string) *CustomizableSchema {
if len(value) == 0 {
panic("SetConflictsWith cannot take in empty list")
panic("SetConflictsWith cannot take in an empty list")
}
s.Schema.ConflictsWith = value
return s
}

func (s *CustomizableSchema) SetExactlyOneOf(value []string) *CustomizableSchema {
if len(value) == 0 {
panic("SetExactlyOneOf cannot take in an empty list")
}
s.Schema.ExactlyOneOf = value
return s
}

func (s *CustomizableSchema) SetAtLeastOneOf(value []string) *CustomizableSchema {
if len(value) == 0 {
panic("SetAtLeastOneOf cannot take in an empty list")
}
s.Schema.AtLeastOneOf = value
return s
}

func (s *CustomizableSchema) SetRequiredWith(value []string) *CustomizableSchema {
if len(value) == 0 {
panic("SetRequiredWith cannot take in an empty list")
}
s.Schema.RequiredWith = value
return s
}

func (s *CustomizableSchema) SetDeprecated(reason string) *CustomizableSchema {
s.Schema.Deprecated = reason
return s
Expand Down
14 changes: 14 additions & 0 deletions common/customizable_schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,20 @@ func TestCustomizableSchemaSetConflictsWith(t *testing.T) {
assert.Truef(t, len(testCustomizableSchemaScm["non_optional"].ConflictsWith) == 1, "conflictsWith should be set in field: non_optional")
}

func TestCustomizableSchemaSetExactlyOneOf(t *testing.T) {
CustomizeSchemaPath(testCustomizableSchemaScm, "non_optional").SetExactlyOneOf([]string{"abc"})
assert.Truef(t, len(testCustomizableSchemaScm["non_optional"].ExactlyOneOf) == 1, "ExactlyOneOf should be set in field: non_optional")
}

func TestCustomizableSchemaAtLeastOneOf(t *testing.T) {
CustomizeSchemaPath(testCustomizableSchemaScm, "non_optional").SetAtLeastOneOf([]string{"abc"})
assert.Truef(t, len(testCustomizableSchemaScm["non_optional"].AtLeastOneOf) == 1, "AtLeastOneOf should be set in field: non_optional")
}

func TestCustomizableSchemaSetRequiredWith(t *testing.T) {
CustomizeSchemaPath(testCustomizableSchemaScm, "non_optional").SetRequiredWith([]string{"abc"})
assert.Truef(t, len(testCustomizableSchemaScm["non_optional"].RequiredWith) == 1, "RequiredWith should be set in field: non_optional")
}
func TestCustomizableSchemaSetDeprecated(t *testing.T) {
CustomizeSchemaPath(testCustomizableSchemaScm, "non_optional").SetDeprecated("test reason")
assert.Truef(t, testCustomizableSchemaScm["non_optional"].Deprecated == "test reason", "deprecated should be overriden in field: non_optional")
Expand Down

0 comments on commit 1940e1a

Please sign in to comment.