-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathexample_customizations_test.go
More file actions
44 lines (35 loc) · 974 Bytes
/
example_customizations_test.go
File metadata and controls
44 lines (35 loc) · 974 Bytes
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
package validating_test
import (
"fmt"
"time"
v "github.com/RussellLuo/validating/v3"
)
func mapNonzero(field *v.Field) v.Errors {
value, ok := field.Value.(map[string]time.Time)
if !ok {
var want map[string]time.Time
return v.NewUnsupportedErrors("mapNonzero", field, want)
}
if len(value) == 0 {
return v.NewInvalidErrors(field, "is zero valued")
}
return nil
}
type MyValidator struct{}
func (mv MyValidator) Validate(field *v.Field) v.Errors {
return mapNonzero(field)
}
func Example_customizations() {
var value map[string]time.Time
errs := v.Validate(v.Schema{
v.F("value", value): v.Func(mapNonzero),
})
fmt.Printf("errs from the func-validator: %+v\n", errs)
errs = v.Validate(v.Schema{
v.F("value", value): MyValidator{},
})
fmt.Printf("errs from the struct-validator: %+v\n", errs)
// Output:
// errs from the func-validator: value: INVALID(is zero valued)
// errs from the struct-validator: value: INVALID(is zero valued)
}