Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add pattern property to schema #304

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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