Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_type_value(self):
# TODO: This test is currently semi broken, partial schemas might be somewhat broken possibly
# # Test that when only having a schema; rule it should throw error
# with pytest.raises(RuleError) as r:
# Rule(schema={"schema;fooone": {"type": "map", "mapping": {"foo": {"type": "str"}}}})
# assert str(r.value) == ""
# assert r.value.error_key == 'type.missing'
# Test a valid rule with both "str" and "unicode" types work
r = Rule(schema={"type": str("str")})
r = Rule(schema={"type": unicode("str")})
# Test that type key must be string otherwise exception is raised
with pytest.raises(RuleError) as r:
Rule(schema={"type": 1})
assert str(r.value) == ""
assert r.value.error_key == 'type.not_string'
# this tests that the type key must be a string
with pytest.raises(RuleError) as r:
Rule(schema={"type": 1}, parent=None)
assert str(r.value) == ""
assert r.value.error_key == 'type.not_string'
def _remap_errors(c):
return [unicode(error) for error in c.errors]
'msg': 'Foobar',
},
'errors': ["Value 'Foobar' is not of type 'int'. Path: '/msg'"]
}
source_f = tmpdir.join(u"2få.json")
source_f.write(yaml.safe_dump(fail_data_2f_yaml, allow_unicode=True))
_fail_tests = [
# Test mapping with unicode key and value but wrong type
(u"1f.yaml", SchemaError),
# Test unicode filename with validation errors.
# It is not possible to package a file with unicode characters
# like åäö in the filename in some python versions.
# Mock a file with åäö during testing to properly simulate this again.
(unicode(source_f), SchemaError),
# Test unicode data inside seq but wrong type
(u"3f.yaml", SchemaError),
]
for failing_test, exception_type in _fail_tests:
f = self.f(failing_test)
with open(f, "r") as stream:
yaml_data = yaml.safe_load(stream)
data = yaml_data["data"]
schema = yaml_data["schema"]
errors = yaml_data["errors"]
try:
print(u"Running test files: {0}".format(f))
c = Core(source_data=data, schema_data=schema)
def validate(self, raise_exception=True):
"""
"""
log.debug(u"starting core")
self._start_validate(self.source)
self.validation_errors = [unicode(error) for error in self.errors]
self.validation_errors_exceptions = self.errors
if self.errors is None or len(self.errors) == 0:
log.info(u"validation.valid")
else:
log.error(u"validation.invalid")
log.error(u" --- All found errors ---")
log.error(self.validation_errors)
if raise_exception:
raise SchemaError(u"Schema validation failed:\n - {error_msg}.".format(
error_msg=u'.\n - '.join(self.validation_errors)))
else:
log.error(u"Errors found but will not raise exception...")
# Return validated data
return self.source
# func keyword is not defined so nothing to do
if not func:
return
found_method = False
for extension in self.loaded_extensions:
method = getattr(extension, func, None)
if method:
found_method = True
# No exception will should be caught. If one is raised it should bubble up all the way.
ret = method(value, rule, path)
if ret is not True and ret is not None:
msg = '%s. Path: {path}' % unicode(ret)
self.errors.append(SchemaError.SchemaErrorEntry(
msg=msg,
path=path,
value=None))
# If False or None or some other object that is interpreted as False
if not ret:
raise CoreError(u"Error when running extension function : {0}".format(func))
# Only run the first matched function. Sinc loading order is determined
# it should be easy to determine which file is used before others
break
if not found_method:
raise CoreError(u"Did not find method '{0}' in any loaded extension file".format(func))
def _validate_scalar_type(self, value, t, path):
"""
"""
log.debug(u" # Core scalar: validating scalar type : %s", t)
log.debug(u" # Core scalar: scalar type: %s", type(value))
try:
if not tt[t](value):
self.errors.append(SchemaError.SchemaErrorEntry(
msg=u"Value '{value}' is not of type '{scalar_type}'. Path: '{path}'",
path=path,
value=unicode(value) if tt['str'](value) else value,
scalar_type=t))
return False
return True
except KeyError as e:
# Type not found in valid types mapping
log.debug(e)
raise CoreError(u"Unknown type check: {0!s} : {1!s} : {2!s}".format(path, value, t))