Skip to content

Commit

Permalink
valid_subject_identifier_or_raise
Browse files Browse the repository at this point in the history
  • Loading branch information
erikvw committed Dec 18, 2023
1 parent ff3da5b commit a238577
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion edc_registration/utils.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
from __future__ import annotations

import re
from typing import TYPE_CHECKING

from django.apps import apps as django_apps
from django.conf import settings
from django.core.exceptions import ObjectDoesNotExist
from edc_protocol import Protocol

if TYPE_CHECKING:
from edc_registration.models import RegisteredSubject
from edc_registration.models import RegisteredSubject, RegisteredSubjectError


class RegisteredSubjectDoesNotExist(Exception):
Expand Down Expand Up @@ -41,3 +43,19 @@ def get_registered_subject(
f"Got {opts}."
)
return registered_subject


def valid_subject_identifier_or_raise(
subject_identifier: str, raise_exception: bool | None = None
) -> bool:
if not re.match(Protocol().subject_identifier_pattern, subject_identifier):
if raise_exception:
raise RegisteredSubjectError(
f"Invalid subject identifier format. "
f"Valid pattern is `{Protocol().subject_identifier_pattern}`. "
f"See `edc_protocol.Protocol().subject_identifier_pattern`. "
f"Got `{subject_identifier}`."
)
else:
return False
return True

0 comments on commit a238577

Please sign in to comment.