Skip to content

Commit

Permalink
[#3607] Feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
Viicos committed Jan 10, 2024
1 parent ffd9b9f commit f843ca2
Showing 1 changed file with 11 additions and 17 deletions.
28 changes: 11 additions & 17 deletions src/openforms/validations/validators/formats.py
Original file line number Diff line number Diff line change
@@ -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(
Expand All @@ -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 = _(
Expand All @@ -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 = _(
Expand All @@ -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)

0 comments on commit f843ca2

Please sign in to comment.