From f843ca2246cc1c1d38902b3906ca42160131af5f Mon Sep 17 00:00:00 2001 From: Viicos <65306057+Viicos@users.noreply.github.com> Date: Wed, 10 Jan 2024 16:29:14 +0100 Subject: [PATCH] [#3607] Feedback --- .../validations/validators/formats.py | 28 ++++++++----------- 1 file changed, 11 insertions(+), 17 deletions(-) diff --git a/src/openforms/validations/validators/formats.py b/src/openforms/validations/validators/formats.py index be40fe7f2b..c7b66a0995 100644 --- a/src/openforms/validations/validators/formats.py +++ b/src/openforms/validations/validators/formats.py @@ -1,31 +1,19 @@ -from __future__ import annotations - -from abc import ABC, abstractmethod -from typing import TYPE_CHECKING - from django.core.exceptions import ValidationError from django.utils.translation import gettext_lazy as _ import phonenumbers -from phonenumbers import NumberParseException +from phonenumbers import NumberParseException, PhoneNumber from openforms.validations.base import BasePlugin from openforms.validations.registry import register -if TYPE_CHECKING: - from phonenumbers.phonenumber import PhoneNumber - -class PhoneNumberBaseValidator(ABC): +class PhoneNumberBaseValidatorMixin: country: str | None country_name: str = "" error_message = _("Not a valid %(country)s phone number") - @abstractmethod - def _parse_phonenumber(self, value: str) -> PhoneNumber: - pass - - def __call__(self, value: str, submission): + def validate(self, value: str): z = self._parse_phonenumber(value) if not phonenumbers.is_possible_number(z) or not phonenumbers.is_valid_number( @@ -50,7 +38,7 @@ def __call__(self, value: str, submission): @register("phonenumber-international") -class InternationalPhoneNumberValidator(PhoneNumberBaseValidator, BasePlugin[str]): +class InternationalPhoneNumberValidator(PhoneNumberBaseValidatorMixin, BasePlugin[str]): country = None country_name = _("international") error_message = _( @@ -74,9 +62,12 @@ def _parse_phonenumber(self, value: str) -> PhoneNumber: code="invalid", ) + def __call__(self, value: str, submission): + self.validate(value) + @register("phonenumber-nl") -class DutchPhoneNumberValidator(PhoneNumberBaseValidator, BasePlugin[str]): +class DutchPhoneNumberValidator(PhoneNumberBaseValidatorMixin, BasePlugin[str]): country = "NL" country_name = _("Dutch") error_message = _( @@ -94,3 +85,6 @@ def _parse_phonenumber(self, value: str) -> PhoneNumber: self.error_message, code="invalid", ) + + def __call__(self, value: str, submission): + self.validate(value)