Skip to content

Commit

Permalink
Merge pull request #3997 from open-formulieren/refactor/3977-update-a…
Browse files Browse the repository at this point in the history
…ppointments-validation

Update appointments validation
  • Loading branch information
sergei-maertens authored Mar 12, 2024
2 parents 84cd243 + ba34b6f commit ef9be79
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 149 deletions.
16 changes: 10 additions & 6 deletions src/openforms/appointments/api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from drf_spectacular.utils import extend_schema_field
from rest_framework import serializers

from openforms.formio.service import validate_formio_data
from openforms.formio.service import build_serializer
from openforms.forms.models import Form
from openforms.submissions.api.fields import PrivacyPolicyAcceptedField
from openforms.submissions.models import Submission
Expand Down Expand Up @@ -274,12 +274,16 @@ def validate(self, attrs: dict) -> dict:
{"datetime": _("The selected datetime is not available (anymore).")}
)

# 5. Validate contact details against product
# 5. Validate contact details against product(s)
contact_details_meta = plugin.get_required_customer_fields(products)
try:
validate_formio_data(contact_details_meta, attrs["contact_details"])
except serializers.ValidationError as errors:
raise serializers.ValidationError({"contactDetails": errors.detail})
contact_details_serializer = build_serializer(
contact_details_meta,
data=attrs["contact_details"],
context=self.context,
)
if not contact_details_serializer.is_valid():
errors = contact_details_serializer.errors
raise serializers.ValidationError({"contact_details": errors})

# expose additional metadata
attrs.update(
Expand Down
8 changes: 5 additions & 3 deletions src/openforms/appointments/contrib/jcc/tests/test_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import requests_mock
from hypothesis import given, strategies as st

from openforms.formio.validation import build_validation_chain
from openforms.formio.service import build_serializer
from openforms.utils.tests.logging import disable_logging
from openforms.utils.xml import fromstring
from soap.tests.factories import SoapServiceFactory
Expand Down Expand Up @@ -696,13 +696,15 @@ def test_all_customer_fields_have_required_formio_properties(self):
self.assertIn("key", component)
self.assertIn("label", component)

def test_can_create_validation_chain_for_formio_fields(self):
def test_can_create_serializer_for_formio_fields(self):
for component in FIELD_TO_FORMIO_COMPONENT.values():
assert "key" in component
with self.subTest(component=component["key"]):
try:
build_validation_chain(component)
serializer = build_serializer([component])
except Exception as exc:
raise self.failureException(
"Could not create validation chain"
) from exc

self.assertIn(component["key"], serializer.fields)
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import requests_mock
from hypothesis import given, strategies as st

from openforms.formio.validation import build_validation_chain
from openforms.formio.service import build_serializer
from openforms.utils.date import TIMEZONE_AMS
from openforms.utils.tests.logging import disable_logging

Expand Down Expand Up @@ -609,13 +609,15 @@ def test_all_customer_fields_have_required_formio_properties(self):
self.assertIn("key", component)
self.assertIn("label", component)

def test_can_create_validation_chain_for_formio_fields(self):
def test_can_create_serializer_for_formio_fields(self):
for component in FIELD_TO_FORMIO_COMPONENT.values():
assert "key" in component
with self.subTest(component=component["key"]):
try:
build_validation_chain(component)
serializer = build_serializer([component])
except Exception as exc:
raise self.failureException(
"Could not create validation chain"
) from exc

self.assertIn(component["key"], serializer.fields)
10 changes: 6 additions & 4 deletions src/openforms/appointments/tests/test_api_appointment_create.py
Original file line number Diff line number Diff line change
Expand Up @@ -616,9 +616,10 @@ def test_invalid_contact_details(self):
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
invalid_params = response.json()["invalidParams"]
self.assertEqual(len(invalid_params), 2)
self.assertEqual(invalid_params[0]["name"], "contactDetails.0")
self.assertEqual(invalid_params[0]["name"], "contactDetails.lastName")
self.assertEqual(invalid_params[0]["code"], "required")
self.assertEqual(invalid_params[1]["name"], "contactDetails.1")

self.assertEqual(invalid_params[1]["name"], "contactDetails.email")
self.assertEqual(invalid_params[1]["code"], "required")

with self.subTest("value too long"):
Expand All @@ -634,8 +635,9 @@ def test_invalid_contact_details(self):

self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
invalid_params = response.json()["invalidParams"]

self.assertEqual(len(invalid_params), 2)
self.assertEqual(invalid_params[0]["name"], "contactDetails.0")
self.assertEqual(invalid_params[0]["name"], "contactDetails.lastName")
self.assertEqual(invalid_params[0]["code"], "max_length")
self.assertEqual(invalid_params[1]["name"], "contactDetails.1")
self.assertEqual(invalid_params[1]["name"], "contactDetails.email")
self.assertEqual(invalid_params[1]["code"], "max_length")
2 changes: 0 additions & 2 deletions src/openforms/formio/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
from .serializers import build_serializer as _build_serializer
from .typing import Component
from .utils import iter_components, iterate_data_with_components, recursive_apply
from .validation import validate_formio_data
from .variables import inject_variables

__all__ = [
Expand All @@ -41,7 +40,6 @@
"rewrite_formio_components_for_request",
"FormioData",
"iterate_data_with_components",
"validate_formio_data",
"recursive_apply",
"build_serializer",
]
Expand Down
131 changes: 0 additions & 131 deletions src/openforms/formio/validation.py

This file was deleted.

0 comments on commit ef9be79

Please sign in to comment.