def test_nested_data_min(self): validation = Validator({ 'user': { 'name': 'john', 'age': 20, 'children': [ { 'name': 'jr1', 'age': 3 }, { 'name': 'jr2', 'age': 7 } ] }, }, { 'user': '******', 'user.name': 'required|min:4', 'user.age': 'required|min:18', 'user.children.*.name': 'required|max:10', 'user.children.*.age': 'required|max:10|min:2' }) self.assertTrue(validation.passes())
def test_ipv4(self): ips = [ '0.0.0.0', '9.255.255.255', '11.0.0.0', '126.255.255.255', '129.0.0.0', '169.253.255.255', '169.255.0.0', '172.15.255.255', '172.32.0.0', '191.0.1.255', '192.88.98.255', '192.88.100.0', '192.167.255.255', '192.169.0.0', '198.17.255.255', '223.255.255.255', '223.255.255.1', ] for ip in ips: v = Validator({'ip': ip}, {'ip': 'ipv4'}) self.assertTrue(v.passes())
def test_email_rule_fails(self): emails = [ 'Abc.example.com', 'A@b@[email protected]', 'a"b(c)d,e:f;g<h>i[j\k][email protected]', 'just"not"*****@*****.**', 'thisis"not\[email protected]', 'this\ still\"*****@*****.**', 'plainaddress', '#@%^%#$@#$@#.com', '@example.com', 'Joe Smith < *****@*****.** >', 'email.example.com', 'email@[email protected]', '*****@*****.**', '*****@*****.**', '*****@*****.**', 'email@example', '.333', '.44444', 'email @ example..com', 'Abc.', '.123 @ example.com', '[email protected]', 'あいうえお@example.com', '*****@*****.**', '[email protected] (Joe Smith)', ] for email in emails: v = Validator({'email': email}, {'email': 'required|email'}) self.assertTrue(v.fails())
def test_max(self): validation = Validator({ 'age': 9, 'height': 185 }, { 'age': 'max:100', 'height': 'max:250' }) self.assertTrue(validation.passes())
def test_min(self): validation = Validator({ 'age': 9, 'height': 185 }, { 'age': 'min:1', 'height': 'min:1' }) self.assertTrue(validation.passes())
def test_gt(self): validation = Validator({ 'age': 22, 'height': 185 }, { 'age': 'required', 'height': 'gt:age' }) self.assertTrue(validation.passes())
def test_lt(self): validation = Validator({ 'age': 29, 'height': 20 }, { 'age': 'required', 'height': 'lt:age' }) self.assertTrue(validation.passes())
def test_between(self): validation = Validator({ 'age': 11, 'bill': -3200 }, { 'age': 'between:10,100', 'bill': 'between:-3200,0' }) self.assertTrue(validation.passes())
def test_lte(self): validation = Validator({ 'age': 90, 'height': 90 }, { 'age': 'required', 'height': 'lte:height' }) self.assertTrue(validation.passes())
def test_max_fails(self): validation = Validator({ 'age': 9, 'height': 251 }, { 'age': 'max:100', 'height': 'max:250' }) self.assertTrue(validation.fails()) self.assertTrue('height' in validation.failed_rules)
def test_present_fails(self): v = Validator({ 'user': { } }, { 'user': '******', 'user.age': 'present' }) self.assertTrue(v.fails()) self.assertTrue('user.age' in v.failed_rules)
def test_present(self): v = Validator({ 'user': { 'age': 25 } }, { 'user': '******', 'user.age': 'present' }) self.assertTrue(v.passes())
def test_required_unless(self): v = Validator({ 'user': { 'age': 25 } }, { 'user': '******', 'user.name': 'required_unless:user.age,25' }) self.assertTrue(v.passes())
def test_required_without_all_fails_with_empty_data(self): v = Validator({ 'user': { } }, { 'user': '******', 'user.name': 'required_without_all:user.age,user.wife' }) self.assertTrue(v.fails()) self.assertTrue('user.name' in v.failed_rules)
def test_gt_fail(self): validation = Validator({ 'age': 17, 'height': 10 }, { 'age': 'required', 'height': 'gt:age' }) self.assertTrue(validation.fails()) self.assertTrue('height' in validation.failed_rules)
def test_integer(self): validation = Validator({ 'age': 10, 'height': 120 }, { 'age': 'integer', 'height': 'integer' }) self.assertTrue(validation.passes())
def test_ipv4_and_ipv6_ips_fails(self): ips = [ '1200:0000:AB00:1234:O000:2552:7777:1313', '[2001:db8:0:1]:80', 'http://[2001:db8:0:1]:80', '256.0.0.0' ] for ip in ips: v = Validator({'ip': ip}, {'ip': 'ip'}) self.assertTrue(v.fails())
def test_regex_fail(self): inv = ['AA45aa', '45aaa', 'AA@!5', 'aa@!5aaa'] for p in inv: v = Validator( {'password': p}, { 'password': '******', }) self.assertTrue(v.fails())
def test_min_fails(self): validation = Validator({ 'age': 2, 'height': -185 }, { 'age': 'min:1', 'height': 'min:1' }) self.assertTrue(validation.fails()) self.assertTrue('height' in validation.failed_rules)
def test_lte_fails(self): validation = Validator({ 'age': 184, 'height': 185 }, { 'age': 'required', 'height': 'lte:age' }) self.assertTrue(validation.fails()) self.assertTrue('height' in validation.failed_rules)
def test_required_if(self): v = Validator({ 'user': { 'name': 'John', 'age': 26, } }, { 'user': '******', 'user.name': 'required_if:user.age,26' }) self.assertTrue(v.passes())
def test_integer_fails(self): validation = Validator({ 'age': 10.5, 'height': 120 }, { 'age': 'integer', 'height': 'integer' }) self.assertTrue(validation.fails()) self.assertTrue('age' in validation.failed_rules)
def test_ipv6(self): ips = [ '1200:0000:AB00:1234:0000:2552:7777:1313', '21DA:D3:0:2F3B:2AA:FF:FE28:9C5A', 'FE80:0000:0000:0000:0202:B3FF:FE1E:8329' ] for ip in ips: v = Validator({'ip': ip}, {'ip': 'ipv6'}) self.assertTrue(v.passes())
def test_size(self): validation = Validator({ 'age': 10, 'bill': -6, 'count': 0 }, { 'age': 'size:10', 'bill': 'size:-6', 'count': 'size:0' }) self.assertTrue(validation.passes())
def test_between_must_fail(self): validation = Validator({ 'age': 9, 'bill': -3300 }, { 'age': 'between:10,100', 'bill': 'between:-3200,0' }) self.assertTrue(validation.fails()) self.assertTrue('age' in validation.failed_rules) self.assertTrue('bill' in validation.failed_rules)
def test_required_without_fails(self): v = Validator({ 'user': { 'age': None, } }, { 'user': '******', 'user.name': 'required_without:user.age' }) self.assertTrue(v.fails()) self.assertTrue('user.name' in v.failed_rules)
def test_size_must_fail(self): validation = Validator({ 'age': 5, 'bill': -7, 'count': 0 }, { 'age': 'size:10', 'bill': 'size:-6', 'count': 'size:0' }) self.assertTrue(validation.fails())
def test_required_unless_fails(self): v = Validator({ 'user': { 'age': 25 } }, { 'user': '******', 'user.name': 'required_unless:user.age:26,27' }) self.assertTrue(v.fails()) self.assertTrue('user.name' in v.failed_rules)
def test_gte(self): validation = Validator({ 'age': 10, 'height': 185, 'width': 185 }, { 'age': 'required', 'height': 'gte:age', 'width': 'gte:height', }) self.assertTrue(validation.passes())
def test_regex(self): passwords = ['AA!45a.aa', 'ABl49#kghjgdf', 'Akl9!!CB69*#kdf'] for p in passwords: # print(p) v = Validator( {'password': p}, { 'password': '******', }) self.assertTrue(v.passes())