Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def __normalize_coerce(self, processor, field, value, nullable, error):
if isinstance(processor, str):
processor = self.__get_rule_handler('normalize_coerce', processor)
elif isinstance(processor, Iterable):
result = value
for p in processor:
result = self.__normalize_coerce(p, field, result, nullable, error)
if (
errors.COERCION_FAILED
in self.document_error_tree.fetch_errors_from(
self.document_path + (field,)
)
):
break
return result
try:
return processor(value)
except Exception as e:
if not (nullable and value is None):
self._error(field, error, str(e))
return value
def __normalize_coerce(self, processor, field, value, nullable, error):
if isinstance(processor, _str_type):
processor = self.__get_rule_handler('normalize_coerce', processor)
elif isinstance(processor, Iterable):
result = value
for p in processor:
result = self.__normalize_coerce(p, field, result, nullable, error)
if (
errors.COERCION_FAILED
in self.document_error_tree.fetch_errors_from(
self.document_path + (field,)
)
):
break
return result
try:
return processor(value)
except Exception as e:
if not nullable and e is not TypeError:
self._error(field, error, str(e))
return value
def __normalize_coerce(self, processor, field, value, nullable, error):
if isinstance(processor, _str_type):
processor = self.__get_rule_handler('normalize_coerce', processor)
elif isinstance(processor, Iterable):
result = value
for p in processor:
result = self.__normalize_coerce(p, field, result, nullable, error)
if (
errors.COERCION_FAILED
in self.document_error_tree.fetch_errors_from(
self.document_path + (field,)
)
):
break
return result
try:
return processor(value)
except Exception as e:
if not (nullable and value is None):
self._error(field, error, str(e))
return value
def _normalize_coerce(self, mapping, schema):
""" {'oneof': [
{'type': 'callable'},
{'type': 'list',
'schema': {'oneof': [{'type': 'callable'},
{'type': 'string'}]}},
{'type': 'string'}
]} """
error = errors.COERCION_FAILED
for field in mapping:
if field in schema and 'coerce' in schema[field]:
mapping[field] = self.__normalize_coerce(
schema[field]['coerce'],
field,
mapping[field],
schema[field].get('nullable', False),
error,
)
elif (
isinstance(self.allow_unknown, Mapping)
and 'coerce' in self.allow_unknown
):
mapping[field] = self.__normalize_coerce(
self.allow_unknown['coerce'],
field,
def _normalize_coerce(self, mapping, schema):
""" {'oneof': [
{'type': 'callable'},
{'type': 'list',
'schema': {'oneof': [{'type': 'callable'},
{'type': 'string'}]}},
{'type': 'string'}
]} """
error = errors.COERCION_FAILED
for field in mapping:
if field in schema and 'coerce' in schema[field]:
mapping[field] = self.__normalize_coerce(
schema[field]['coerce'],
field,
mapping[field],
schema[field].get('nullable', False),
error,
)
elif (
isinstance(self.allow_unknown, Mapping)
and 'coerce' in self.allow_unknown
):
mapping[field] = self.__normalize_coerce(
self.allow_unknown['coerce'],
field,
def _normalize_coerce(self, mapping, schema):
""" {'oneof': [
{'type': 'callable'},
{'type': 'list',
'itemsrules': {'oneof': [{'type': 'callable'},
{'type': 'string'}]}},
{'type': 'string'}
]} """
error = errors.COERCION_FAILED
for field in mapping:
if field in schema and 'coerce' in schema[field]:
mapping[field] = self.__normalize_coerce(
schema[field]['coerce'],
field,
mapping[field],
schema[field].get('nullable', False),
error,
)
elif (
isinstance(self.allow_unknown, Mapping)
and 'coerce' in self.allow_unknown
):
mapping[field] = self.__normalize_coerce(
self.allow_unknown['coerce'],
field,