-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🎨 [#1617] added custom field validators for api_connection_check_path
- Loading branch information
1 parent
4365497
commit 5df8ec5
Showing
7 changed files
with
163 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
from django.core.exceptions import ValidationError | ||
|
||
import pytest | ||
|
||
from zgw_consumers.models.validators import IsNotUrlValidator, StartWithValidator | ||
|
||
|
||
def test_start_with_validator_return_value_false(): | ||
validator = StartWithValidator(prefix="/", return_value=False) | ||
|
||
assert validator.__call__("no_leading_slash") is None | ||
|
||
with pytest.raises(ValidationError) as exc_context: | ||
validator.__call__("/with_leading_slash") | ||
|
||
assert "The given value cannot start with '/'" in exc_context.value | ||
|
||
|
||
def test_start_with_validator_return_value_true(): | ||
validator = StartWithValidator(prefix="/") | ||
|
||
with pytest.raises(ValidationError) as exc_context: | ||
validator.__call__("no_leading_slash") | ||
|
||
assert "The given value must start with '/'" in exc_context.value | ||
|
||
assert validator.__call__("/with_leading_slash") is None | ||
|
||
|
||
def test_is_not_url_validator(): | ||
validator = IsNotUrlValidator() | ||
|
||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
zgw_consumers/migrations/0021_service_api_connection_check_path.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# Generated by Django 4.2 on 2024-05-21 08:15 | ||
|
||
from django.db import migrations, models | ||
|
||
import zgw_consumers.models.validators | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
("zgw_consumers", "0020_service_timeout"), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name="service", | ||
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.", | ||
max_length=255, | ||
validators=[ | ||
zgw_consumers.models.validators.StartWithValidator( | ||
prefix="/", return_value=False | ||
), | ||
zgw_consumers.models.validators.IsNotUrlValidator(), | ||
], | ||
verbose_name="connection check endpoint", | ||
), | ||
), | ||
] |
23 changes: 0 additions & 23 deletions
23
zgw_consumers/migrations/0021_service_api_health_check_endpoint.py
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
from django.core.exceptions import ValidationError | ||
from django.core.validators import URLValidator | ||
from django.utils.deconstruct import deconstructible | ||
from django.utils.translation import gettext_lazy as _ | ||
|
||
|
||
@deconstructible | ||
class StartWithValidator: | ||
code = "invalid" | ||
|
||
def __init__( | ||
self, | ||
prefix: str, | ||
message: str = None, | ||
code: str = None, | ||
return_value: bool = True, | ||
): | ||
self.prefix = prefix | ||
self.return_value = return_value | ||
|
||
if code is not None: | ||
self.code = code | ||
|
||
if message is not None: | ||
self.message = message | ||
else: | ||
self.message = _( | ||
"The given value {must_or_cannot} start with '{prefix}'" | ||
).format( | ||
must_or_cannot="must" if self.return_value else "cannot", | ||
prefix=self.prefix, | ||
) | ||
|
||
def __call__(self, value: str) -> bool: | ||
if not value.startswith(self.prefix) == self.return_value: | ||
raise ValidationError(self.message, code=self.code, params={"value": value}) | ||
|
||
def __eq__(self, other): | ||
return ( | ||
isinstance(other, self.__class__) | ||
and self.prefix == other.prefix | ||
and (self.message == other.message) | ||
and (self.code == other.code) | ||
and (self.return_value == other.return_value) | ||
) | ||
|
||
|
||
@deconstructible | ||
class IsNotUrlValidator(URLValidator): | ||
message = _("String cannot be a URL") | ||
|
||
def __call__(self, value): | ||
try: | ||
super().__call__(value) | ||
except ValidationError: | ||
return | ||
|
||
raise ValidationError(self.message, code=self.code, params={"value": value}) |