From 3390b8b54143bd4bcc4f413ce23b31327a9cfc39 Mon Sep 17 00:00:00 2001 From: Billy Shambrook Date: Mon, 27 May 2024 17:47:19 -0700 Subject: [PATCH] support multiple types for const --- pkg/schema/schema.go | 4 ++-- pkg/schema/schema_test.go | 14 ++++++++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/pkg/schema/schema.go b/pkg/schema/schema.go index 87597ee..4ad0cd2 100644 --- a/pkg/schema/schema.go +++ b/pkg/schema/schema.go @@ -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"` @@ -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") } diff --git a/pkg/schema/schema_test.go b/pkg/schema/schema_test.go index 443677e..b1aad7b 100644 --- a/pkg/schema/schema_test.go +++ b/pkg/schema/schema_test.go @@ -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,