Skip to content

Commit

Permalink
Merge pull request #28 from billyshambrook/fix/any-type-const
Browse files Browse the repository at this point in the history
Support multiple types for const
  • Loading branch information
dadav authored May 29, 2024
2 parents 2cafc75 + 3390b8b commit bdf7656
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
4 changes: 2 additions & 2 deletions pkg/schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ type Schema struct {
Maximum *int `yaml:"maximum,omitempty" json:"maximum,omitempty"`
Else *Schema `yaml:"else,omitempty" json:"else,omitempty"`
Pattern string `yaml:"pattern,omitempty" json:"pattern,omitempty"`
Const string `yaml:"const,omitempty" json:"const,omitempty"`
Const interface{} `yaml:"const,omitempty" json:"const,omitempty"`
Ref string `yaml:"$ref,omitempty" json:"$ref,omitempty"`
Schema string `yaml:"$schema,omitempty" json:"$schema,omitempty"`
Id string `yaml:"$id,omitempty" json:"$id,omitempty"`
Expand Down Expand Up @@ -233,7 +233,7 @@ func (s Schema) Validate() error {
return fmt.Errorf("cant use items if type is %s. Use type=array", s.Type)
}

if s.Const != "" && !s.Type.IsEmpty() {
if s.Const != nil && !s.Type.IsEmpty() {
return errors.New("if your are using const, you can't use type")
}

Expand Down
14 changes: 14 additions & 0 deletions pkg/schema/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,20 @@ func TestValidate(t *testing.T) {
{
comment: `
# @schema
# const: "hello"
# @schema`,
expectedValid: true,
},
{
comment: `
# @schema
# const: true
# @schema`,
expectedValid: true,
},
{
comment: `
# @schema
# format: ipv4
# @schema`,
expectedValid: true,
Expand Down

0 comments on commit bdf7656

Please sign in to comment.