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)
| 79 | // |
| 80 | // Returns nil if the attribute is valid, otherwise returns an error. |
| 81 | func 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. |
no test coverage detected