Skip to content

Commit

Permalink
🎨 [#1617] changed PrefixValidator to StartWithValidator
Browse files Browse the repository at this point in the history
  • Loading branch information
bart-maykin committed Jun 11, 2024
1 parent 5df8ec5 commit 9bfd0e8
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 24 deletions.
14 changes: 7 additions & 7 deletions tests/test_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

import pytest

from zgw_consumers.models.validators import IsNotUrlValidator, StartWithValidator
from zgw_consumers.models.validators import NonUrlValidator, PrefixValidator


def test_start_with_validator_return_value_false():
validator = StartWithValidator(prefix="/", return_value=False)
def test_start_with_validator_starts_with_false():
validator = PrefixValidator(prefix="/", starts_with=False)

assert validator.__call__("no_leading_slash") is None

Expand All @@ -16,8 +16,8 @@ def test_start_with_validator_return_value_false():
assert "The given value cannot start with '/'" in exc_context.value


def test_start_with_validator_return_value_true():
validator = StartWithValidator(prefix="/")
def test_start_with_validator_starts_with_true():
validator = PrefixValidator(prefix="/")

with pytest.raises(ValidationError) as exc_context:
validator.__call__("no_leading_slash")
Expand All @@ -28,11 +28,11 @@ def test_start_with_validator_return_value_true():


def test_is_not_url_validator():
validator = IsNotUrlValidator()
validator = NonUrlValidator()

assert validator.__call__("some random text") is None

with pytest.raises(ValidationError) as exc_context:
assert validator.__call__("http://www.example.com")

assert "String cannot be a URL" in exc_context.value
assert "Value cannot be a URL" in exc_context.value
3 changes: 2 additions & 1 deletion zgw_consumers/admin.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from django.contrib import admin
from django.db import models
from django.http import HttpRequest
from django.utils.translation import gettext_lazy as _

from privates.admin import PrivateMediaMixin
from solo.admin import SingletonModelAdmin
Expand All @@ -18,7 +19,7 @@ class ServiceAdmin(admin.ModelAdmin):
search_fields = ("label", "api_root", "nlx", "uuid")
readonly_fields = ("get_connection_check",)

@admin.display(description="Connection Check")
@admin.display(description=_("Connection check status code"))
def get_connection_check(self, obj):
return obj.connection_check

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ class Migration(migrations.Migration):
name="api_connection_check_path",
field=models.CharField(
blank=True,
help_text="An optional API endpoint which will be used to check if the API is configured correctly and is currently up or down. This field is only used for in the admin's 'Connection check' field.",
help_text="An optional API endpoint which will be used to check if the API is configured correctly and is currently up or down. This field is only used for the admin's 'Connection check status code' field.",
max_length=255,
validators=[
zgw_consumers.models.validators.StartWithValidator(
prefix="/", return_value=False
zgw_consumers.models.validators.PrefixValidator(
prefix="/", starts_with=False
),
zgw_consumers.models.validators.IsNotUrlValidator(),
zgw_consumers.models.validators.NonUrlValidator(),
],
verbose_name="connection check endpoint",
),
Expand Down
8 changes: 4 additions & 4 deletions zgw_consumers/models/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

from ..constants import APITypes, AuthTypes, NLXDirectories
from .abstract import RestAPIService
from .validators import IsNotUrlValidator, StartWithValidator
from .validators import NonUrlValidator, PrefixValidator

logger = logging.getLogger(__name__)

Expand All @@ -37,12 +37,12 @@ class Service(RestAPIService):
_("connection check endpoint"),
help_text=_(
"An optional API endpoint which will be used to check if the API is configured correctly and "
"is currently up or down. This field is only used for in the admin's 'Connection check' field."
"is currently up or down. This field is only used for the admin's 'Connection check status code' field."
),
max_length=255,
validators=[
StartWithValidator(prefix="/", return_value=False),
IsNotUrlValidator(),
PrefixValidator(prefix="/", starts_with=False),
NonUrlValidator(),
],
blank=True,
)
Expand Down
16 changes: 8 additions & 8 deletions zgw_consumers/models/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@


@deconstructible
class StartWithValidator:
class PrefixValidator:
code = "invalid"

def __init__(
self,
prefix: str,
message: str = None,
code: str = None,
return_value: bool = True,
starts_with: bool = True,
):
self.prefix = prefix
self.return_value = return_value
self.starts_with = starts_with

if code is not None:
self.code = code
Expand All @@ -27,12 +27,12 @@ def __init__(
self.message = _(
"The given value {must_or_cannot} start with '{prefix}'"
).format(
must_or_cannot="must" if self.return_value else "cannot",
must_or_cannot="must" if self.starts_with else "cannot",
prefix=self.prefix,
)

def __call__(self, value: str) -> bool:
if not value.startswith(self.prefix) == self.return_value:
if value.startswith(self.prefix) != self.starts_with:
raise ValidationError(self.message, code=self.code, params={"value": value})

def __eq__(self, other):
Expand All @@ -41,13 +41,13 @@ def __eq__(self, other):
and self.prefix == other.prefix
and (self.message == other.message)
and (self.code == other.code)
and (self.return_value == other.return_value)
and (self.starts_with == other.starts_with)
)


@deconstructible
class IsNotUrlValidator(URLValidator):
message = _("String cannot be a URL")
class NonUrlValidator(URLValidator):
message = _("Value cannot be a URL")

def __call__(self, value):
try:
Expand Down

0 comments on commit 9bfd0e8

Please sign in to comment.