MCPcopy Create free account
hub / github.com/Permify/permify / ValidateAttribute

Function ValidateAttribute

internal/validation/validation.go:81–107  ·  view source on GitHub ↗

ValidateAttribute checks whether a given attribute request (reqAttribute) aligns with the attribute definition in a given entity definition. It verifies if the attribute exists in the entity definition and if the attribute type matches the type specified in the request. If the attribute value is not

(definition *base.EntityDefinition, reqAttribute *base.Attribute)

Source from the content-addressed store, hash-verified

79//
80// Returns nil if the attribute is valid, otherwise returns an error.
81func ValidateAttribute(definition *base.EntityDefinition, reqAttribute *base.Attribute) (err error) {
82 // Initialize a pointer to an AttributeDefinition.
83 var attr *base.AttributeDefinition
84
85 // Fetch the attribute from the entity definition by the attribute name from reqAttribute.
86 // If it does not exist, an error is returned.
87 attr, err = schema.GetAttributeByNameInEntityDefinition(definition, reqAttribute.GetAttribute())
88 if err != nil {
89 return err
90 }
91
92 // Check whether the type of the attribute in the request matches the type defined in the entity.
93 // If the types do not match, an error indicating a type mismatch is returned.
94 if attribute.TypeToString(attr.GetType()) != attribute.TypeUrlToString(reqAttribute.GetValue().GetTypeUrl()) {
95 return errors.New(base.ErrorCode_ERROR_CODE_ATTRIBUTE_TYPE_MISMATCH.String())
96 }
97
98 // Validate the value of the attribute in the request against the type defined in the entity.
99 // If the value is not valid, an error is returned.
100 err = attribute.ValidateValue(reqAttribute.GetValue(), attr.GetType())
101 if err != nil {
102 return err
103 }
104
105 // If all checks pass without returning, the attribute is considered valid and the function returns nil.
106 return nil
107}
108
109// IsTupleFilterEmpty checks whether any of the fields in a TupleFilter are filled.
110// It assumes that a filter is "empty" if all its fields are unset or have zero values.

Callers 3

RunWithShapeMethod · 0.92
WriteMethod · 0.92
validation_test.goFile · 0.85

Calls 8

GetTypeMethod · 0.95
TypeToStringFunction · 0.92
TypeUrlToStringFunction · 0.92
ValidateValueFunction · 0.92
StringMethod · 0.65
GetAttributeMethod · 0.45
GetValueMethod · 0.45

Tested by

no test coverage detected