Skip to content

Commit 9d3115a

Browse files
committed
Merge branch 'master' into regex_value_validation
2 parents 634b503 + fe72efe commit 9d3115a

File tree

3 files changed

+40
-19
lines changed

3 files changed

+40
-19
lines changed

json/bin/jsonschema_suite

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,14 @@
11
#! /usr/bin/env python
22
from __future__ import print_function
3-
import sys
4-
import textwrap
5-
6-
try:
7-
import argparse
8-
except ImportError:
9-
print(textwrap.dedent("""
10-
The argparse library could not be imported. jsonschema_suite requires
11-
either Python 2.7 or for you to install argparse. You can do so by
12-
running `pip install argparse`, `easy_install argparse` or by
13-
downloading argparse and running `python2.6 setup.py install`.
14-
15-
See https://pypi.python.org/pypi/argparse for details.
16-
""".strip("\n")))
17-
sys.exit(1)
18-
3+
import argparse
194
import errno
205
import fnmatch
216
import json
227
import os
238
import random
249
import shutil
10+
import sys
11+
import textwrap
2512
import unittest
2613
import warnings
2714

jsonschema/tests/test_validators.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1119,7 +1119,7 @@ def test(self, format=format):
11191119
setattr(TestBuiltinFormats, name, test)
11201120

11211121

1122-
class TestValidatorFor(TestCase):
1122+
class TestValidatorFor(SynchronousTestCase):
11231123
def test_draft_3(self):
11241124
schema = {"$schema": "http://json-schema.org/draft-03/schema"}
11251125
self.assertIs(
@@ -1213,6 +1213,26 @@ def test_validator_for_jsonschema_default(self):
12131213
def test_validator_for_custom_default(self):
12141214
self.assertIs(validators.validator_for({}, default=None), None)
12151215

1216+
def test_warns_if_meta_schema_specified_was_not_found(self):
1217+
self.assertWarns(
1218+
category=DeprecationWarning,
1219+
message=(
1220+
"The metaschema specified by $schema was not found. "
1221+
"Using the latest draft to validate, but this will raise "
1222+
"an error in the future."
1223+
),
1224+
# https://tm.tl/9363 :'(
1225+
filename=sys.modules[self.assertWarns.__module__].__file__,
1226+
1227+
f=validators.validator_for,
1228+
schema={u"$schema": "unknownSchema"},
1229+
default={},
1230+
)
1231+
1232+
def test_does_not_warn_if_meta_schema_is_unspecified(self):
1233+
validators.validator_for(schema={}, default={}),
1234+
self.assertFalse(self.flushWarnings())
1235+
12161236

12171237
class TestValidate(TestCase):
12181238
def assertUses(self, schema, Validator):

jsonschema/validators.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,10 @@ def create(
187187
will be converted to functions and redefined in this object's
188188
`jsonschema.TypeChecker`.
189189
190+
id_of (callable):
191+
192+
A function that given a schema, returns its ID.
193+
190194
Returns:
191195
192196
a new `jsonschema.IValidator` class
@@ -895,6 +899,16 @@ def validator_for(schema, default=_LATEST_VERSION):
895899
If unprovided, the default is to return
896900
the latest supported draft.
897901
"""
898-
if schema is True or schema is False:
902+
if schema is True or schema is False or u"$schema" not in schema:
899903
return default
900-
return meta_schemas.get(schema.get(u"$schema", u""), default)
904+
if schema[u"$schema"] not in meta_schemas:
905+
warn(
906+
(
907+
"The metaschema specified by $schema was not found. "
908+
"Using the latest draft to validate, but this will raise "
909+
"an error in the future."
910+
),
911+
DeprecationWarning,
912+
stacklevel=2,
913+
)
914+
return meta_schemas.get(schema[u"$schema"], _LATEST_VERSION)

0 commit comments

Comments
 (0)