-
-
Notifications
You must be signed in to change notification settings - Fork 127
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #764 from eaguad1337/feat/custom-validator-messages
feat: custom validation messages
- Loading branch information
Showing
4 changed files
with
91 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
from tests import TestCase | ||
from masonite.validation import email, isnt, is_in, numeric | ||
from src.masonite.validation import Validator | ||
|
||
|
||
class TestValidation(TestCase): | ||
def test_can_validate_request(self): | ||
request = self.make_request(query_string="[email protected]") | ||
|
@@ -22,6 +24,55 @@ def test_can_validate_request_with_no_inputs(self): | |
|
||
self.assertEqual(validation.all(), {"email": ["The email field is required."]}) | ||
|
||
def test_custom_messages(self): | ||
request = self.make_request(post_data={"not_email": "[email protected]", "age": 20}) | ||
# validator = Validator() | ||
validate = request.validate( | ||
isnt(is_in("age", [20, 21]), numeric("age")), | ||
isnt(email("not_email")), | ||
{ | ||
"username": "required", | ||
"valid_email": "required|email", | ||
"secondary_email": "required|email", | ||
}, | ||
messages={ | ||
"username.required": "Custom Message for required Username.", | ||
"not_email.isnt_email": "Custom Message for Email not being an email.", | ||
"valid_email.required": "Custom Message for required Email.", | ||
"valid_email.email": "Custom Message for Email being a valid email.", | ||
"age.isnt_is_in.isnt_numeric": "Custom: Age must not be in 20, 21 and must not be numeric.", | ||
}, | ||
) | ||
|
||
self.assertIn( | ||
"Custom: Age must not be in 20, 21 and must not be numeric.", | ||
validate.get("age"), | ||
) | ||
self.assertIn( | ||
"Custom Message for required Email.", | ||
validate.get("valid_email"), | ||
) | ||
self.assertIn( | ||
"Custom Message for Email being a valid email.", | ||
validate.get("valid_email"), | ||
) | ||
self.assertIn( | ||
"Custom Message for Email not being an email.", | ||
validate.get("not_email"), | ||
) | ||
self.assertIn( | ||
"Custom Message for required Username.", | ||
validate.get("username"), | ||
) | ||
self.assertIn( | ||
"The secondary_email field is required.", | ||
validate.get("secondary_email"), | ||
) | ||
self.assertIn( | ||
"The secondary_email must be a valid email address.", | ||
validate.get("secondary_email"), | ||
) | ||
|
||
def test_can_forward_validation_calls(self): | ||
request = self.make_request(query_string="") | ||
validate = Validator() | ||
|
@@ -34,3 +85,4 @@ def test_can_forward_validation_calls(self): | |
self.assertIn("The user field is required.", errors.get("user")) | ||
self.assertIn("The email field is required.", errors.get("email")) | ||
self.assertIn("The terms must be accepted.", errors.get("terms")) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters