Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_custom_error_messages_missing_one(self):
email_validator = Validator()
email_validator.validate({'id': [Required]})
email_validator.messages(
{'username': 'dont forget your username'})
assert email_validator.check({'username': '5'}) is False
self.assertEqual(email_validator.errors()['id'], 'must be present')
def test_validator_sets_request(self):
validator = Validator(self.request)
self.assertIsInstance(validator.request, self.request)
def test_validator_errors_returns_false(self):
email_validator = Validator()
email_validator.validate({'id': [Required, Pattern(r'\d+')]})
assert email_validator.check({'id': '4'}) is True
assert email_validator.errors() is None
def test_validator_errors_returns_false():
email_validator = Validator()
email_validator.validate({'id': [Required, Pattern(r'\d+')]})
assert email_validator.check({'id': '4'}) is True
assert email_validator.errors() is None
def test_validator_error():
email_validator = Validator(REQUEST)
email_validator.validate({'username': [Required]})
assert email_validator.check() is False
assert email_validator.errors()['username'] == 'must be present'
def test_validator_length(self):
email_validator = Validator()
email_validator.validate({'id': [Length(0, maximum=2)]})
assert email_validator.check({'id': [1, 2, 3, 4, 5]}) is False
def test_validator_check_matches_without_request(self):
email_validator = Validator()
email_validator.validate({'id': [Required, Pattern(r'\d+')]})
assert email_validator.check({'id': '4'}) is True
def test_validator_check_does_not_match():
email_validator = Validator(REQUEST)
email_validator.validate({'id': [Required, Pattern('[a-zA-Z]')]})
assert email_validator.check() is False
def test_validator_length():
email_validator = Validator()
email_validator.validate({'id': [Length(0, maximum=2)]})
assert email_validator.check({'id': [1,2,3,4,5]}) is False
def validate(validations, data, messages={}):
"""Helper function for shorthand validations
Arguments:
validations {dict} -- A dictionary of validations from the validator.py library
data {dict} -- The data to be validated
Keyword Arguments:
messages {dict} -- Optional messages to render if there was a bad validation. (default: {{}})
Returns:
Bool|dict -- Returns True if validations are good or a dictionary if there are errors.
"""
validator = Validator().validate(validations)
validator.error_messages = messages
if validator.check(data):
return True
else:
return validator.errors()