Skip to content

Commit 69aa835

Browse files
committed
Support of in ref resolver
1 parent 7129c47 commit 69aa835

File tree

6 files changed

+17
-50
lines changed

6 files changed

+17
-50
lines changed

fastjsonschema/draft06.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
class CodeGeneratorDraft06(CodeGeneratorDraft04):
66
FORMAT_REGEXS = dict(CodeGeneratorDraft04.FORMAT_REGEXS, **{
77
'json-pointer': r'^(/(([^/~])|(~[01]))*)*$',
8-
#'uri-reference': r'',
8+
'uri-reference': r'^(\w+:(\/?\/?))?[^#\\\s]*(#[^\\\s]*)?$',
99
'uri-template': (
1010
r'^(?:(?:[^\x00-\x20\"\'<>%\\^`{|}]|%[0-9a-f]{2})|'
1111
r'\{[+#./;?&=,!@|]?(?:[a-z0-9_]|%[0-9a-f]{2})+'

fastjsonschema/draft07.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
class CodeGeneratorDraft07(CodeGeneratorDraft06):
55
FORMAT_REGEXS = dict(CodeGeneratorDraft06.FORMAT_REGEXS, **{
66
'date': r'^(?P<year>\d{4})-(?P<month>\d{1,2})-(?P<day>\d{1,2})$',
7-
#'iri': r'',
8-
#'iri-reference': r'',
9-
#'idn-email': r'',
7+
'iri': r'^\w+:(\/?\/?)[^\s]+$',
8+
'iri-reference': r'^(\w+:(\/?\/?))?[^#\\\s]*(#[^\\\s]*)?$',
9+
'idn-email': r'^\w+@\w+\.\w+$',
1010
#'idn-hostname': r'',
1111
'relative-json-pointer': r'^(?:0|[1-9][0-9]*)(?:#|(?:\/(?:[^~/]|~0|~1)*)*)$',
1212
#'regex': r'',

fastjsonschema/ref_resolver.py

+12-26
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,6 @@ def resolve_path(schema, fragment):
2323
Return definition from path.
2424
2525
Path is unescaped according https://tools.ietf.org/html/rfc6901
26-
27-
:argument schema: the referrant schema document
28-
:argument str fragment: a URI fragment to resolve within it
29-
:returns: the retrieved schema definition
30-
3126
"""
3227
fragment = fragment.lstrip('/')
3328
parts = unquote(fragment).split('/') if fragment else []
@@ -59,11 +54,6 @@ def resolve_remote(uri, handlers):
5954
For unknown schemes urlib is used with UTF-8 encoding.
6055
6156
.. _Requests: http://pypi.python.org/pypi/requests/
62-
63-
:argument str uri: the URI to resolve
64-
:argument dict handlers: the URI resolver functions for each scheme
65-
:returns: the retrieved schema document
66-
6757
"""
6858
scheme = urlparse.urlsplit(uri).scheme
6959
if scheme in handlers:
@@ -78,18 +68,13 @@ def resolve_remote(uri, handlers):
7868
class RefResolver:
7969
"""
8070
Resolve JSON References.
81-
82-
:argument str base_uri: URI of the referring document
83-
:argument schema: the actual referring schema document
84-
:argument dict store: a mapping from URIs to documents to cache
85-
:argument bool cache: whether remote refs should be cached after
86-
first resolution
87-
:argument dict handlers: a mapping from URI schemes to functions that
88-
should be used to retrieve them
8971
"""
9072

9173
# pylint: disable=dangerous-default-value,too-many-arguments
9274
def __init__(self, base_uri, schema, store={}, cache=True, handlers={}):
75+
"""
76+
`base_uri` is URI of the referring document from the `schema`.
77+
"""
9378
self.base_uri = base_uri
9479
self.resolution_scope = base_uri
9580
self.schema = schema
@@ -102,19 +87,19 @@ def __init__(self, base_uri, schema, store={}, cache=True, handlers={}):
10287
def from_schema(cls, schema, handlers={}, **kwargs):
10388
"""
10489
Construct a resolver from a JSON schema object.
105-
106-
:argument schema schema: the referring schema
107-
:rtype: :class:`RefResolver`
10890
"""
10991
return cls(
110-
schema.get('id', '') if isinstance(schema, dict) else '',
92+
schema.get('$id', schema.get('id', '')) if isinstance(schema, dict) else '',
11193
schema,
11294
handlers=handlers,
11395
**kwargs
11496
)
11597

11698
@contextlib.contextmanager
117-
def in_scope(self, scope):
99+
def in_scope(self, scope: str):
100+
"""
101+
Context manager to handle current scope.
102+
"""
118103
old_scope = self.resolution_scope
119104
self.resolution_scope = urlparse.urljoin(old_scope, scope)
120105
try:
@@ -123,12 +108,10 @@ def in_scope(self, scope):
123108
self.resolution_scope = old_scope
124109

125110
@contextlib.contextmanager
126-
def resolving(self, ref):
111+
def resolving(self, ref: str):
127112
"""
128113
Context manager which resolves a JSON ``ref`` and enters the
129114
resolution scope of this ref.
130-
131-
:argument str ref: reference to resolve
132115
"""
133116
new_uri = urlparse.urljoin(self.resolution_scope, ref)
134117
uri, fragment = urlparse.urldefrag(new_uri)
@@ -154,6 +137,9 @@ def get_uri(self):
154137
return normalize(self.resolution_scope)
155138

156139
def get_scope_name(self):
140+
"""
141+
Get current scope and return it as a valid function name.
142+
"""
157143
name = 'validate_' + unquote(self.resolution_scope).replace('~1', '_').replace('~0', '_')
158144
name = re.sub(r'[:/#\.\-\%]', '_', name)
159145
name = name.lower().rstrip('_')

tests/json_schema/test_draft06.py

-8
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,6 @@ def pytest_generate_tests(metafunc):
1111
# Optional.
1212
'ecmascript-regex.json',
1313
],
14-
ignore_tests=[
15-
'Recursive references between schemas',
16-
'validation of URI References',
17-
'base URI change - change folder',
18-
'base URI change - change folder in subschema',
19-
'base URI change',
20-
'root ref in remote ref',
21-
],
2214
)
2315
metafunc.parametrize(['version', 'schema', 'data', 'is_valid'], param_values, ids=param_ids)
2416

tests/json_schema/test_draft07.py

-11
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,7 @@ def pytest_generate_tests(metafunc):
1010
ignored_suite_files=[
1111
# Optional.
1212
'ecmascript-regex.json',
13-
'idn-email.json',
1413
'idn-hostname.json',
15-
'iri-reference.json',
16-
'iri.json',
17-
],
18-
ignore_tests=[
19-
'Recursive references between schemas',
20-
'validation of URI References',
21-
'base URI change - change folder',
22-
'base URI change - change folder in subschema',
23-
'base URI change',
24-
'root ref in remote ref',
2514
],
2615
)
2716
metafunc.parametrize(['version', 'schema', 'data', 'is_valid'], param_values, ids=param_ids)

tests/json_schema/utils.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def remotes_handler(uri):
2929
return requests.get(uri).json()
3030

3131

32-
def resolve_param_values_and_ids(version, suite_dir, ignored_suite_files, ignore_tests):
32+
def resolve_param_values_and_ids(version, suite_dir, ignored_suite_files=[], ignore_tests=[]):
3333
suite_dir_path = Path(suite_dir).resolve()
3434
test_file_paths = sorted(set(suite_dir_path.glob("**/*.json")))
3535

0 commit comments

Comments
 (0)