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_between(self):
        validation = Validator({
            'age': 11,
            'bill': -3200
        }, {
            'age': 'between:10,100',
            'bill': 'between:-3200,0'
        })

        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_min(self):
        validation = Validator({
            'age': 9,
            'height': 185
        }, {
            'age': 'min:1',
            'height': 'min:1'
        })

        self.assertTrue(validation.passes())
    def test_max(self):
        validation = Validator({
            'age': 9,
            'height': 185
        }, {
            'age': 'max:100',
            'height': 'max:250'
        })

        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_lte(self):
        validation = Validator({
            'age': 90,
            'height': 90
        }, {
            'age': 'required',
            'height': 'lte:height'
        })

        self.assertTrue(validation.passes())
    def test_integer(self):
        validation = Validator({
            'age': 10,
            'height': 120
        },
        {
            'age': 'integer',
            'height': 'integer'
        })

        self.assertTrue(validation.passes())
    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_if(self):
        v = Validator({
            'user': {
                'name': 'John',
                'age': 26,
            }
        }, {
            'user': '******',
            'user.name': 'required_if:user.age,26'
        })

        self.assertTrue(v.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())
    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_gte(self):
        validation = Validator({
            'age': 10,
            'height': 185,
            'width': 185
        }, {
            'age': 'required',
            'height': 'gte:age',
            'width': 'gte:height',
        })

        self.assertTrue(validation.passes())
    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_required_with_fails(self):
        v = Validator({
            'user': {
                'age': 20,
            }
        }, {
            'user': '******',
            'user.name': 'required_with:user.age',
            'user.wife': 'required_with:user.age'
        })

        self.assertTrue(not v.passes())
        self.assertTrue('user.name' in v.failed_rules)
        self.assertTrue('user.wife' in v.failed_rules)
    def test_required_with_all_pass_without_all_params(self):
        v = Validator({
            'user': {
                'wife': {
                    'name': 'Anna',
                    'age': 20
                }
            }
        }, {
            'user': '******',
            'user.name': 'required_with_all:user.age,user.wife'
        })

        self.assertTrue(v.passes())
    def test_multiple_rules(self):
        v = Validator(
            {
                'name': 'john',
                'email': '*****@*****.**',
                'birth_date': '1985-08-25 15:12:02',
                'height': 190
            }, {
                'name': 'required|string|min:3|max:16',
                'email': 'required|string|email|min:3|max:32',
                'birth_date': 'required|date',
                'height': 'required|integer|max:250'
            })

        self.assertTrue(v.passes())
    def test_required_if_fails(self):
        v = Validator({
            'user': {
                'age': 26,
            }
        }, {
            'user': '******',
            'user.name': 'required_if:user.age,26',
            'user.wife': 'required_if:user.age,26,30,69',
            'user.not_required': 'required_if:user.age,30,69',
        })

        self.assertTrue(not v.passes())
        self.assertTrue('user.name' in v.failed_rules)
        self.assertTrue('user.wife' in v.failed_rules)
        self.assertTrue('user.not_required' not in v.failed_rules)
    def test_rule_list_params(self):
        v = Validator(
            {
                'name': 'john',
                'email': '*****@*****.**',
                'birth_date': '1985-08-25 15:12:02',
                'height': 190
            }, {
                'name': ['required', 'string', ['min', 3], ['max', 16]],
                'email':
                ['required', 'string', 'email', ['min', 3], ['max', 32]],
                'birth_date': ['required', 'date'],
                'height': ['required', 'integer', ['max', 250]]
            })

        self.assertTrue(v.passes())
    def test_required_with(self):
        v = Validator({
            'user': {
                'name': 'John',
                'age': 26,
                'wife': {
                    'name': 'Anna',
                    'age': 20
                }
            }
        }, {
            'user': '******',
            'user.name': 'required_with:user.age,user.wife'
        })

        self.assertTrue(v.passes())
    def test_valid_urls(self):
        urls = [
            'http://foo.com/blah_blah', 'http://foo.com/blah_blah_(wikipedia)', 'http://142.42.1.1/',
            'http://foo.com/blah_(wikipedia)_blah#cite-1', 'ftp://foo.bar/baz'
        ]

        for url in urls:
            v = Validator(
                {
                    'url': url
                },
                {
                    'url': 'url'
                }
            )

            self.assertTrue(v.passes())
    def test_ipv4_and_ipv6_ips(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',
            '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': 'ip'})

            self.assertTrue(v.passes())
    def test_numeric(self):
        validation = Validator({
            'int': 10,
            'float': 0.5,
            'decimal': decimal.Decimal(3),
            'fraction': fractions.Fraction(2, 1),
            'complex': 2 + 4j,
            'stringInteger': '4',
            'stringFloat': '5.9'
        },
        {
            'int': 'numeric',
            'float': 'numeric',
            'decimal': 'numeric',
            'fraction': 'numeric',
            'complex': 'numeric',
            'stringInteger': 'numeric',
        })

        self.assertTrue(validation.passes())
    def test_email_rule(self):

        emails = [
            '*****@*****.**',
            '*****@*****.**',
            '*****@*****.**',
            '*****@*****.**',
            '*****@*****.**',
            '*****@*****.**',
            '*****@*****.**',
            '*****@*****.**',
            '*****@*****.**',
            '*****@*****.**',
            '*****@*****.**',
            '*****@*****.**',
            '*****@*****.**',
            'email@[123.123.123.123]',
            '"email"@example.com',
        ]

        for email in emails:
            v = Validator({'email': email}, {'email': 'required|email'})
            self.assertTrue(v.passes())
Beispiel #27
0
from pyva import Validator

data = {'users': [{'name': 'John', 'age': 28}, {'name': 'David', 'age': 19}]}

rules = {
    'users': 'required|list',
    'users.*': 'dict',
    'users.*.name': 'required|min:3',
    'users.*.age': 'required|min:18|max:100',
}

validation = Validator(data, rules)
if validation.passes():
    print(validation.validated())  # get validated data
    # do something cool
Beispiel #28
0

class DivisibleBy(RuleContract):

    def __init__(self, divide_to):
        self.divide_to = divide_to

    def passes(self, attribute, value):
        if self.divide_to == 0:
            return False

        return value % self.divide_to == 0

    def message(self, attribute, value):
        return "{} is not divisible by {}".format(value, self.divide_to)


v = Validator(
    {
        'someNumber': 9
    },
    {
        'someNumber': [DivisibleBy(4)]
    }
)

if v.passes():
    print(v.validated())
else:
    print(v.failed_rules)  # prints {'someNumber': ['9 is not divisible to 4']}