From 828c1357f60f642e9aa7c0fb6f69567b5f321ee8 Mon Sep 17 00:00:00 2001 From: Hakan Calim <hakan.calim@fau.de> Date: Wed, 25 Oct 2023 21:52:11 +0200 Subject: [PATCH] NAT-328: add validator with regular expression for validate site name --- gso/workflows/site/create_site.py | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/gso/workflows/site/create_site.py b/gso/workflows/site/create_site.py index ac509adf..5335abdd 100644 --- a/gso/workflows/site/create_site.py +++ b/gso/workflows/site/create_site.py @@ -1,3 +1,4 @@ +import re from orchestrator.forms import FormPage from orchestrator.targets import Target from orchestrator.types import FormGenerator, State, SubscriptionLifecycle, UUIDstr @@ -42,10 +43,27 @@ def initial_input_form_generator(product_name: str) -> FormGenerator: # noqa: C validate_country_code(country_code) return country_code - @validator("site_name", "site_internal_id", "site_bgp_community_id", allow_reuse=True) + @validator("site_internal_id", "site_bgp_community_id", allow_reuse=True) def validate_unique_fields(cls, value: str, field: ModelField) -> str | int: return validate_site_fields_is_unique(field.name, value) + @validator("site_name", allow_reuse=True) + def site_name_must_be_valid(cls, site_name: str) -> str: + """Validate the site name. + + The site name must consist of three uppercase letters (A-Z) followed + by an optional single digit (0-9). + """ + validate_site_fields_is_unique("site_name", site_name) + pattern = re.compile(r"^[A-Z]{3}[0-9]?$") + if pattern.match(site_name): + return site_name + else: + raise ValueError( + "Enter a valid site name. It must consist of three uppercase letters (A-Z) followed by an optional" + " single digit (0-9)." + ) + user_input = yield CreateSiteForm return user_input.dict() -- GitLab