diff --git a/gso/workflows/vrf/modify_vrf_router_list.py b/gso/workflows/vrf/modify_vrf_router_list.py index 7bfbba4e09cbaf5f4dacbf7f34f2f482d86f0bd6..b0618d95237be5ff3455953033edea3377fa0346 100644 --- a/gso/workflows/vrf/modify_vrf_router_list.py +++ b/gso/workflows/vrf/modify_vrf_router_list.py @@ -8,7 +8,8 @@ from orchestrator.types import FormGenerator, State, UUIDstr from orchestrator.workflow import StepList, begin, done, step, workflow from orchestrator.workflows.steps import resync, store_process_subscription, unsync from orchestrator.workflows.utils import wrap_modify_initial_input_form -from pydantic import BaseModel, ConfigDict, Field, field_validator +from pydantic import AfterValidator, BaseModel, ConfigDict, Field +from pydantic_forms.validators import validate_unique_list from gso.products.product_types.router import Router from gso.products.product_types.vrf import VRF @@ -27,22 +28,16 @@ def initial_input_form_generator(subscription_id: UUIDstr) -> FormGenerator: router_list: Annotated[ list[RouterSelection], + AfterValidator(validate_unique_list), Field( description="A list of routers to add or remove from the VRF.", min_length=0 if subscription.vrf.vrf_router_list else 1, + json_schema_extra={"uniqueItems": True}, ), ] = [ # noqa: RUF012 RouterSelection(router_id=str(router.owner_subscription_id)) for router in subscription.vrf.vrf_router_list ] - @field_validator("router_list") - def router_list_must_not_have_duplicates(cls, router_list: list[RouterSelection]) -> list[RouterSelection]: - router_ids = [router.router_id for router in router_list] - if len(router_ids) != len(set(router_ids)): - msg = "Duplicate router IDs found in the list." - raise ValueError(msg) - return router_list - user_input = yield ModifyVRFRouterListForm return user_input.model_dump() diff --git a/test/workflows/vrf/test_modify_vrf_router_list.py b/test/workflows/vrf/test_modify_vrf_router_list.py index bd076390ef33b2508b948d232b1fac1b6a5cdc26..97f046561e503cb31eecd0e3f441911fc1a8318d 100644 --- a/test/workflows/vrf/test_modify_vrf_router_list.py +++ b/test/workflows/vrf/test_modify_vrf_router_list.py @@ -51,5 +51,5 @@ def test_modify_vrf_router_list_with_duplicate_router_id(vrf_subscription_factor }, ] - with pytest.raises(FormValidationError, match="Duplicate router IDs found in the list."): + with pytest.raises(FormValidationError, match="List must be unique"): run_workflow("modify_vrf_router_list", initial_vrf_data)