本文整理匯總了Python中jsonschema.FormatChecker類的典型用法代碼示例。如果您正苦於以下問題:Python FormatChecker類的具體用法?Python FormatChecker怎麽用?Python FormatChecker使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了FormatChecker類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: test_it_can_register_checkers
def test_it_can_register_checkers(self):
checker = FormatChecker()
checker.checks("boom")(boom)
self.assertEqual(
checker.checkers,
dict(FormatChecker.checkers, boom=(boom, ()))
)
示例2: test_validate_with_format
def test_validate_with_format(app, db):
"""Test that validation can accept custom format rules."""
with app.app_context():
checker = FormatChecker()
checker.checks("foo")(lambda el: el.startswith("foo"))
data = {"bar": "foo", "$schema": {"properties": {"bar": {"format": "foo"}}}}
# test record creation with valid data
assert data == Record.create(data)
record = Record.create(data, format_checker=checker)
# test direct call to validate with valid data
assert record.validate(format_checker=checker) is None
# test commit with valid data
record.commit(format_checker=checker)
record["bar"] = "bar"
# test direct call to validate with invalid data
with pytest.raises(ValidationError) as excinfo:
record.validate(format_checker=checker)
assert "'bar' is not a 'foo'" in str(excinfo.value)
# test commit with invalid data
with pytest.raises(ValidationError) as excinfo:
record.commit(format_checker=checker)
assert "'bar' is not a 'foo'" in str(excinfo.value)
data["bar"] = "bar"
# test record creation with invalid data
with pytest.raises(ValidationError) as excinfo:
record = Record.create(data, format_checker=checker)
assert "'bar' is not a 'foo'" in str(excinfo.value)
示例3: test_it_can_register_checkers
def test_it_can_register_checkers(self):
checker = FormatChecker()
checker.checks("new")(self.fn)
self.assertEqual(
checker.checkers,
dict(FormatChecker.checkers, new=(self.fn, ()))
)
示例4: test_it_can_register_cls_checkers
def test_it_can_register_cls_checkers(self):
original = dict(FormatChecker.checkers)
self.addCleanup(FormatChecker.checkers.pop, "boom")
FormatChecker.cls_checks("boom")(boom)
self.assertEqual(
FormatChecker.checkers,
dict(original, boom=(boom, ())),
)
示例5: test_format_checkers_come_with_defaults
def test_format_checkers_come_with_defaults(self):
# This is bad :/ but relied upon.
# The docs for quite awhile recommended people do things like
# validate(..., format_checker=FormatChecker())
# We should change that, but we can't without deprecation...
checker = FormatChecker()
with self.assertRaises(FormatError):
checker.check(instance="not-an-ipv4", format="ipv4")
示例6: get_schema_validation_errors
def get_schema_validation_errors(json_data, schema_url, current_app):
schema = requests.get(schema_url).json()
validation_errors = collections.defaultdict(list)
format_checker = FormatChecker()
if current_app == 'cove-360':
format_checker.checkers['date-time'] = (datetime_or_date, ValueError)
for n, e in enumerate(validator(schema, format_checker=format_checker).iter_errors(json_data)):
validation_errors[e.message].append("/".join(str(item) for item in e.path))
return dict(validation_errors)
示例7: is_valid_json
def is_valid_json(data, schema):
checker = FormatChecker();
# add the "interval" format
checker.checks("interval")(parse_iso8601_interval)
validator = Draft4Validator(schema, format_checker=checker)
errors = []
for error in validator.iter_errors(data):
errors.append(error.message)
return errors
示例8: test_format_error_causes_become_validation_error_causes
def test_format_error_causes_become_validation_error_causes(self):
checker = FormatChecker()
checker.checks("foo", raises=ValueError)(self.fn)
cause = self.fn.side_effect = ValueError()
validator = Draft4Validator({"format": "foo"}, format_checker=checker)
with self.assertRaises(ValidationError) as cm:
validator.validate("bar")
self.assertIs(cm.exception.__cause__, cause)
示例9: test_format_error_causes_become_validation_error_causes
def test_format_error_causes_become_validation_error_causes(self):
checker = FormatChecker()
checker.checks("boom", raises=ValueError)(boom)
validator = Draft4Validator({"format": "boom"}, format_checker=checker)
with self.assertRaises(ValidationError) as cm:
validator.validate("BOOM")
self.assertIs(cm.exception.cause, BOOM)
self.assertIs(cm.exception.__cause__, BOOM)
示例10: test_invalid_format_default_message
def test_invalid_format_default_message(self):
checker = FormatChecker(formats=())
check_fn = mock.Mock(return_value=False)
checker.checks("thing")(check_fn)
schema = {"format" : "thing"}
message = self.message_for("bla", schema, format_checker=checker)
self.assertIn(repr("bla"), message)
self.assertIn(repr("thing"), message)
self.assertIn("is not a", message)
示例11: __init__
def __init__(self, spec_dict, origin_url=None, http_client=None,
config=None):
self.spec_dict = spec_dict
self.origin_url = origin_url
self.http_client = http_client
self.api_url = None
self.config = dict(CONFIG_DEFAULTS, **(config or {}))
# Cached copy of spec_dict with x-scope metadata removed.
# See @property client_spec_dict().
self._client_spec_dict = None
# (key, value) = (simple format def name, Model type)
# (key, value) = (#/ format def ref, Model type)
self.definitions = {}
# (key, value) = (simple resource name, Resource)
# (key, value) = (#/ format resource ref, Resource)
self.resources = None
# (key, value) = (simple ref name, param_spec in dict form)
# (key, value) = (#/ format ref name, param_spec in dict form)
self.params = None
# Built on-demand - see get_op_for_request(..)
self._request_to_op_map = None
# (key, value) = (format name, SwaggerFormat)
self.user_defined_formats = {}
self.format_checker = FormatChecker()
self.resolver = RefResolver(
base_uri=origin_url or '',
referrer=self.spec_dict,
handlers=build_http_handlers(http_client))
示例12: __init__
def __init__(self, spec_dict, origin_url=None, http_client=None,
config=None):
self.spec_dict = spec_dict
self.origin_url = origin_url
self.http_client = http_client
self.api_url = None
self.config = dict(CONFIG_DEFAULTS, **(config or {}))
# (key, value) = (simple format def name, Model type)
# (key, value) = (#/ format def ref, Model type)
self.definitions = None
# (key, value) = (simple resource name, Resource)
# (key, value) = (#/ format resource ref, Resource)
self.resources = None
# (key, value) = (simple ref name, param_spec in dict form)
# (key, value) = (#/ format ref name, param_spec in dict form)
self.params = None
# Built on-demand - see get_op_for_request(..)
self._request_to_op_map = None
# (key, value) = (format name, SwaggerFormat)
self.user_defined_formats = {}
self.format_checker = FormatChecker()
示例13: test_validate_with_format
def test_validate_with_format(app, db):
"""Test that validation can accept custom format rules."""
with app.app_context():
checker = FormatChecker()
checker.checks('foo')(lambda el: el.startswith('foo'))
record = Record.create({
'bar': 'foo',
'$schema': {
'properties': {
'bar': {'format': 'foo'}
}
}
})
assert record.validate(format_checker=checker) is None
record['bar'] = 'bar'
with pytest.raises(ValidationError) as excinfo:
record.validate(format_checker=checker)
assert "'bar' is not a 'foo'" in str(excinfo.value)
示例14: test_it_catches_registered_errors
def test_it_catches_registered_errors(self):
checker = FormatChecker()
checker.checks("boom", raises=type(BOOM))(boom)
with self.assertRaises(FormatError) as cm:
checker.check(instance=12, format="boom")
self.assertIs(cm.exception.cause, BOOM)
self.assertIs(cm.exception.__cause__, BOOM)
# Unregistered errors should not be caught
with self.assertRaises(type(BANG)):
checker.check(instance="bang", format="boom")
示例15: test_it_catches_registered_errors
def test_it_catches_registered_errors(self):
checker = FormatChecker()
checker.checks("foo", raises=ValueError)(self.fn)
# Registered errors should be caught and turned into FormatErrors
cause = ValueError()
self.fn.side_effect = cause
with self.assertRaises(FormatError) as cm:
checker.check("bar", "foo")
# Original exception should be attached to cause attribute
self.assertIs(cm.exception.cause, cause)
# Unregistered errors should not be caught
self.fn.side_effect = AttributeError
with self.assertRaises(AttributeError):
checker.check("bar", "foo")