def test_optional_str(): validator = Validator(Optional[str]) validator.is_valid(None) validator.is_valid("asd") validator.is_valid([1]) assert validator.repr_errors() == ["[[1]]"] validator = Validator({ 'name': str, 'age': int, 'selected_skill': Optional[str] }) assert validator.is_valid({ 'name': 'Georgy', 'age': 29, 'selected_skill': 'Python' }), validator.repr_errors() assert validator.is_valid({ 'name': 'Georgy', 'age': 29, 'selected_skill': None }), validator.repr_errors() validator.is_valid({'name': 'Georgy', 'age': 29, 'selected_skill': 1}) assert validator.repr_errors() == ["{'selected_skill'}->int(1)"]
def test_union_str(): validator = Validator(Union[int, str]) assert validator.is_valid(1), validator.repr_errors() assert validator.is_valid("1"), validator.repr_errors() validator.is_valid([1]) assert validator.repr_errors() == ['[[1]]']
def test_non_iterable(): validator = Validator({'name': str, 'age': int, 'skills': [str]}) assert not validator.is_valid({ 'name': 'Georgy', 'age': 29, 'skills': None }) assert not validator.is_valid(None)
def test_object(): validator = Validator({"users": [User]}) assert validator.is_valid(DATA_SAMPLE) assert not validator.is_valid(WRONG_DATA_SAMPLE) assert set(validator.repr_errors()) == { "{'users'}->[0]->{'id'}->str('1')", "{'users'}->[1]->{'name'}", "{'users'}->[0]->{'email'}->str('hello')", }
def test_union_with_custom_validation(): validator = Validator( {'format': Union[OperationsFileType, List[OperationsFileType]]}) assert validator.is_valid({'format': 'xml'}) assert validator.is_valid({'format': ['xml']}) validator.is_valid({'format': ['xlm']}) assert set(validator.repr_errors()) == { "{'format'}->[0]->str('xlm')", "{'format'}->[['xlm']]", }
def test_optional_list(): validator = Validator({'selected_skills': Optional[List[str]]}) assert validator.is_valid({'selected_skills': ['Python']}), validator.repr_errors() assert validator.is_valid({'selected_skills': None}), validator.repr_errors() validator.is_valid({'selected_skills': 'Python'}) assert validator.repr_errors() == ["{'selected_skills'}->str('Python')"]
def test_readme_example(): validator = Validator({'name': str, 'age': int, 'skills': [str]}) assert validator.is_valid({ 'name': 'Georgy', 'age': 29, 'skills': ['Python', 'Perl', 'C'] })
def test_simple_match(): validator = Validator(re.compile(r'^https?://')) assert validator.is_valid('https://900913.ru') assert Validator(re.compile(r'^https?://')).is_valid('http://google.com') assert not Validator( re.compile(r'^https?://')).is_valid(' http://google.com') assert not Validator(re.compile(r'^https?://')).is_valid('900913.ru')
def test_repr_one(): validator = Validator(str) validator.is_valid("asd") validator.is_valid([1]) assert validator.repr_errors() == ["[[1]]"] validator = validate({ 'name': str, 'age': int, 'skills': [str] }, { 'name': 'Georgy', 'age': None, 'skills': ['Python', 'Perl', 'C'] }) assert validator.repr_errors() == ["{'age'}->NoneType(None)"]
def test_dict(): validator = Validator(Dict[str, int]) assert validator.is_valid({"a": 1}), validator.repr_errors() assert validator.is_valid({}), validator.repr_errors() validator.is_valid({1: 1}) assert validator.repr_errors() == ['{1}'] validator.is_valid({"a": "a"}) assert validator.repr_errors() == ["{'a'}->str('a')"] validator.is_valid(1) assert validator.repr_errors() == ["int(1)"]
def test_composite(): validator = Validator({ 'name': str, 'email': re.compile(r'^[\w_\-.+]+@[\w\-.+]+.\w+'), 'blog': re.compile(r'^https?://[\w\-.+]+.\w+'), }) assert validator.is_valid({ 'name': 'Georgy', 'email': '*****@*****.**', 'blog': 'https://900913.ru' }) assert not validator.is_valid({ 'name': 'Georgy', 'email': 'bugov[at]cpan.org', 'blog': '900913.ru' }) assert set(validator.repr_errors()) == { "{'email'}->str('bugov[at]cpan.org')", "{'blog'}->str('900913.ru')", } validator = Validator( {'media': { 'web': [re.compile(r'^https?://[\w\-.+]+.\w+')] }}) assert validator.is_valid({'media': {'web': ['https://900913.ru']}}) assert not validator.is_valid( {'media': { 'web': ['https://900913.ru', '900913.net'] }}) assert validator.repr_errors() == [ "{'media'}->{'web'}->[1]->str('900913.net')" ]
def test_valid(): validator = Validator({ "hero": { "name": str, "friends": [{ "name": str, "appearsIn": [str], "friends": [{ "name": str }] }] } }) assert validator.is_valid(DATA_SAMPLE)
def test_list(): validator = Validator(List[str]) assert validator.is_valid(["asd"]), validator.repr_errors() assert validator.is_valid([]), validator.repr_errors() validator.is_valid([1]) assert validator.repr_errors() == ["[0]->int(1)"] validator = Validator(List[List[str]]) assert validator.is_valid([["zxc"], ["qwe"], ["asd"], []]), validator.repr_errors() validator = Validator(List[List[Optional[str]]]) assert validator.is_valid([[None], ["qwe"], ["asd"], []]), validator.repr_errors()
def test_none(): validator = Validator(None) assert validator.is_valid(None) assert not validator.is_valid('')
def test_dict(): validator = Validator(dict) assert validator.is_valid({'year': 2019}) assert not validator.is_valid([1, 2, 3])
def test_list(): validator = Validator(list) assert validator.is_valid([1, 2, 3]) assert not validator.is_valid('qwe')
def test_str(): validator = Validator(str) assert validator.is_valid('hello') assert not validator.is_valid(1)
def test_bool(): validator = Validator(bool) assert validator.is_valid(True) assert not validator.is_valid(None)
def test_float(): validator = Validator(float) assert validator.is_valid(1.123) assert not validator.is_valid(True)
def test_int(): validator = Validator(int) assert validator.is_valid(1) assert not validator.is_valid('1')
def test_union_complex(): validator = Validator({'value': Union[Optional[int], List[str]]}) assert validator.is_valid({'value': None}), validator.repr_errors() assert validator.is_valid({'value': 1}), validator.repr_errors() assert validator.is_valid({'value': ['1']}), validator.repr_errors()