-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathvalidate.go
88 lines (76 loc) · 1.91 KB
/
validate.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package validator
import (
"reflect"
"oss.nandlabs.io/golly/l3"
)
var logger = l3.Get()
type StructValidatorFunc func(field field, param string) error
type tStruct struct {
name string
value string
fnc StructValidatorFunc
}
type field struct {
name string
value reflect.Value
typ reflect.Type
index []int
constraints []tStruct
inter interface{}
}
type structFields struct {
list []field
}
type StructValidator struct {
fields structFields
validationFunc map[string]StructValidatorFunc
tagName string
enableCache bool
}
// NewStructValidator : Generates the new StructValidator object
func NewStructValidator() *StructValidator {
return &StructValidator{
validationFunc: map[string]StructValidatorFunc{
// Base Constraints
// Numeric Constraints
// <, > only
"min": min,
"max": max,
// <=, >= this is inclusive of the input value
"exclusiveMin": exclusiveMin,
"exclusiveMax": exclusiveMax,
"multipleOf": multipleOf,
// String Constraints
// boolean value
"notnull": notnull,
"min-length": minLength,
"max-length": maxLength,
// regex pattern support
"pattern": pattern,
// enums support
"enum": enum,
},
tagName: "constraints",
enableCache: false,
}
}
// SetTagName : provide the custom tag-name for your constraints
// By default : 'constraints' tag-name is used
func (sv *StructValidator) SetTagName(tag string) *StructValidator {
sv.tagName = tag
return sv
}
// SetCache : provide true/false to enable or disable cache in the validator
func (sv *StructValidator) SetCache(action bool) *StructValidator {
sv.enableCache = action
return sv
}
// Validate : Runs the core of logic of the validations schema
func (sv *StructValidator) Validate(v interface{}) error {
//check for cache
sv.fields = sv.cachedTypeFields(v)
if err := sv.validateFields(); err != nil {
return err
}
return nil
}