Skip to content

Commit

Permalink
add missing validate_format methods
Browse files Browse the repository at this point in the history
  • Loading branch information
ajtmccarty committed Jan 14, 2025
1 parent e65a11b commit 1aefb94
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
22 changes: 22 additions & 0 deletions backend/infrahub/core/attribute.py
Original file line number Diff line number Diff line change
Expand Up @@ -1163,6 +1163,17 @@ def deserialize_value(self, data: AttributeFromDB) -> Any:
return ujson.loads(data.value)
return data.value

@classmethod
def validate_format(cls, value: Any, name: str, schema: AttributeSchema) -> None:
value_to_check = value

if isinstance(value, str):
try:
value_to_check = cls.deserialize_from_string(value_as_string=value)
except ujson.JSONDecodeError as exc:
raise ValidationError({name: f"{value} is not a valid JSON list"}) from exc
super().validate_format(value=value_to_check, name=name, schema=schema)


class ListAttributeOptional(ListAttribute):
value: Optional[list[Any]]
Expand All @@ -1188,6 +1199,17 @@ def deserialize_value(self, data: AttributeFromDB) -> Any:
return ujson.loads(data.value)
return data.value

@classmethod
def validate_format(cls, value: Any, name: str, schema: AttributeSchema) -> None:
value_to_check = value

if isinstance(value, str):
try:
value_to_check = cls.deserialize_from_string(value_as_string=value)
except ujson.JSONDecodeError as exc:
raise ValidationError({name: f"{value} is not valid JSON"}) from exc
super().validate_format(value=value_to_check, name=name, schema=schema)


class JSONAttributeOptional(JSONAttribute):
value: Optional[Union[dict, list]]
2 changes: 1 addition & 1 deletion backend/infrahub/core/validators/attribute/kind.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ async def get_paths(self) -> GroupedDataPaths:
if value in (None, NULL_VALUE):
continue
try:
infrahub_attribute_class.validate(
infrahub_attribute_class.validate_format(
value=result.get("attribute_value"), name=self.attribute_schema.name, schema=self.attribute_schema
)
except ValidationError:
Expand Down

0 comments on commit 1aefb94

Please sign in to comment.