-
Notifications
You must be signed in to change notification settings - Fork 180
Add schema and config validation to jsonschema package #740
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
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
7f05e5c
Add validation for schema default value during Load. Also cast defaul…
shreyas-goenka ff04596
removing todos
shreyas-goenka b865190
This commit introduces the LoadInstance and ValidateInstance methods.…
shreyas-goenka 8bad4e9
Remove integer conversion logic during config default assignment
shreyas-goenka 634a260
Add validation for required properties
shreyas-goenka 189e7ad
Replace template config validation with validation defined in the jso…
shreyas-goenka 1a702a3
Merge remote-tracking branch 'origin' into schema-config-validators
shreyas-goenka 7413bed
Remove validation type function from template library
shreyas-goenka d8ef933
Move to/from string methods to the json schema package. Delete utils.…
shreyas-goenka a741d2b
-
shreyas-goenka a617180
Merge remote-tracking branch 'origin' into schema-config-validators
shreyas-goenka f2acc09
Add unit test for LoadInstance
shreyas-goenka 369e8b0
address commments
shreyas-goenka e15a81e
Merge remote-tracking branch 'origin' into schema-config-validators
shreyas-goenka ec0da3d
-
shreyas-goenka File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| package jsonschema | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "fmt" | ||
| "os" | ||
| ) | ||
|
|
||
| // Load a JSON document and validate it against the JSON schema. Instance here | ||
| // refers to a JSON document. see: https://json-schema.org/draft/2020-12/json-schema-core.html#name-instance | ||
| func (s *Schema) LoadInstance(path string) (map[string]any, error) { | ||
| instance := make(map[string]any) | ||
| b, err := os.ReadFile(path) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| err = json.Unmarshal(b, &instance) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| // The default JSON unmarshaler parses untyped number values as float64. | ||
| // We convert integer properties from float64 to int64 here. | ||
| for name, v := range instance { | ||
| propertySchema, ok := s.Properties[name] | ||
| if !ok { | ||
| continue | ||
| } | ||
| if propertySchema.Type != IntegerType { | ||
| continue | ||
| } | ||
| integerValue, err := toInteger(v) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to parse property %s: %w", name, err) | ||
| } | ||
| instance[name] = integerValue | ||
| } | ||
| return instance, s.ValidateInstance(instance) | ||
| } | ||
|
|
||
| func (s *Schema) ValidateInstance(instance map[string]any) error { | ||
| if err := s.validateAdditionalProperties(instance); err != nil { | ||
| return err | ||
| } | ||
| if err := s.validateRequired(instance); err != nil { | ||
| return err | ||
| } | ||
| return s.validateTypes(instance) | ||
| } | ||
|
|
||
| // If additional properties is set to false, this function validates instance only | ||
| // contains properties defined in the schema. | ||
| func (s *Schema) validateAdditionalProperties(instance map[string]any) error { | ||
| // Note: AdditionalProperties has the type any. | ||
| if s.AdditionalProperties != false { | ||
|
shreyas-goenka marked this conversation as resolved.
|
||
| return nil | ||
| } | ||
| for k := range instance { | ||
| _, ok := s.Properties[k] | ||
| if !ok { | ||
| return fmt.Errorf("property %s is not defined in the schema", k) | ||
| } | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| // This function validates that all require properties in the schema have values | ||
| // in the instance. | ||
| func (s *Schema) validateRequired(instance map[string]any) error { | ||
| for _, name := range s.Required { | ||
| if _, ok := instance[name]; !ok { | ||
| return fmt.Errorf("no value provided for required property %s", name) | ||
| } | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| // Validates the types of all input properties values match their types defined in the schema | ||
| func (s *Schema) validateTypes(instance map[string]any) error { | ||
| for k, v := range instance { | ||
| fieldInfo, ok := s.Properties[k] | ||
| if !ok { | ||
| continue | ||
| } | ||
| err := validateType(v, fieldInfo.Type) | ||
| if err != nil { | ||
| return fmt.Errorf("incorrect type for property %s: %w", k, err) | ||
| } | ||
| } | ||
| return nil | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,129 @@ | ||
| package jsonschema | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestValidateInstanceAdditionalPropertiesPermitted(t *testing.T) { | ||
| instance := map[string]any{ | ||
| "int_val": 1, | ||
| "float_val": 1.0, | ||
| "bool_val": false, | ||
| "an_additional_property": "abc", | ||
| } | ||
|
|
||
| schema, err := Load("./testdata/instance-validate/test-schema.json") | ||
| require.NoError(t, err) | ||
|
|
||
| err = schema.validateAdditionalProperties(instance) | ||
| assert.NoError(t, err) | ||
|
|
||
| err = schema.ValidateInstance(instance) | ||
| assert.NoError(t, err) | ||
| } | ||
|
|
||
| func TestValidateInstanceAdditionalPropertiesForbidden(t *testing.T) { | ||
| instance := map[string]any{ | ||
| "int_val": 1, | ||
| "float_val": 1.0, | ||
| "bool_val": false, | ||
| "an_additional_property": "abc", | ||
| } | ||
|
|
||
| schema, err := Load("./testdata/instance-validate/test-schema-no-additional-properties.json") | ||
| require.NoError(t, err) | ||
|
|
||
| err = schema.validateAdditionalProperties(instance) | ||
| assert.EqualError(t, err, "property an_additional_property is not defined in the schema") | ||
|
|
||
| err = schema.ValidateInstance(instance) | ||
| assert.EqualError(t, err, "property an_additional_property is not defined in the schema") | ||
|
|
||
| instanceWOAdditionalProperties := map[string]any{ | ||
| "int_val": 1, | ||
| "float_val": 1.0, | ||
| "bool_val": false, | ||
| } | ||
|
|
||
| err = schema.validateAdditionalProperties(instanceWOAdditionalProperties) | ||
| assert.NoError(t, err) | ||
|
|
||
| err = schema.ValidateInstance(instanceWOAdditionalProperties) | ||
| assert.NoError(t, err) | ||
| } | ||
|
|
||
| func TestValidateInstanceTypes(t *testing.T) { | ||
| schema, err := Load("./testdata/instance-validate/test-schema.json") | ||
| require.NoError(t, err) | ||
|
|
||
| validInstance := map[string]any{ | ||
| "int_val": 1, | ||
| "float_val": 1.0, | ||
| "bool_val": false, | ||
| } | ||
|
|
||
| err = schema.validateTypes(validInstance) | ||
| assert.NoError(t, err) | ||
|
|
||
| err = schema.ValidateInstance(validInstance) | ||
| assert.NoError(t, err) | ||
|
|
||
| invalidInstance := map[string]any{ | ||
| "int_val": "abc", | ||
| "float_val": 1.0, | ||
| "bool_val": false, | ||
| } | ||
|
|
||
| err = schema.validateTypes(invalidInstance) | ||
| assert.EqualError(t, err, "incorrect type for property int_val: expected type integer, but value is \"abc\"") | ||
|
|
||
| err = schema.ValidateInstance(invalidInstance) | ||
| assert.EqualError(t, err, "incorrect type for property int_val: expected type integer, but value is \"abc\"") | ||
| } | ||
|
|
||
| func TestValidateInstanceRequired(t *testing.T) { | ||
| schema, err := Load("./testdata/instance-validate/test-schema-some-fields-required.json") | ||
| require.NoError(t, err) | ||
|
|
||
| validInstance := map[string]any{ | ||
| "int_val": 1, | ||
| "float_val": 1.0, | ||
| "bool_val": false, | ||
| } | ||
| err = schema.validateRequired(validInstance) | ||
| assert.NoError(t, err) | ||
| err = schema.ValidateInstance(validInstance) | ||
| assert.NoError(t, err) | ||
|
|
||
| invalidInstance := map[string]any{ | ||
| "string_val": "abc", | ||
| "float_val": 1.0, | ||
| "bool_val": false, | ||
| } | ||
| err = schema.validateRequired(invalidInstance) | ||
| assert.EqualError(t, err, "no value provided for required property int_val") | ||
| err = schema.ValidateInstance(invalidInstance) | ||
| assert.EqualError(t, err, "no value provided for required property int_val") | ||
| } | ||
|
|
||
| func TestLoadInstance(t *testing.T) { | ||
| schema, err := Load("./testdata/instance-validate/test-schema.json") | ||
| require.NoError(t, err) | ||
|
|
||
| // Expect the instance to be loaded successfully. | ||
| instance, err := schema.LoadInstance("./testdata/instance-load/valid-instance.json") | ||
| assert.NoError(t, err) | ||
| assert.Equal(t, map[string]any{ | ||
| "bool_val": false, | ||
| "int_val": int64(1), | ||
| "string_val": "abc", | ||
| "float_val": 2.0, | ||
| }, instance) | ||
|
|
||
| // Expect instance validation against the schema to fail. | ||
| _, err = schema.LoadInstance("./testdata/instance-load/invalid-type-instance.json") | ||
| assert.EqualError(t, err, "incorrect type for property string_val: expected type string, but value is 123") | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
libs/jsonschema/testdata/instance-load/invalid-type-instance.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| { | ||
| "int_val": 1, | ||
| "bool_val": false, | ||
| "string_val": 123, | ||
| "float_val": 3.0 | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| { | ||
| "int_val": 1, | ||
| "bool_val": false, | ||
| "string_val": "abc", | ||
| "float_val": 2.0 | ||
| } |
19 changes: 19 additions & 0 deletions
19
libs/jsonschema/testdata/instance-validate/test-schema-no-additional-properties.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| { | ||
| "properties": { | ||
| "int_val": { | ||
| "type": "integer", | ||
| "default": 123 | ||
| }, | ||
| "float_val": { | ||
| "type": "number" | ||
| }, | ||
| "bool_val": { | ||
| "type": "boolean" | ||
| }, | ||
| "string_val": { | ||
| "type": "string", | ||
| "default": "abc" | ||
| } | ||
| }, | ||
| "additionalProperties": false | ||
| } |
19 changes: 19 additions & 0 deletions
19
libs/jsonschema/testdata/instance-validate/test-schema-some-fields-required.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| { | ||
| "properties": { | ||
| "int_val": { | ||
| "type": "integer", | ||
| "default": 123 | ||
| }, | ||
| "float_val": { | ||
| "type": "number" | ||
| }, | ||
| "bool_val": { | ||
| "type": "boolean" | ||
| }, | ||
| "string_val": { | ||
| "type": "string", | ||
| "default": "abc" | ||
| } | ||
| }, | ||
| "required": ["int_val", "float_val", "bool_val"] | ||
| } |
18 changes: 18 additions & 0 deletions
18
libs/jsonschema/testdata/instance-validate/test-schema.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| { | ||
| "properties": { | ||
| "int_val": { | ||
| "type": "integer", | ||
| "default": 123 | ||
| }, | ||
| "float_val": { | ||
| "type": "number" | ||
| }, | ||
| "bool_val": { | ||
| "type": "boolean" | ||
| }, | ||
| "string_val": { | ||
| "type": "string", | ||
| "default": "abc" | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.