Skip to content

Commit

Permalink
add pattern property to schema
Browse files Browse the repository at this point in the history
Signed-off-by: Yury Rudichev <[email protected]>
  • Loading branch information
Yury Rudichev committed Oct 13, 2024
1 parent b10e925 commit ffa74e8
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 11 deletions.
23 changes: 12 additions & 11 deletions bundle/definition/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package definition

import (
"encoding/json"
"regexp"
"strconv"
"strings"

Expand Down Expand Up @@ -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"`
Expand Down
15 changes: 15 additions & 0 deletions bundle/definition/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 := `{
Expand Down

0 comments on commit ffa74e8

Please sign in to comment.