-
Notifications
You must be signed in to change notification settings - Fork 0
/
validator_parser.go
71 lines (63 loc) · 1.83 KB
/
validator_parser.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package validator
import (
"fmt"
"reflect"
"github.com/siherrmann/validator/model"
"github.com/siherrmann/validator/parser"
"github.com/siherrmann/validator/validators"
)
// func ValidateValueWithParser(input reflect.Value, requirement string, checkFunction func(reflect.Value, *model.AstValue) error) error {
// lexer := parser.NewLexer(requirement)
// p := parser.NewParser(lexer)
// r, err := p.ParseValidation()
// if err != nil {
// return err
// }
// err = r.RootValue.RunFuncOnConditionGroup(input, checkFunction)
// if err != nil {
// return err
// }
// return nil
// }
func ValidateValueWithParser(input reflect.Value, validation *model.Validation) (interface{}, error) {
validValue, err := validation.GetValidValue(input.Interface())
if err != nil {
return nil, err
}
lexer := parser.NewLexer(validation.Requirement)
p := parser.NewParser(lexer)
r, err := p.ParseValidation()
if err != nil {
return nil, err
}
var checkFunction func(reflect.Value, *model.AstValue) error
switch validation.Type {
case model.String:
checkFunction = validators.CheckString
case model.Int:
checkFunction = validators.CheckInt
case model.Float:
checkFunction = validators.CheckFloat
case model.Bool:
checkFunction = validators.CheckBool
case model.Array:
checkFunction = validators.CheckArray
case model.Map:
checkFunction = validators.CheckMap
case model.Struct:
checkFunction = validators.CheckMap
case model.Time:
checkFunction = validators.CheckTime
case model.TimeISO8601:
checkFunction = validators.CheckTime
case model.TimeUnix:
checkFunction = validators.CheckTime
default:
return nil, fmt.Errorf("invalid validation type: %v", validation.Type)
}
err = r.RootValue.RunFuncOnConditionGroup(reflect.ValueOf(validValue), checkFunction)
if err != nil {
return nil, err
}
return validValue, nil
}