Skip to content

Commit

Permalink
Merge pull request #767 from eaguad1337/fix/autoload-validations
Browse files Browse the repository at this point in the history
Autoload validation classes from Validator
  • Loading branch information
josephmancuso authored Dec 6, 2023
2 parents b705c06 + 3726cf1 commit 0c473ba
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
6 changes: 6 additions & 0 deletions src/masonite/validation/Validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
15 changes: 14 additions & 1 deletion tests/features/validation/test_request_validation.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from tests import TestCase

from src.masonite.validation import Validator

class TestValidation(TestCase):
def test_can_validate_request(self):
Expand All @@ -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"))

0 comments on commit 0c473ba

Please sign in to comment.