示例#1
0
    def test_list_validation_with_required_field_expect_error(self):
        rules = {"list.*.test": "required|max:2", "list.*.test2": "min:2"}
        data = {"list": [{}, {}, {}]}
        expected = {
            "list.0.test": [REQUIRED_ERROR.format(field="list.0.test")],
            "list.1.test": [REQUIRED_ERROR.format(field="list.1.test")],
            "list.2.test": [REQUIRED_ERROR.format(field="list.2.test")],
        }

        errors = self.validator.validate(data, rules)

        self.assertEqual(errors, expected)
示例#2
0
    def test_list_validation_required_expect_error(self):
        rules = {"list.*.test": "required|max:2", "list.*.test2": "min:2"}
        data = {
            "list": [
                {
                    "test": "12",
                    "test2": "1"
                },
                {
                    "test2": "1"
                },
                {
                    "test": "1234",
                    "test2": "1"
                },
            ]
        }
        expected = {
            "list.1.test": [REQUIRED_ERROR.format(field="list.1.test")],
            "list.2.test":
            [MAX_STRING_ERROR.format(field="list.2.test", max=2)],
            "list.0.test2":
            [MIN_STRING_ERROR.format(field="list.0.test2", min=2)],
            "list.1.test2":
            [MIN_STRING_ERROR.format(field="list.1.test2", min=2)],
            "list.2.test2":
            [MIN_STRING_ERROR.format(field="list.2.test2", min=2)],
        }

        errors = self.validator.validate(data, rules)

        self.assertEqual(errors, expected)
    def test_nested_validation_with_nested_required_field_expect_error(self):
        rules = {"nested.test": "required"}
        data = {"nested": {}}
        expected = {"nested.test": [REQUIRED_ERROR.format(field="nested.test")]}

        errors = self.validator.validate(data, rules)

        self.assertEqual(errors, expected)
示例#4
0
    def test_dict_rule_and_required_with_empty_dict_expect_error(self):
        rules = {"test": "required|dict"}
        data = {"test": {}}
        expected = REQUIRED_ERROR.format(field=self.field)

        errors = self.validator.validate(data, rules)
        errs = errors.get(self.field)

        self.assertEqual(errs[0], expected)
示例#5
0
 def setUp(self):
     self.field = "test"
     self.required_error = REQUIRED_ERROR.format(field=self.field)
     self.rules = {"test": "required"}