Skip to content

Commit 1a02f54

Browse files
committed
Respect ECMA regex syntax for schemas.
Merge remote-tracking branch 'Zac-HD/js-regex' * Zac-HD/js-regex: Validate `format: regex` with js-regex Squashed 'json/' changes from 2d554504..433ab2f0 Minor style. Use the corresponding tests. Use JS regex syntax
2 parents cbb222c + b3f0aad commit 1a02f54

File tree

6 files changed

+22
-15
lines changed

6 files changed

+22
-15
lines changed

jsonschema/_format.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import socket
44
import struct
55

6+
import js_regex
7+
68
from jsonschema.compat import str_types
79
from jsonschema.exceptions import FormatError
810

@@ -297,11 +299,11 @@ def is_time(instance):
297299
return is_datetime("1970-01-01T" + instance)
298300

299301

300-
@_checks_drafts(name="regex", raises=re.error)
302+
@_checks_drafts(name="regex", raises=(re.error, js_regex.NotJavascriptRegex))
301303
def is_regex(instance):
302304
if not isinstance(instance, str_types):
303305
return True
304-
return re.compile(instance)
306+
return js_regex.compile(instance)
305307

306308

307309
@_checks_drafts(draft3="date", draft7="date", raises=ValueError)

jsonschema/_utils.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import itertools
22
import json
33
import pkgutil
4-
import re
4+
5+
import js_regex
56

67
from jsonschema.compat import MutableMapping, str_types, urlsplit
78

@@ -92,10 +93,10 @@ def find_additional_properties(instance, schema):
9293
"""
9394

9495
properties = schema.get("properties", {})
95-
patterns = "|".join(schema.get("patternProperties", {}))
96+
patterns = "|".join(sorted(schema.get("patternProperties", {})))
9697
for property in instance:
9798
if property not in properties:
98-
if patterns and re.search(patterns, property):
99+
if patterns and js_regex.compile(patterns).search(property):
99100
continue
100101
yield property
101102

jsonschema/_validators.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import re
1+
import js_regex
22

33
from jsonschema._utils import (
44
ensure_list,
@@ -19,7 +19,7 @@ def patternProperties(validator, patternProperties, instance, schema):
1919

2020
for pattern, subschema in iteritems(patternProperties):
2121
for k, v in iteritems(instance):
22-
if re.search(pattern, k):
22+
if js_regex.compile(pattern).search(k):
2323
for error in validator.descend(
2424
v, subschema, path=k, schema_path=pattern,
2525
):
@@ -197,7 +197,7 @@ def uniqueItems(validator, uI, instance, schema):
197197
def pattern(validator, patrn, instance, schema):
198198
if (
199199
validator.is_type(instance, "string") and
200-
not re.search(patrn, instance)
200+
not js_regex.compile(patrn).search(instance)
201201
):
202202
yield ValidationError("%r does not match %r" % (instance, patrn))
203203

jsonschema/tests/test_jsonschema_test_suite.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ def narrow_unicode_build(test): # pragma: no cover
6868

6969
TestDraft3 = DRAFT3.to_unittest_testcase(
7070
DRAFT3.tests(),
71-
DRAFT3.optional_tests_of(name="format"),
7271
DRAFT3.optional_tests_of(name="bignum"),
72+
DRAFT3.optional_tests_of(name="format"),
7373
DRAFT3.optional_tests_of(name="zeroTerminatedFloats"),
7474
Validator=Draft3Validator,
7575
format_checker=draft3_format_checker,
@@ -87,8 +87,9 @@ def narrow_unicode_build(test): # pragma: no cover
8787

8888
TestDraft4 = DRAFT4.to_unittest_testcase(
8989
DRAFT4.tests(),
90-
DRAFT4.optional_tests_of(name="format"),
9190
DRAFT4.optional_tests_of(name="bignum"),
91+
DRAFT4.optional_tests_of(name="ecmascript-regex"),
92+
DRAFT4.optional_tests_of(name="format"),
9293
DRAFT4.optional_tests_of(name="zeroTerminatedFloats"),
9394
Validator=Draft4Validator,
9495
format_checker=draft4_format_checker,
@@ -135,8 +136,9 @@ def narrow_unicode_build(test): # pragma: no cover
135136

136137
TestDraft6 = DRAFT6.to_unittest_testcase(
137138
DRAFT6.tests(),
138-
DRAFT6.optional_tests_of(name="format"),
139139
DRAFT6.optional_tests_of(name="bignum"),
140+
DRAFT6.optional_tests_of(name="ecmascript-regex"),
141+
DRAFT6.optional_tests_of(name="format"),
140142
DRAFT6.optional_tests_of(name="zeroTerminatedFloats"),
141143
Validator=Draft6Validator,
142144
format_checker=draft6_format_checker,
@@ -185,8 +187,9 @@ def narrow_unicode_build(test): # pragma: no cover
185187
DRAFT7.tests(),
186188
DRAFT7.format_tests(),
187189
DRAFT7.optional_tests_of(name="bignum"),
188-
DRAFT7.optional_tests_of(name="zeroTerminatedFloats"),
189190
DRAFT7.optional_tests_of(name="content"),
191+
DRAFT7.optional_tests_of(name="ecmascript-regex"),
192+
DRAFT7.optional_tests_of(name="zeroTerminatedFloats"),
190193
Validator=Draft7Validator,
191194
format_checker=draft7_format_checker,
192195
skip=lambda test: (

jsonschema/tests/test_validators.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -849,7 +849,7 @@ def test_recursive(self):
849849
"children": {
850850
"type": "object",
851851
"patternProperties": {
852-
"^.*$": {
852+
u"^.*$": {
853853
"$ref": "#/definitions/node",
854854
},
855855
},
@@ -951,8 +951,8 @@ def test_patternProperties(self):
951951
instance = {"bar": 1, "foo": 2}
952952
schema = {
953953
"patternProperties": {
954-
"bar": {"type": "string"},
955-
"foo": {"minimum": 5},
954+
u"bar": {"type": "string"},
955+
u"foo": {"minimum": 5},
956956
},
957957
}
958958

setup.cfg

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ setup_requires = setuptools_scm
2828
install_requires =
2929
attrs>=17.4.0
3030
importlib_metadata
31+
js-regex>=1.0.0
3132
pyrsistent>=0.14.0
3233
setuptools
3334
six>=1.11.0

0 commit comments

Comments
 (0)