From 8e2f4a6dfd245618cbade3275c37b4fcfd6cbc53 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Konrad=20Ja=C5=82owiecki?= Date: Thu, 11 Jul 2024 01:54:49 +0200 Subject: [PATCH] Reimplement connections validation using model_validator instead of field_validator --- src/qref/schema_v1.py | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/src/qref/schema_v1.py b/src/qref/schema_v1.py index d3540b9..97a6987 100644 --- a/src/qref/schema_v1.py +++ b/src/qref/schema_v1.py @@ -24,7 +24,7 @@ ConfigDict, Field, StringConstraints, - field_validator, + model_validator, ) from pydantic.json_schema import GenerateJsonSchema @@ -115,18 +115,16 @@ class RoutineV1(BaseModel): def __init__(self, **data: Any): super().__init__(**{k: v for k, v in data.items() if v != [] and v != {}}) - @field_validator("connections", mode="after") + @model_validator(mode="after") @classmethod - def _validate_connections(cls, v, values) -> list[ConnectionV1]: - children_port_names = [ - f"{child.name}.{port.name}" for child in values.data.get("children") for port in child.ports - ] - parent_port_names = [port.name for port in values.data["ports"]] + def _validate_connections(cls, routine) -> list[ConnectionV1]: + children_port_names = [f"{child.name}.{port.name}" for child in routine.children for port in child.ports] + parent_port_names = [port.name for port in routine.ports] available_port_names = set(children_port_names + parent_port_names) missed_ports = [ port - for connection in v + for connection in routine.connections for port in (connection.source, connection.target) if port not in available_port_names ] @@ -135,7 +133,7 @@ def _validate_connections(cls, v, values) -> list[ConnectionV1]: "The following ports appear in a connection but are not " "among routine's port or their children's ports: {missed_ports}." ) - return v + return routine class SchemaV1(BaseModel):