From ffa74e8c827e8a8e88595ee6dcb5bd6189dfa13b Mon Sep 17 00:00:00 2001 From: Yury Rudichev Date: Sun, 13 Oct 2024 07:09:25 +0300 Subject: [PATCH] add pattern property to schema Signed-off-by: Yury Rudichev --- bundle/definition/schema.go | 23 ++++++++++++----------- bundle/definition/validation_test.go | 15 +++++++++++++++ 2 files changed, 27 insertions(+), 11 deletions(-) diff --git a/bundle/definition/schema.go b/bundle/definition/schema.go index 2ff21e89..1b1b3458 100644 --- a/bundle/definition/schema.go +++ b/bundle/definition/schema.go @@ -2,6 +2,7 @@ package definition import ( "encoding/json" + "regexp" "strconv" "strings" @@ -35,17 +36,17 @@ type Schema struct { Format string `json:"format,omitempty" yaml:"format,omitempty"` If *Schema `json:"if,omitempty" yaml:"if,omitempty"` //Items can be a Schema or an Array of Schema :( - Items interface{} `json:"items,omitempty" yaml:"items,omitempty"` - Maximum *float64 `json:"maximum,omitempty" yaml:"maximum,omitempty"` - MaxLength *int `json:"maxLength,omitempty" yaml:"maxLength,omitempty"` - MinItems *int `json:"minItems,omitempty" yaml:"minItems,omitempty"` - MinLength *int `json:"minLength,omitempty" yaml:"minLength,omitempty"` - MinProperties *int `json:"minProperties,omitempty" yaml:"minProperties,omitempty"` - Minimum *float64 `json:"minimum,omitempty" yaml:"minimum,omitempty"` - MultipleOf *float64 `json:"multipleOf,omitempty" yaml:"multipleOf,omitempty"` - Not *Schema `json:"not,omitempty" yaml:"not,omitempty"` - OneOf *Schema `json:"oneOf,omitempty" yaml:"oneOf,omitempty"` - + Items interface{} `json:"items,omitempty" yaml:"items,omitempty"` + Maximum *float64 `json:"maximum,omitempty" yaml:"maximum,omitempty"` + MaxLength *int `json:"maxLength,omitempty" yaml:"maxLength,omitempty"` + MinItems *int `json:"minItems,omitempty" yaml:"minItems,omitempty"` + MinLength *int `json:"minLength,omitempty" yaml:"minLength,omitempty"` + MinProperties *int `json:"minProperties,omitempty" yaml:"minProperties,omitempty"` + Minimum *float64 `json:"minimum,omitempty" yaml:"minimum,omitempty"` + MultipleOf *float64 `json:"multipleOf,omitempty" yaml:"multipleOf,omitempty"` + Not *Schema `json:"not,omitempty" yaml:"not,omitempty"` + OneOf *Schema `json:"oneOf,omitempty" yaml:"oneOf,omitempty"` + Pattern *regexp.Regexp `json:"pattern,omitempty" yaml:"pattern,omitempty"` PatternProperties map[string]*Schema `json:"patternProperties,omitempty" yaml:"patternProperties,omitempty"` Properties map[string]*Schema `json:"properties,omitempty" yaml:"properties,omitempty"` diff --git a/bundle/definition/validation_test.go b/bundle/definition/validation_test.go index 31192c51..75584da3 100644 --- a/bundle/definition/validation_test.go +++ b/bundle/definition/validation_test.go @@ -41,6 +41,21 @@ func TestObjectValidationValid(t *testing.T) { assert.Len(t, valErrors, 0, "expected no validation errors") assert.NoError(t, err) } +func TestPatternValidationValid(t *testing.T) { + s := `{ + "type": "string", + "pattern" : "^[0-9]{3}-[0-9]{4}$" + }` + definition := new(Schema) + err := json.Unmarshal([]byte(s), definition) + require.NoError(t, err, "should have been able to marshall definition") + assert.Equal(t, "string", definition.Type, "type should have been an object") + + val := "124-1234" + valErrors, err := definition.Validate(val) + assert.Len(t, valErrors, 0, "expected no validation errors") + assert.NoError(t, err) +} func TestObjectValidationValid_CustomValidator_ContentEncoding_base64(t *testing.T) { s := `{