Skip to content

Commit

Permalink
add AttributeKindChecker to aggregated constraints (#5460)
Browse files Browse the repository at this point in the history
* add AttributeKindChecker to aggregated constraints

* add missing validate_format methods

* add changelog
  • Loading branch information
ajtmccarty authored Jan 15, 2025
1 parent a08a438 commit 77df253
Show file tree
Hide file tree
Showing 5 changed files with 34 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
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from .attribute_choices import SchemaAttributeChoicesConstraintDependency
from .attribute_enum import SchemaAttributeEnumConstraintDependency
from .attribute_kind import SchemaAttributeKindConstraintDependency
from .attribute_length import SchemaAttributLengthConstraintDependency
from .attribute_optional import SchemaAttributeOptionalConstraintDependency
from .attribute_regex import SchemaAttributeRegexConstraintDependency
Expand Down Expand Up @@ -32,6 +33,7 @@ def build(cls, context: DependencyBuilderContext) -> AggregatedConstraintChecker
SchemaAttributeChoicesConstraintDependency.build(context=context),
SchemaAttributeEnumConstraintDependency.build(context=context),
SchemaAttributLengthConstraintDependency.build(context=context),
SchemaAttributeKindConstraintDependency.build(context=context),
SchemaNodeAttributeAddConstraintDependency.build(context=context),
SchemaNodeRelationshipAddConstraintDependency.build(context=context),
],
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from infrahub.core.validators.attribute.kind import AttributeKindChecker
from infrahub.dependencies.interface import DependencyBuilder, DependencyBuilderContext


class SchemaAttributeKindConstraintDependency(DependencyBuilder[AttributeKindChecker]):
@classmethod
def build(cls, context: DependencyBuilderContext) -> AttributeKindChecker:
return AttributeKindChecker(db=context.db, branch=context.branch)
1 change: 1 addition & 0 deletions changelog/5460.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Validate updates to an attribute's `kind` when loading a new schema

0 comments on commit 77df253

Please sign in to comment.