Skip to content
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

Use JS regex syntax #609

Merged
merged 6 commits into from
Oct 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
8 changes: 4 additions & 4 deletions json/tests/draft2019-09/optional/ecmascript-regex.json
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@
},
{
"description": "NKO DIGIT ZERO (as \\u escape) does not match",
"data": "\\u07c0",
"data": "\u07c0",
"valid": false
}
]
Expand All @@ -129,7 +129,7 @@
},
{
"description": "NKO DIGIT ZERO (as \\u escape) matches",
"data": "\\u07c0",
"data": "\u07c0",
"valid": true
}
]
Expand Down Expand Up @@ -186,7 +186,7 @@
},
{
"description": "latin-1 non-breaking-space does not match (unlike e.g. Python)",
"data": "\\u00a0",
"data": "\u00a0",
"valid": false
}
]
Expand All @@ -205,7 +205,7 @@
},
{
"description": "latin-1 non-breaking-space matches (unlike e.g. Python)",
"data": "\\u00a0",
"data": "\u00a0",
"valid": true
}
]
Expand Down
8 changes: 4 additions & 4 deletions json/tests/draft4/optional/ecmascript-regex.json
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@
},
{
"description": "NKO DIGIT ZERO (as \\u escape) does not match",
"data": "\\u07c0",
"data": "\u07c0",
"valid": false
}
]
Expand All @@ -129,7 +129,7 @@
},
{
"description": "NKO DIGIT ZERO (as \\u escape) matches",
"data": "\\u07c0",
"data": "\u07c0",
"valid": true
}
]
Expand Down Expand Up @@ -186,7 +186,7 @@
},
{
"description": "latin-1 non-breaking-space does not match (unlike e.g. Python)",
"data": "\\u00a0",
"data": "\u00a0",
"valid": false
}
]
Expand All @@ -205,7 +205,7 @@
},
{
"description": "latin-1 non-breaking-space matches (unlike e.g. Python)",
"data": "\\u00a0",
"data": "\u00a0",
"valid": true
}
]
Expand Down
8 changes: 4 additions & 4 deletions json/tests/draft6/optional/ecmascript-regex.json
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@
},
{
"description": "NKO DIGIT ZERO (as \\u escape) does not match",
"data": "\\u07c0",
"data": "\u07c0",
"valid": false
}
]
Expand All @@ -129,7 +129,7 @@
},
{
"description": "NKO DIGIT ZERO (as \\u escape) matches",
"data": "\\u07c0",
"data": "\u07c0",
"valid": true
}
]
Expand Down Expand Up @@ -186,7 +186,7 @@
},
{
"description": "latin-1 non-breaking-space does not match (unlike e.g. Python)",
"data": "\\u00a0",
"data": "\u00a0",
"valid": false
}
]
Expand All @@ -205,7 +205,7 @@
},
{
"description": "latin-1 non-breaking-space matches (unlike e.g. Python)",
"data": "\\u00a0",
"data": "\u00a0",
"valid": true
}
]
Expand Down
8 changes: 4 additions & 4 deletions json/tests/draft7/optional/ecmascript-regex.json
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@
},
{
"description": "NKO DIGIT ZERO (as \\u escape) does not match",
"data": "\\u07c0",
"data": "\u07c0",
"valid": false
}
]
Expand All @@ -129,7 +129,7 @@
},
{
"description": "NKO DIGIT ZERO (as \\u escape) matches",
"data": "\\u07c0",
"data": "\u07c0",
"valid": true
}
]
Expand Down Expand Up @@ -186,7 +186,7 @@
},
{
"description": "latin-1 non-breaking-space does not match (unlike e.g. Python)",
"data": "\\u00a0",
"data": "\u00a0",
"valid": false
}
]
Expand All @@ -205,7 +205,7 @@
},
{
"description": "latin-1 non-breaking-space matches (unlike e.g. Python)",
"data": "\\u00a0",
"data": "\u00a0",
"valid": true
}
]
Expand Down
6 changes: 4 additions & 2 deletions jsonschema/_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import socket
import struct

import js_regex

from jsonschema.compat import str_types
from jsonschema.exceptions import FormatError

Expand Down Expand Up @@ -297,11 +299,11 @@ def is_time(instance):
return is_datetime("1970-01-01T" + instance)


@_checks_drafts(name="regex", raises=re.error)
@_checks_drafts(name="regex", raises=(re.error, js_regex.NotJavascriptRegex))
def is_regex(instance):
if not isinstance(instance, str_types):
return True
return re.compile(instance)
return js_regex.compile(instance)


@_checks_drafts(draft3="date", draft7="date", raises=ValueError)
Expand Down
7 changes: 4 additions & 3 deletions jsonschema/_utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import itertools
import json
import pkgutil
import re

