Skip to content

Commit

Permalink
[maykinmedia/objects-api#481] Add validate_no_empty + tests
Browse files Browse the repository at this point in the history
  • Loading branch information
danielmursa-dev committed Dec 13, 2024
1 parent 18ec10e commit d6df41a
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,10 @@ class Migration(migrations.Migration):
field=models.CharField(
max_length=40,
unique=True,
validators=[objecttypes.token.validators.validate_no_whitespace],
validators=[
objecttypes.token.validators.validate_no_empty,
objecttypes.token.validators.validate_no_whitespace,
],
verbose_name="token",
),
),
Expand Down
4 changes: 2 additions & 2 deletions src/objecttypes/token/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from django.db import models
from django.utils.translation import gettext_lazy as _

from objecttypes.token.validators import validate_no_whitespace
from objecttypes.token.validators import validate_no_empty, validate_no_whitespace


class TokenAuth(models.Model):
Expand All @@ -16,7 +16,7 @@ class TokenAuth(models.Model):
_("token"),
max_length=40,
unique=True,
validators=[validate_no_whitespace],
validators=[validate_no_empty, validate_no_whitespace],
)

contact_person = models.CharField(
Expand Down
15 changes: 14 additions & 1 deletion src/objecttypes/token/tests/test_validators.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
from django.core.exceptions import ValidationError
from django.test import SimpleTestCase

from objecttypes.token.validators import validate_no_whitespace
from objecttypes.token.validators import validate_no_empty, validate_no_whitespace


class NoEmptyValidatorTestCase(SimpleTestCase):
def test_valid(self):
self.assertIsNone(validate_no_empty("test123"))

def test_invalid_string(self):
with self.assertRaises(ValidationError):
validate_no_empty("")

def test_invalid_none(self):
with self.assertRaises(ValidationError):
validate_no_empty(None)


class WhiteSpaceValidatorTestCase(SimpleTestCase):
Expand Down
5 changes: 5 additions & 0 deletions src/objecttypes/token/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,8 @@ def validate_no_whitespace(value: str) -> None:
code="all-whitespace",
message=_("Tokens cannot contain whitespace-like characters"),
)


def validate_no_empty(value: str) -> None:
if not value:
raise ValidationError(code="invalid", message=_("Blank values are not allowed"))

0 comments on commit d6df41a

Please sign in to comment.