-
Notifications
You must be signed in to change notification settings - Fork 9
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
Issue 147 #148
Draft
anakhalil
wants to merge
6
commits into
networktocode:develop
Choose a base branch
from
anakhalil:issue-147
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Issue 147 #148
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f092290
add custom error to schema validation
anakhalil 04dacaa
make message generic
anakhalil 886971d
replace placeholder with instance data
anakhalil 0f55cfd
Simplify message tag
anakhalil 42e2d6e
Adding tests for custom error handling
aggle-baggle b113e89
Update README.md
aggle-baggle 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 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
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.
Nice work! You could even make the
iData
variable dynamic and couple it to the user defined data structure so the user can then use the property key prepended with$
:Then then schema would look like:
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.
I think you could even do it on one line:
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.
Thanks for the suggestion!
I am only worried about how you index the path. Since:
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.
Oh good catch, I guess the potential
IndexError
could be caught with atry/except
orif len(err.absolute_path) > 0
or similar...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.
Been doing a bit of testing with the following test schema:
When validating the following data the
IndexError: deque index out of range
error is observed:The error observed:
One solution that seems to work is to ignore the
errMessage
key at the root level i.e. iferr.absolute_path
evaluates to an empty deque:Then you get the built-in error messages back from JSON Schema at the root level, and custom error message processing for anything nested:
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.
Hey Phill, no problem!
So I've tried quite a few schemas in my local testing and using
$instance
does indeed seem to work fine in every scenario I've looked at. One thing to bear in mind is that it will only return the parent error (this is the default behaviour of Schema Enforcer anyway). When the parent error is the result of sub-errors when using schema composition keywords likeanyOf
, theValidationError.context
attribute can be used to return the sub-errors. But returning these sub-errors in SE is crossing over into a whole different feature request.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.
Acknowledged, thanks for all the testing.
It would be awesome to get those tests into unit tests to validate future changes don't break things, and that different custom message cases work as we expect them to.
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.
Hi Phill,
I would like to open a new PR for this feature as @anakhalil has moved on to a new adventure and likely won't have the time to flesh out the unit/integration tests required.
Hopefully this will be fairly soon!
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.
Sounds good, thank you! :)
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.
I've pushed some commits to this PR that introduce some unit tests for custom error message handling and also a README snippet. There were a few hurdles with the unit tests, for some reason I could not get it to work with the schema broken up into separate files, so had to adjust it so it was all in one file. Even then it would not allow me to use references to definitions from within the same file.
However the tests do pass. I tried to make the tests in line with the existing ones and not interfere with them so there is some repeated code. Hopefully it's a good starting point anyway!