Skip to content

Commit 0509b20

Browse files
authored
IHS-217: Fix object profile file validation for mandatory field (#980)
1 parent a14b7df commit 0509b20

3 files changed

Lines changed: 19 additions & 3 deletions

File tree

changelog/908.fixed.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Skip mandatory field validation during object loading when `object_profile` is specified.

infrahub_sdk/spec/object.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -267,9 +267,9 @@ async def validate_object(
267267
context = context.copy() if context else {}
268268

269269
# First validate if all mandatory fields are present
270-
# Skip mandatory check when an object_template is specified, as the template provides defaults, we expect the API server to just send a
271-
# response with a failure if the template is not valid or doesn't provide all mandatory fields
272-
if "object_template" not in data:
270+
# Skip mandatory check when an object_template or object_profile is specified, as the template/profile provides defaults;
271+
# we expect the API server to return a failure if the template/profile is not valid or doesn't provide all mandatory fields
272+
if "object_template" not in data and "object_profile" not in data:
273273
errors.extend(
274274
ObjectValidationError(position=[*position, element], message=f"{element} is mandatory")
275275
for element in schema.mandatory_input_names

tests/unit/sdk/spec/test_object.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,3 +472,18 @@ async def test_validate_object_skips_mandatory_check_with_object_template(
472472

473473
mandatory_errors = [e for e in errors if e.message == "type is mandatory"]
474474
assert mandatory_errors == []
475+
476+
477+
async def test_validate_object_skips_mandatory_check_with_object_profile(
478+
client_with_schema_01: InfrahubClient,
479+
) -> None:
480+
"""When object_profile is present, mandatory field validation should be skipped."""
481+
schema = await client_with_schema_01.schema.get(kind="BuiltinLocation")
482+
483+
data = {"name": "Site1", "object_profile": "Standard Site"}
484+
errors = await InfrahubObjectFileData.validate_object(
485+
client=client_with_schema_01, position=[1], schema=schema, data=data
486+
)
487+
488+
mandatory_errors = [e for e in errors if e.message == "type is mandatory"]
489+
assert mandatory_errors == []

0 commit comments

Comments
 (0)