forked from santhosh-tekuri/jsonschema
-
Notifications
You must be signed in to change notification settings - Fork 6
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 ValidationRequiredError error context #1
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains 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 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 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,54 @@ | ||
[ | ||
{ | ||
"$schema": "http://json-schema.org/draft-07/schema#", | ||
"type": "object", | ||
"properties": { | ||
"bar": { | ||
"type": "object", | ||
"properties": { | ||
"foo": { | ||
"type": "string" | ||
} | ||
}, | ||
"required": [ | ||
"foo" | ||
] | ||
} | ||
}, | ||
"required": [ | ||
"bar" | ||
] | ||
}, | ||
{ | ||
"$schema": "http://json-schema.org/draft-07/schema#", | ||
"type": "object", | ||
"properties": { | ||
"object": { | ||
"type": "object", | ||
"properties": { | ||
"object": { | ||
"type": "object", | ||
"properties": { | ||
"foo": { | ||
"type": "string" | ||
}, | ||
"bar": { | ||
"type": "string" | ||
} | ||
}, | ||
"required": [ | ||
"foo", | ||
"bar" | ||
] | ||
} | ||
}, | ||
"required": [ | ||
"object" | ||
] | ||
} | ||
}, | ||
"required": [ | ||
"object" | ||
] | ||
} | ||
] |
This file contains 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,50 @@ | ||
package jsonschema | ||
|
||
import ( | ||
"fmt" | ||
"strconv" | ||
"strings" | ||
) | ||
|
||
// ValidationContext | ||
type ValidationContext interface { | ||
AddContext(instancePtr, schemaPtr string) | ||
FinishInstanceContext() | ||
} | ||
|
||
// ValidationContextRequired is used as error context when one or more required properties are missing. | ||
type ValidationContextRequired struct { | ||
// Missing contains JSON Pointers to all missing properties. | ||
Missing []string | ||
} | ||
|
||
func (r *ValidationContextRequired) AddContext(instancePtr, _ string) { | ||
for k, p := range r.Missing { | ||
r.Missing[k] = joinPtr(instancePtr, p) | ||
} | ||
} | ||
|
||
func (r *ValidationContextRequired) FinishInstanceContext() { | ||
for k, p := range r.Missing { | ||
if len(p) == 0 { | ||
r.Missing[k] = "#" | ||
} else { | ||
r.Missing[k] = "#/" + p | ||
} | ||
} | ||
} | ||
|
||
func validationRequiredError(properties []string) *ValidationError { | ||
missing := make([]string, len(properties)) | ||
|
||
for k := range missing { | ||
missing[k] = strconv.Quote(properties[k]) | ||
properties[k] = escape(properties[k]) | ||
} | ||
|
||
return &ValidationError{ | ||
SchemaPtr: "required", | ||
Message: fmt.Sprintf("missing properties: %s", strings.Join(missing, ", ")), | ||
Context: &ValidationContextRequired{Missing: properties}, | ||
} | ||
} |
This file contains 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,86 @@ | ||
package jsonschema_test | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/santhosh-tekuri/jsonschema/v2" | ||
) | ||
|
||
func TestErrorsContext(t *testing.T) { | ||
for k, tc := range []struct { | ||
path string | ||
doc string | ||
expected interface{} | ||
}{ | ||
{ | ||
path: "testdata/errors/required.json#/0", | ||
doc: `{}`, | ||
expected: &jsonschema.ValidationContextRequired{Missing: []string{"#/bar"}}, | ||
}, | ||
{ | ||
path: "testdata/errors/required.json#/0", | ||
doc: `{"bar":{}}`, | ||
expected: &jsonschema.ValidationContextRequired{ | ||
Missing: []string{"#/bar/foo"}, | ||
}, | ||
}, | ||
{ | ||
path: "testdata/errors/required.json#/1", | ||
doc: `{"object":{"object":{"foo":"foo"}}}`, | ||
expected: &jsonschema.ValidationContextRequired{ | ||
Missing: []string{"#/object/object/bar"}, | ||
}, | ||
}, | ||
{ | ||
path: "testdata/errors/required.json#/1", | ||
doc: `{"object":{"object":{"bar":"bar"}}}`, | ||
expected: &jsonschema.ValidationContextRequired{ | ||
Missing: []string{"#/object/object/foo"}, | ||
}, | ||
}, | ||
{ | ||
path: "testdata/errors/required.json#/1", | ||
doc: `{"object":{"object":{}}}`, | ||
expected: &jsonschema.ValidationContextRequired{ | ||
Missing: []string{"#/object/object/foo", "#/object/object/bar"}, | ||
}, | ||
}, | ||
{ | ||
path: "testdata/errors/required.json#/1", | ||
doc: `{"object":{}}`, | ||
expected: &jsonschema.ValidationContextRequired{ | ||
Missing: []string{"#/object/object"}, | ||
}, | ||
}, | ||
{ | ||
path: "testdata/errors/required.json#/1", | ||
doc: `{}`, | ||
expected: &jsonschema.ValidationContextRequired{ | ||
Missing: []string{"#/object"}, | ||
}, | ||
}, | ||
} { | ||
t.Run(fmt.Sprintf("case=%d", k), func(t *testing.T) { | ||
var ( | ||
schema = jsonschema.MustCompile(tc.path) | ||
err = schema.Validate(bytes.NewBufferString(tc.doc)) | ||
) | ||
|
||
if err == nil { | ||
t.Errorf("Expected error but got nil") | ||
return | ||
} | ||
|
||
var ( | ||
actual = err.(*jsonschema.ValidationError).Context | ||
) | ||
|
||
if !reflect.DeepEqual(tc.expected, actual) { | ||
t.Errorf("expected:\t%#v\n\tactual:\t%#v", tc.expected, actual) | ||
} | ||
}) | ||
} | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
pname may contain comma or space. because of this it was quoted in message
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We want the raw JSON pointers here (e.g.
#/foo/bar
) because we want to pass that to the error details. The error message itself is still being quoted: https://github.com/ory/jsonschema/pull/1/files#diff-02934e0fdc85f35aa62e19cf7c6f6bcaR39There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
got it. but before including pname in json pointer, some special characters needs to be escaped.
check https://github.com/santhosh-tekuri/jsonschema/blob/master/schema.go#L557
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right! I though
joinPtr
would do that but I assumed that incorrectly.