Пример #1
0
func ValidateNode(ctx context.Context, n *node.Node) (errors []ValidationError, err error) {

	// First validate all the nodes
	for _, current := range n.Flatten(true) {
		// Validate the actual object
		if v, ok := current.Value.(system.Validator); ok {
			failed, messages, err := v.Validate(ctx)
			if err != nil {
				return nil, kerr.Wrap("JXKTDTIIYG", err)
			}
			if failed {
				for _, message := range messages {
					errors = append(errors, ValidationError{Struct: kerr.New("KULDIJUYFB", message), Source: current})
				}
			}
		}
	}

	// Then build a list of all nodes that have rules
	cache := map[*node.Node][]system.RuleInterface{}
	if err := BuildRulesNode(ctx, n, cache); err != nil {
		return nil, kerr.Wrap("YPUHTXPGRA", err)
	}

	// Then enforce the rules
	for current, rules := range cache {
		for _, rule := range rules {
			e, ok := rule.(system.Enforcer)
			if !ok {
				continue
			}
			failed, messages, err := e.Enforce(ctx, current.Value)
			if err != nil {
				return nil, kerr.Wrap("EBEMISLGDX", err)
			}
			if failed {
				for _, message := range messages {
					errors = append(errors, ValidationError{Struct: kerr.New("HLKQWDCMRN", message), Source: current})
				}
			}
		}
	}
	return errors, nil
}
Пример #2
0
func CreateParser(ctx context.Context, node *node.Node) (*Parser, error) {
	parser := Parser{node: node, root: node, ctx: ctx}
	parser.flattened = node.Flatten(true)

	//fmt.Println("CreateParser")
	//for i, n := range parser.flattened {
	//	fmt.Println(i, n.Type.Id.Value(), n.ValueString, n.Key)
	//}

	return &parser, nil
}