-
-
Notifications
You must be signed in to change notification settings - Fork 592
Return regex pattern error if regex failed to compile #425
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
Return regex pattern error if regex failed to compile #425
Conversation
@Julian The tests are ok after I rebased and merged the last master. |
Hi -- this wouldn't be the right place to do that check, it'd need to happen during schema checking, but that should already be the case, e.g.:
Are you experiencing some specific issue here that's prompting adding this? |
@Julian
If it happens it causes an exception which is unwanted in a typical use case, thus I wrapped up the error:
|
The right place for that to have raised an exception is in the first line
there (the check_schema line). Looks like the reason it didn't is probably
just that we don't currently pass format checkers through when validating
against the meta schema, but that seems like possibly a reasonable change
to make (and even be the default).
…On Mon, Aug 27, 2018 at 10:15 AM, Valery M. ***@***.***> wrote:
@Julian <https://github.com/Julian>
Yes, I meant the case when the regexp is a valid string, but an invalid
python regex.
>>> Draft6Validator.check_schema({"pattern": "\q"})
>>> jsonschema.validate("s", {"pattern": "\q"})
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/valery/Documents/_code/jsonschema/jsonschema/validators.py", line 822, in validate
cls(schema, *args, **kwargs).validate(instance)
File "/Users/valery/Documents/_code/jsonschema/jsonschema/validators.py", line 304, in validate
for error in self.iter_errors(*args, **kwargs):
File "/Users/valery/Documents/_code/jsonschema/jsonschema/validators.py", line 280, in iter_errors
for error in errors:
File "/Users/valery/Documents/_code/jsonschema/jsonschema/_validators.py", line 248, in pattern
not re.search(patrn, instance)
File "/Users/valery/.local/share/virtualenvs/jsonschema-zk5Nmia0/lib/python3.6/re.py", line 182, in search
return _compile(pattern, flags).search(string)
File "/Users/valery/.local/share/virtualenvs/jsonschema-zk5Nmia0/lib/python3.6/re.py", line 301, in _compile
p = sre_compile.compile(pattern, flags)
File "/Users/valery/.local/share/virtualenvs/jsonschema-zk5Nmia0/lib/python3.6/sre_compile.py", line 562, in compile
p = sre_parse.parse(p, flags)
File "/Users/valery/.local/share/virtualenvs/jsonschema-zk5Nmia0/lib/python3.6/sre_parse.py", line 855, in parse
p = _parse_sub(source, pattern, flags & SRE_FLAG_VERBOSE, 0)
File "/Users/valery/.local/share/virtualenvs/jsonschema-zk5Nmia0/lib/python3.6/sre_parse.py", line 416, in _parse_sub
not nested and not items))
File "/Users/valery/.local/share/virtualenvs/jsonschema-zk5Nmia0/lib/python3.6/sre_parse.py", line 502, in _parse
code = _escape(source, this, state)
File "/Users/valery/.local/share/virtualenvs/jsonschema-zk5Nmia0/lib/python3.6/sre_parse.py", line 401, in _escape
raise source.error("bad escape %s" % escape, len(escape))
sre_constants.error: bad escape \q at position 0
If it happens it causes an exception which is unwanted in a typical use
case.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#425 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAUIXk_Lo_QesU5x7AoE-hwpvtm3iAaqks5uU7i_gaJpZM4UtaOe>
.
|
Would you please elaborate a bit? Update: Update:
|
@Julian Would you recommend the best way to handle I thought about calling pattern from within
|
Hey, sorry, things are a bit hectic, have just returned from vacation and started a new job :) To elaborate, it's the Does that help? Happy to answer further questions, and definitely appreciated! |
@Julian Hi, I passed the format_checker but I cannot figure out how to create a default one. As far as I understand, each meta schema should have it's own format checker . Also, it doesn't check patternProperties since the format_checker checks only the value, not the instance name. Last but not least, I added tests for both (patternProperties obviously fails). |
Hey! Thanks for continuing to tackle this! I have to re-review your patch, but on the patternProperties front, I believe that the recent drafts added There's a possibility though that the meta schema isn't actually trying to apply that to But tl;dr -- I think if what you've got isn't catching Will try to leave some additional comments after reviewing, but yeah thanks again, this is definitely appreciated! |
@Julian Hi, I would appreciate if you review this one once you have a free minute. |
Thanks! This is looking close to what I expected. Looks like CI is failing though. Possibly you just need to merge in master. |
I merged master and also updated this one https://github.com/Julian/jsonschema/pull/425/files#diff-523a4ebdb045b5a1613d6711add86c7fL981 and disabled https://github.com/Julian/jsonschema/pull/425/files#diff-523a4ebdb045b5a1613d6711add86c7fR889 |
jsonschema/tests/test_validators.py
Outdated
validator = self.Validator({}) | ||
self.assertIsNone(validator.format_checker) | ||
self.assertIsInstance(validator.format_checker, FormatChecker) |
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.
This test shouldn't pass (i.e. it was correct before), so if it does, something seems wrong -- the goal here wasn't to change the default for all validators, it was just to do so for calling .check_schema
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.
validator.format_checker is not None, I think it's because I passed it here https://github.com/Julian/jsonschema/pull/425/files#diff-ed2f83de8c2ff5b1c1138f9f28ba4ba6R509
I made suggested updates, there are still some failures though:
|
jsonschema/validators.py
Outdated
@@ -220,7 +227,7 @@ def __init__( | |||
schema, | |||
types=(), | |||
resolver=None, | |||
format_checker=None, | |||
format_checker=format_checker, |
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.
It's failing because of this line, where you've changed the default here.
jsonschema/validators.py
Outdated
@@ -257,7 +265,7 @@ def iter_errors(self, instance, _schema=None): | |||
return | |||
elif _schema is False: | |||
yield ValidationError( | |||
"False schema does not allow %r" % (instance,), | |||
"False schema does not allow '{}'".format(instance) |
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.
Can you undo this too?
Think we're close! |
Done. Can it be that that passed format_checker messes with uri format_checker? |
jsonschema/tests/test_validators.py
Outdated
with self.assertRaises(SchemaError): | ||
self.Validator.check_schema({"pattern": "\q"}) | ||
|
||
@unittest.skip("This test fails but it shouldn't, see" |
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 so here, this test doesn't fail on draft 7, which properly says that the keys need to be regexes for patternProperties
. It does that though using a new validator (propertyNames
) that didn't exist before draft 6.
Draft 6 though seems to have a bug in the metaschema (by not specifying propertyNames for patternProperties even though that existed for draft 6), which sounds like something that should be filed upstream.
And drafts 3 and 4 don't have that, so that's why this fails -- so it seems like the right thing to do isn't to skip this here, it should pass on draft 7, and draft 6 when/if the metaschema is fixed.
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.
Ah, right, I think the reason it's not there in draft6 is because draft6 (and 4) accidentally removed the regex
format.
So, this seems to be a thing that really only should pass on draft 7, and "correctly" (FSVO correctly) blows up on earlier drafts.
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.
Understood, how can I skip the test on particular drafts? I don't understand the skip lambda https://github.com/Julian/jsonschema/blob/master/jsonschema/tests/test_jsonschema_test_suite.py#L81
I don't understand skip lambda, how can I skip tests with it? |
@manycoding hey! So I wouldn't bother with that stuff here, what we need here is just a test case we only run for draft 6 and 7 -- the most straightforward way to do that is just a |
I moved pattern tests to their own draft class, but it seems that my code affects ref in Draft3 and 4 and it's not comprehensible to me in what ways. |
@Julian I really would like to finish this one in this year :) |
Hey @manycoding so would I! I've unfortunately got extremely limited time, which currently even for Definitely appreciate the PR. |
@Julian I have a hunch that the failing tests could be connected to the fact that draft 3 and 4 don't have $ref defined in meta schema properties. |
47d2064
to
2c15ae3
Compare
Codecov Report
@@ Coverage Diff @@
## master #425 +/- ##
==========================================
- Coverage 95.13% 95.11% -0.02%
==========================================
Files 18 18
Lines 2425 2437 +12
Branches 309 306 -3
==========================================
+ Hits 2307 2318 +11
- Misses 97 101 +4
+ Partials 21 18 -3 |
Because FormatChecker is bad and I should feel bad :/ This is currently being overriden by the older Draft3 time format. Probably time to deprecate cls_checks, and pass format checkers in on validator creation. The changes in #425 are likely also relevant.
@manycoding FWIW I am going to try to finally find some time to give you another breadcrumb (or ideally figure out what's up here) some time next week :/ Fingers crossed! |
…at_checker to None
2c15ae3
to
6731bcc
Compare
* manycoding/regex_value_validation: Set format_checker to None for draft3 and 4 Move test_invalid_pattern under Draft6 and 7 Enable invalid regex tests Move regex patternProperty to InvalidRegexMixin Add propertyNames to Draft6 patternProperties, do not skip patternProperties test Pass draft7_format_checker to meta schema, set default Validator format_checker to None Revert style changes and test_it_does_not_validate_formats_by_default Fix test_it_validates_formats_by_default Add quotes back to False schema does not allow Fix test_False_schema Update test_it_validates_formats_by_default Ignore test_invalid_patternProperty Add invalid_patternProperty test Add invalid pattern tests Pass format_checker to validator in check_schema Pass format_checkers to meta schemas Fix regex output msg Return regex pattern error if regex failed to compile
6615403
to
4853f05
Compare
4853f05
to
6615403
Compare
If an invalid regex pattern were provided, validation fails.
So I added validate_regex to check it and also a yield "Pattern error in" .