ValidateTuple checks if the provided tuple conforms to the entity definition and relation schema provided. It returns an error if the tuple is invalid.
(definition *base.EntityDefinition, tup *base.Tuple)
| 13 | // ValidateTuple checks if the provided tuple conforms to the entity definition |
| 14 | // and relation schema provided. It returns an error if the tuple is invalid. |
| 15 | func ValidateTuple(definition *base.EntityDefinition, tup *base.Tuple) (err error) { |
| 16 | // Check if the entity and the subject of the tuple are the same |
| 17 | if tuple.IsEntityAndSubjectEquals(tup) { |
| 18 | return errors.New(base.ErrorCode_ERROR_CODE_ENTITY_AND_SUBJECT_CANNOT_BE_EQUAL.String()) |
| 19 | } |
| 20 | |
| 21 | // Initialize variables for the relation definition and valid types |
| 22 | var rel *base.RelationDefinition |
| 23 | var vt []string |
| 24 | |
| 25 | // Get the relation definition for the tuple's relation within the entity definition |
| 26 | rel, err = schema.GetRelationByNameInEntityDefinition(definition, tup.GetRelation()) |
| 27 | if err != nil { |
| 28 | return err |
| 29 | } |
| 30 | |
| 31 | // Iterate over relation references and build the list of valid types |
| 32 | for _, t := range rel.GetRelationReferences() { |
| 33 | if t.GetRelation() != "" { |
| 34 | vt = append(vt, fmt.Sprintf("%s#%s", t.GetType(), t.GetRelation())) |
| 35 | } else { |
| 36 | vt = append(vt, t.GetType()) |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | // Validate if the subject type is among the valid types |
| 41 | err = tuple.ValidateSubjectType(tup.GetSubject(), vt) |
| 42 | if err != nil { |
| 43 | return err |
| 44 | } |
| 45 | |
| 46 | // If no errors were encountered, return nil |
| 47 | return nil |
| 48 | } |
| 49 | |
| 50 | // ValidateTupleFilter checks if the provided filter conforms to the entity definition |
| 51 | func ValidateTupleFilter(tupleFilter *base.TupleFilter) (err error) { |
no test coverage detected