async def test_error_reporting(): s = Schema( { '<files>': [Use(open, error='<files> should be readable')], '<path>': And(os.path.exists, error='<path> should exist'), '--count': Or(None, And(Use(int), lambda n: 0 < n < 5), error='--count should be integer 0 < n < 5') }, error='Error:') await s.validate({'<files>': [], '<path>': './', '--count': 3}) try: await s.validate({'<files>': [], '<path>': './', '--count': '10'}) except SchemaError as e: assert e.code == 'Error:\n--count should be integer 0 < n < 5' try: await s.validate({'<files>': [], '<path>': './hai', '--count': '2'}) except SchemaError as e: assert e.code == 'Error:\n<path> should exist' try: await s.validate({'<files>': ['hai'], '<path>': './', '--count': '2'}) except SchemaError as e: assert e.code == 'Error:\n<files> should be readable'
async def test_missing_keys_exception_with_non_str_dict_keys(): s = Schema({And(str, Use(str.lower), 'name'): And(str, len)}) with SE: await s.validate(dict()) with SE: try: await Schema({1: 'x'}).validate(dict()) except SchemaMissingKeyError as e: assert e.args[0] == "Missing keys: 1" raise
async def test_complex(): s = Schema({ '<file>': And([Use(open)], lambda l: len(l)), '<path>': os.path.exists, Optional('--count'): And(int, lambda n: 0 <= n <= 5) }) data = await s.validate({'<file>': ['./LICENSE-MIT'], '<path>': './'}) assert len(data) == 2 assert len(data['<file>']) == 1 assert data['<file>'][0].read().startswith('Copyright') assert data['<path>'] == './'
async def test_test(): def unique_list(_list): return len(_list) == len(set(_list)) def dict_keys(key, _list): return list(map(lambda d: d[key], _list)) schema = (Schema(Const(And(Use(partial(dict_keys, "index")), unique_list)))) data = [{"index": 1, "value": "foo"}, {"index": 2, "value": "bar"}] assert await schema.validate(data) == data bad_data = [{"index": 1, "value": "foo"}, {"index": 1, "value": "bar"}] with SE: await schema.validate(bad_data)
async def test_use_json(): import json gist_schema = Schema( And( Use(json.loads), # first convert from JSON { Optional('description'): basestring, 'public': bool, 'files': { basestring: { 'content': basestring } } })) gist = '''{"description": "the description for this gist", "public": true, "files": { "file1.txt": {"content": "String file contents"}, "other.txt": {"content": "Another file contents"}}}''' assert await gist_schema.validate(gist)
async def test_dict_optional_defaults(): # Optionals fill out their defaults: assert await Schema({ Optional('a', default=1): 11, Optional('b', default=2): 22 }).validate({'a': 11}) == { 'a': 11, 'b': 2 } # Optionals take precedence over types. Here, the "a" is served by the # Optional: assert await Schema({ Optional('a', default=1): 11, basestring: 22 }).validate({'b': 22}) == { 'a': 1, 'b': 22 } with raises(TypeError): Optional(And(str, Use(int)), default=7)
async def test_schema_repr(): # what about repr with `error`s? schema = Schema([Or(None, And(str, Use(float)))]) repr_ = "Schema([Or(None, And(<type 'str'>, Use(<type 'float'>)))])" # in Python 3 repr contains <class 'str'>, not <type 'str'> assert repr(schema).replace('class', 'type') == repr_
from schema_async import Schema, And, Use, Optional import asyncio schema = Schema([{ 'name': And(str, len), 'age': And(Use(int), lambda n: 18 <= n <= 99), Optional('gender'): And(str, Use(str.lower), lambda s: s in ('squid', 'kid')) }]) data = [{ 'name': 'Sue', 'age': '28', 'gender': 'Squid' }, { 'name': 'Sam', 'age': '42' }, { 'name': 'Sacha', 'age': '20', 'gender': 'KID' }] loop = asyncio.get_event_loop() validated = loop.run_until_complete(schema.validate(data)) print(validated) assert validated == [{