From 3726cf1bc827977a72effa1c2e3decbef00fce1c Mon Sep 17 00:00:00 2001 From: Eduardo Aguad Date: Sun, 8 Oct 2023 00:38:31 -0300 Subject: [PATCH] autoload validations --- src/masonite/validation/Validator.py | 6 ++++++ .../validation/test_request_validation.py | 15 ++++++++++++++- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/masonite/validation/Validator.py b/src/masonite/validation/Validator.py index 465069bdb..4c0886b4b 100644 --- a/src/masonite/validation/Validator.py +++ b/src/masonite/validation/Validator.py @@ -1411,6 +1411,12 @@ def register(self, *cls): self.__dict__.update({obj.__name__: obj}) ValidationFactory().register(obj) + def __getattr__(self, name): + if name in ValidationFactory().registry: + return ValidationFactory().registry[name] + + raise AttributeError("Validator has no attribute {}".format(name)) + class ValidationFactory: diff --git a/tests/features/validation/test_request_validation.py b/tests/features/validation/test_request_validation.py index 54a6b36a8..b2ac36e69 100644 --- a/tests/features/validation/test_request_validation.py +++ b/tests/features/validation/test_request_validation.py @@ -1,5 +1,5 @@ from tests import TestCase - +from src.masonite.validation import Validator class TestValidation(TestCase): def test_can_validate_request(self): @@ -21,3 +21,16 @@ def test_can_validate_request_with_no_inputs(self): ) self.assertEqual(validation.all(), {"email": ["The email field is required."]}) + + def test_can_forward_validation_calls(self): + request = self.make_request(query_string="") + validate = Validator() + + errors = request.validate( + validate.required(['user', 'email']), + validate.accepted('terms') + ) + + 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"))