def test_walk(walk_test): datatype, actual, call_args_list = walk_test callback_mock = Mock() result = walk(datatype, actual, callback_mock) assert callback_mock.call_args_list == call_args_list assert result == actual
def failures(datatype, value): """Return list of failures (if any) validating `value` against `datatype`. Params: `datatype`: Datatype to validate value against. See README.markdown for examples. `value`: Value to validate `path`: Used internally for location of failures. Example: >>> failures('int', 'foo') ['expected int, got str'] """ fails = [] def validate(path, *args): msg = '%s: %%s' % path if path else '%s' fails.extend(msg % m for m in validate_step(*args) or []) walk(datatype, value, validate) return fails
def coerce_value(datatype, value): """Attempt to coerce value to requested datatype. Example: >>> coerce_value("int", "5") 5 If coercion is not possible, the incoercible portion is left changed. Example: >>> coerce_value(['int'], ['1', '2', 'c']) [1, 2, 'c'] """ return walk(datatype, value, coerce_step)