def test_double_nested_validation_custom_message_expect_error(self): rules = { "nested.test": "max:5|email", "nested.double_nested.test2": "max:2", "non_nested": "max:1", } data = { "nested": { "test": "123456", "double_nested": { "test2": "12222" } }, "non_nested": "12", } new_message = "Hey! The {field} field has to be at least {max} chars!" expected = { "nested.test": [ MAX_STRING_ERROR.format(field="nested.test", max=5), EMAIL_ERROR.format(field="nested.test"), ], "nested.double_nested.test2": [new_message.format(field="nested.double_nested.test2", max=2)], "non_nested": [MAX_STRING_ERROR.format(field="non_nested", max=1)], } self.validator.overwrite_messages = { "nested.double_nested.test2.max": new_message } errors = self.validator.validate(data, rules) self.assertEqual(errors, expected)
def test_double_nested_validation_custom_field_expect_error(self): rules = { "nested.test": "max:5|email", "nested.double_nested.test2": "max:2", "non_nested": "max:1", } data = { "nested": { "test": "123456", "double_nested": { "test2": "12222" } }, "non_nested": "12", } expected = { "nested.double_nested.test2": [MAX_STRING_ERROR.format(field="custom", max=2)], "nested.test": [ MAX_STRING_ERROR.format(field="nested.test", max=5), EMAIL_ERROR.format(field="nested.test"), ], "non_nested": [MAX_STRING_ERROR.format(field="non_nested", max=1)], } self.validator.overwrite_fields = { "nested.double_nested.test2": "custom" } errors = self.validator.validate(data, rules) self.assertEqual(errors, expected)
def test_double_nested_validation_with_flat_expect_error_list(self): rules = { "nested.test": "max:5|email", "nested.double_nested.test2": "in:val1,val2,val3", "non_nested": "max:1", } data = { "nested": { "test": "123456", "double_nested": { "test2": "12222" } }, "non_nested": "12", } new_values = "new1, new2, new3" expected = [ MAX_STRING_ERROR.format(field="nested.test", max=5), EMAIL_ERROR.format(field="nested.test"), IN_ERROR.format(field="nested.double_nested.test2", values=new_values), MAX_STRING_ERROR.format(field="non_nested", max=1), ] self.validator.overwrite_values = { "nested.double_nested.test2": { "val1": "new1", "val2": "new2", "val3": "new3", } } errors = self.validator.validate(data, rules, flat=True) self.assertEqual(errors, expected)
def test_combo_with_invalid_email_and_min_expect_error(self): field = "field1" rules = {"field1": "filled|email|min:5|max:255"} data = {"field1": "oops"} errors = self.validator.validate(data, rules) errs = errors.get(field) self.assertEqual(errs[0], EMAIL_ERROR.format(field=field)) self.assertEqual(errs[1], MIN_STRING_ERROR.format(field=field, min=5))
def test_object_input_with_one_rule_expect_error(self): field = "email" rules = {"email": "required|email"} data = CustomData(email="john.doe@") expected = EMAIL_ERROR.format(field=field) errors = self.validator.validate(data, rules) errs = errors.get(field) self.assertEqual(errs[0], expected)
def test_nested_object_input_with_one_rule_expect_error(self): field = "data.email" rules = {"data.email": "required|email"} data = CustomData(email="john.doe@") parent_data = NestedData(data) expected = {field: [EMAIL_ERROR.format(field=field)]} errors = self.validator.validate(parent_data, rules) self.assertEqual(errors, expected)
def test_nested_validation_with_nested_field_expect_error(self): rules = {"nested.test": "max:5|email"} data = {"nested": {"test": "123456"}} expected = { "nested.test": [ MAX_STRING_ERROR.format(field="nested.test", max=5), EMAIL_ERROR.format(field="nested.test"), ] } errors = self.validator.validate(data, rules) self.assertEqual(errors, expected)
def setUp(self): self.field = "email" self.email_error = EMAIL_ERROR.format(field=self.field)