Skip to content

Commit 0d91970

Browse files
committed
Update logic to validate relationship
1 parent feb5b1e commit 0d91970

File tree

1 file changed

+31
-6
lines changed

1 file changed

+31
-6
lines changed

infrahub_sdk/spec/object.py

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,26 @@ def is_valid(self) -> bool:
6969
def is_reference(self) -> bool:
7070
return self.format in [RelationshipDataFormat.ONE_REF, RelationshipDataFormat.MANY_REF]
7171

72+
def get_context(self, value: Any) -> dict:
73+
"""Return a dict to insert to the context if the relationship is mandatory"""
74+
if self.peer_rel and self.is_mandatory and self.peer_rel.cardinality == "one":
75+
return {self.peer_rel.name: value}
76+
if self.peer_rel and self.is_mandatory and self.peer_rel.cardinality == "many":
77+
return {self.peer_rel.name: [value]}
78+
return {}
79+
80+
def find_matching_relationship(
81+
self, peer_schema: MainSchemaTypesAPI, force: bool = False
82+
) -> RelationshipSchema | None:
83+
"""Find the matching relationship on the other side of the relationship"""
84+
if self.peer_rel and not force:
85+
return self.peer_rel
86+
87+
self.peer_rel = peer_schema.get_matching_relationship(
88+
id=self.rel_schema.identifier or "", direction=self.rel_schema.direction
89+
)
90+
return self.peer_rel
91+
7292

7393
async def get_relationship_info(
7494
client: InfrahubClient, schema: MainSchemaTypesAPI, name: str, value: Any, branch: str | None = None
@@ -202,12 +222,6 @@ async def validate_object(
202222
)
203223
)
204224

205-
# If there is a peer relationship, we add a placeholder for the related node
206-
if rel_info.peer_rel and rel_info.is_mandatory and rel_info.peer_rel.cardinality == "one":
207-
context[rel_info.peer_rel.name] = "placeholder"
208-
elif rel_info.peer_rel and rel_info.is_mandatory and rel_info.peer_rel.cardinality == "many":
209-
context[rel_info.peer_rel.name] = ["placeholder"]
210-
211225
errors.extend(
212226
await cls.validate_related_nodes(
213227
client=client,
@@ -243,6 +257,10 @@ async def validate_related_nodes(
243257
if isinstance(data, dict) and rel_info.format == RelationshipDataFormat.ONE_OBJ:
244258
peer_kind = data.get("kind") or rel_info.peer_kind
245259
peer_schema = await client.schema.get(kind=peer_kind, branch=branch)
260+
261+
rel_info.find_matching_relationship(peer_schema=peer_schema)
262+
context.update(rel_info.get_context(value="placeholder"))
263+
246264
errors.extend(
247265
await cls.validate_object(
248266
client=client,
@@ -259,6 +277,9 @@ async def validate_related_nodes(
259277
peer_kind = data.get("kind") or rel_info.peer_kind
260278
peer_schema = await client.schema.get(kind=peer_kind, branch=branch)
261279

280+
rel_info.find_matching_relationship(peer_schema=peer_schema)
281+
context.update(rel_info.get_context(value="placeholder"))
282+
262283
for idx, peer_data in enumerate(data["data"]):
263284
context["list_index"] = idx
264285
errors.extend(
@@ -278,6 +299,10 @@ async def validate_related_nodes(
278299
context["list_index"] = idx
279300
peer_kind = item.get("kind") or rel_info.peer_kind
280301
peer_schema = await client.schema.get(kind=peer_kind, branch=branch)
302+
303+
rel_info.find_matching_relationship(peer_schema=peer_schema)
304+
context.update(rel_info.get_context(value="placeholder"))
305+
281306
errors.extend(
282307
await cls.validate_object(
283308
client=client,

0 commit comments

Comments
 (0)