Skip to content

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

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
fcd23f5
Return regex pattern error if regex failed to compile
manycoding Jun 19, 2018
c4b4b5e
Fix regex output msg
manycoding Jun 22, 2018
2fda5d1
Pass format_checkers to meta schemas
manycoding Sep 4, 2018
a37c94b
Pass format_checker to validator in check_schema
manycoding Sep 21, 2018
90a9ec1
Add invalid pattern tests
manycoding Sep 21, 2018
c4f19cb
Add invalid_patternProperty test
manycoding Sep 21, 2018
b6a2740
Ignore test_invalid_patternProperty
manycoding Oct 16, 2018
bedace8
Update test_it_validates_formats_by_default
manycoding Oct 16, 2018
ff55efa
Fix test_False_schema
manycoding Oct 16, 2018
ab75e73
Add quotes back to False schema does not allow
manycoding Oct 16, 2018
316bd62
Fix test_it_validates_formats_by_default
manycoding Oct 16, 2018
7a047d8
Revert style changes and test_it_does_not_validate_formats_by_default
manycoding Oct 16, 2018
beb17cf
Pass draft7_format_checker to meta schema, set default Validator form…
manycoding Oct 17, 2018
7212568
Add propertyNames to Draft6 patternProperties, do not skip patternPro…
manycoding Oct 22, 2018
d1c25d4
Move regex patternProperty to InvalidRegexMixin
manycoding Nov 5, 2018
43db116
Enable invalid regex tests
manycoding Nov 5, 2018
564d853
Move test_invalid_pattern under Draft6 and 7
manycoding Nov 5, 2018
6731bcc
Set format_checker to None for draft3 and 4
manycoding Nov 25, 2018
75b2048
Merge remote-tracking branch 'manycoding/regex_value_validation'
Julian Jun 17, 2019
46941e3
Docstring and minor style.
Julian Jun 17, 2019
6615403
Add the checkers for 3 and 4 too.
Julian Jun 17, 2019
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
20 changes: 20 additions & 0 deletions jsonschema/tests/test_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -1124,6 +1124,26 @@ def test_enum_allows_non_unique_items(self):
self.Validator.check_schema({"enum": [12, 12]})


class InvalidRegexMixin(object):
def test_invalid_patternProperty(self):
with self.assertRaises(exceptions.SchemaError):
self.Validator.check_schema(
{"patternProperties":{"\q": {"type": "number"}}},
)

def test_invalid_pattern(self):
with self.assertRaises(exceptions.SchemaError):
self.Validator.check_schema({"pattern": "\q"})


class TestDraft6InvalidRegex(InvalidRegexMixin, TestCase):
Validator = validators.Draft6Validator


class TestDraft7InvalidRegex(InvalidRegexMixin, TestCase):
Validator = validators.Draft7Validator


class ValidatorTestMixin(MetaSchemaTestsMixin, object):
def test_valid_instances_are_valid(self):
schema, instance = self.valid
Expand Down
20 changes: 17 additions & 3 deletions jsonschema/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from six import add_metaclass

from jsonschema import (
_format,
_legacy_validators,
_types,
_utils,
Expand Down Expand Up @@ -158,6 +159,7 @@ def create(
default_types=None,
type_checker=None,
id_of=_id_of,
format_checker=None,
):
"""
Create a new validator class.
Expand Down Expand Up @@ -208,7 +210,14 @@ def create(

id_of (callable):

A function that given a schema, returns its ID.
a function that given a schema, returns its ID.

format_checker (jsonschema.FormatChecker):

a format checker, used when applying the :validator:`format`
validator.

If unprovided, none will be associated.

Returns:

Expand Down Expand Up @@ -284,8 +293,9 @@ def __init__(
self.schema = schema

@classmethod
def check_schema(cls, schema):
for error in cls(cls.META_SCHEMA).iter_errors(schema):
def check_schema(cls, schema, format_checker=format_checker):
validator = cls(cls.META_SCHEMA, format_checker=format_checker)
for error in validator.iter_errors(schema):
raise exceptions.SchemaError.create_from(error)

def iter_errors(self, instance, _schema=None):
Expand Down Expand Up @@ -465,6 +475,7 @@ def extend(validator, validators=(), version=None, type_checker=None):
type_checker=_types.draft3_type_checker,
version="draft3",
id_of=lambda schema: schema.get(u"id", ""),
format_checker=_format.draft3_format_checker,
)

Draft4Validator = create(
Expand Down Expand Up @@ -500,6 +511,7 @@ def extend(validator, validators=(), version=None, type_checker=None):
type_checker=_types.draft4_type_checker,
version="draft4",
id_of=lambda schema: schema.get(u"id", ""),
format_checker=_format.draft4_format_checker,
)

Draft6Validator = create(
Expand Down Expand Up @@ -539,6 +551,7 @@ def extend(validator, validators=(), version=None, type_checker=None):
},
type_checker=_types.draft6_type_checker,
version="draft6",
format_checker=_format.draft6_format_checker,
)

Draft7Validator = create(
Expand Down Expand Up @@ -579,6 +592,7 @@ def extend(validator, validators=(), version=None, type_checker=None):
},
type_checker=_types.draft7_type_checker,
version="draft7",
format_checker=_format.draft7_format_checker,
)

_LATEST_VERSION = Draft7Validator
Expand Down