import js_regex

from jsonschema.compat import MutableMapping, str_types, urlsplit

Expand Down Expand Up @@ -92,10 +93,10 @@ def find_additional_properties(instance, schema):
"""

properties = schema.get("properties", {})
patterns = "|".join(schema.get("patternProperties", {}))
patterns = "|".join(sorted(schema.get("patternProperties", {})))
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorting ensures that this is always a single entry in the regex cache, which is quite important for performance (of this tiny function, anyway).

for property in instance:
if property not in properties:
if patterns and re.search(patterns, property):
if patterns and js_regex.compile(patterns).search(property):
continue
yield property

Expand Down
6 changes: 3 additions & 3 deletions jsonschema/_validators.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import re
import js_regex

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

for pattern, subschema in iteritems(patternProperties):
for k, v in iteritems(instance):
if re.search(pattern, k):
if js_regex.compile(pattern).search(k):
for error in validator.descend(
v, subschema, path=k, schema_path=pattern,
):
Expand Down Expand Up @@ -197,7 +197,7 @@ def uniqueItems(validator, uI, instance, schema):
def pattern(validator, patrn, instance, schema):
if (
validator.is_type(instance, "string") and
not re.search(patrn, instance)
not js_regex.compile(patrn).search(instance)
):
yield ValidationError("%r does not match %r" % (instance, patrn))

Expand Down
11 changes: 7 additions & 4 deletions jsonschema/tests/test_jsonschema_test_suite.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ def narrow_unicode_build(test): # pragma: no cover

TestDraft3 = DRAFT3.to_unittest_testcase(
DRAFT3.tests(),
DRAFT3.optional_tests_of(name="format"),
DRAFT3.optional_tests_of(name="bignum"),
DRAFT3.optional_tests_of(name="format"),
DRAFT3.optional_tests_of(name="zeroTerminatedFloats"),
Validator=Draft3Validator,
format_checker=draft3_format_checker,
Expand All @@ -87,8 +87,9 @@ def narrow_unicode_build(test): # pragma: no cover

TestDraft4 = DRAFT4.to_unittest_testcase(
DRAFT4.tests(),
DRAFT4.optional_tests_of(name="format"),
DRAFT4.optional_tests_of(name="bignum"),
DRAFT4.optional_tests_of(name="ecmascript-regex"),
DRAFT4.optional_tests_of(name="format"),
DRAFT4.optional_tests_of(name="zeroTerminatedFloats"),
Validator=Draft4Validator,
format_checker=draft4_format_checker,
Expand Down Expand Up @@ -135,8 +136,9 @@ def narrow_unicode_build(test): # pragma: no cover

TestDraft6 = DRAFT6.to_unittest_testcase(
DRAFT6.tests(),
DRAFT6.optional_tests_of(name="format"),
DRAFT6.optional_tests_of(name="bignum"),
DRAFT6.optional_tests_of(name="ecmascript-regex"),
DRAFT6.optional_tests_of(name="format"),
DRAFT6.optional_tests_of(name="zeroTerminatedFloats"),
Validator=Draft6Validator,
format_checker=draft6_format_checker,
Expand Down Expand Up @@ -185,8 +187,9 @@ def narrow_unicode_build(test): # pragma: no cover
DRAFT7.tests(),
DRAFT7.format_tests(),
DRAFT7.optional_tests_of(name="bignum"),
DRAFT7.optional_tests_of(name="zeroTerminatedFloats"),
DRAFT7.optional_tests_of(name="content"),
DRAFT7.optional_tests_of(name="ecmascript-regex"),
DRAFT7.optional_tests_of(name="zeroTerminatedFloats"),
Validator=Draft7Validator,
format_checker=draft7_format_checker,
skip=lambda test: (
Expand Down
6 changes: 3 additions & 3 deletions jsonschema/tests/test_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -849,7 +849,7 @@ def test_recursive(self):
"children": {
"type": "object",
"patternProperties": {
"^.*$": {
u"^.*$": {
"$ref": "#/definitions/node",
},
},
Expand Down Expand Up @@ -951,8 +951,8 @@ def test_patternProperties(self):
instance = {"bar": 1, "foo": 2}
schema = {
"patternProperties": {
"bar": {"type": "string"},
"foo": {"minimum": 5},
u"bar": {"type": "string"},
u"foo": {"minimum": 5},
},
}

Expand Down
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ setup_requires = setuptools_scm
install_requires =
attrs>=17.4.0
importlib_metadata
js-regex>=1.0.0
pyrsistent>=0.14.0
setuptools
six>=1.11.0
Expand Down