The spec is all characters except white-space and capitalized characters.
Here is the regex validator on the model:
path = models.CharField(max_length=150, help_text="/blog/posts/...", unique=True, validators=[
RegexValidator(regex='(^[a-z0-9-:!@#$%^&*(){}\\?<>,.;\'"`~|/]+){1}', message="Lowercase with no whitespace allowed")
])
Here is the unit test:
def test_path_regex(self):
with self.assertRaises(ValidationError):
post = Post(title="bad regex", path="Super-2Test-2", slug="special")
if post.full_clean():
post.save()
with self.assertRaises(ValidationError):
post = Post(title="bad regex", path="super-2test 2", slug="special")
if post.full_clean():
post.save()
self.assertEqual(Post.objects.filter(title="bad regex").count(), 0)
Result:
line 27, in test_path_regex
post.save()
AssertionError: ValidationError not raised
FAILED (failures=1)
the reason for this seems to be that the RegexValidator is not flagging text if it can match part of the string and {1} is not helping. As you can see from this link: http://pythex.org/
Please help. Thanks.
The spec is all characters except white-space and capitalized characters.
You can simply use Negated Character Class. In your case it will be [^\sA-Z]. This will match all characters except whitespace and Upper Case alphabets