-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathutils.go
121 lines (100 loc) · 4.05 KB
/
utils.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package zog
import (
"reflect"
p "github.com/Oudwins/zog/internals"
"github.com/Oudwins/zog/zconst"
)
// ! Passing Types through
// This is the context that is passed through an entire execution of `schema.Parse()` or `schema.Validate()`.
// You can use it to pass a key/value for a specific execution. More about context in the [docs](https://zog.dev/context)
type Ctx = p.Ctx
// This is a type for the ZogIssue type. It is the type of all the errors returned from zog.
type ZogIssue = p.ZogIssue
// This is a type for the ZogErrList type. It is a list of ZogIssues returned from parsing primitive schemas. The type is []ZogIssue
type ZogIssueList = p.ZogIssueList
// This is a type for the ZogIssueMap type. It is a map[string][]ZogIssue returned from parsing complex schemas. The type is map[string][]ZogIssue
// All errors are returned in a flat map, not matter how deep the schema is. For example:
/*
schema := z.Struct(z.Schema{
"address": z.Struct(z.Schema{
"street": z.String().Min(3).Max(10),
"city": z.String().Min(3).Max(10),
}),
"fields": z.Slice(z.String().Min(3).Max(10)),
})
errors = map[string][]ZogIssue{
"address.street": []ZogIssue{....}, // error for the street field in the address struct
"fields[0]": []ZogIssue{...}, // error for the first field in the slice
}
*/
type ZogIssueMap = p.ZogIssueMap
// ! TESTS
// Test is the test object. It is the struct that represents an individual validation. For example `z.String().Min(3)` is a test that checks if the string is at least 3 characters long.
type Test = p.Test
// WARNING: Use z.schema.TestFunc instead. This may be removed in the future
// TestFunc is a helper function to define a custom test. It takes the error code which will be used for the error message and a validate function. Usage:
//
// schema.Test(z.TestFunc(zconst.IssueCodeCustom, func(val any, ctx Ctx) bool {
// return val == "hello"
// }))
func TestFunc(IssueCode zconst.ZogIssueCode, validateFunc p.TestFunc) p.Test {
t := p.Test{
IssueCode: IssueCode,
ValidateFunc: validateFunc,
}
return t
}
type issueHelpers struct {
}
var Issues = issueHelpers{}
// SanitizeMap returns a map of issue messages for each key in the map. It keeps only the issue messages and strips out any other issue data.
func (i *issueHelpers) SanitizeMap(m ZogIssueMap) map[string][]string {
errs := make(map[string][]string, len(m))
for k, v := range m {
errs[k] = i.SanitizeList(v)
}
return errs
}
// Suger function that does both sanitize and collect.
func (i issueHelpers) SanitizeMapAndCollect(m ZogIssueMap) map[string][]string {
errs := i.SanitizeMap(m)
i.CollectMap(m)
return errs
}
// SanitizeList returns a slice of issue messages for each issue in the list. It keeps only the issue messages and strips out any other issue data.
func (i *issueHelpers) SanitizeList(l ZogIssueList) []string {
errs := make([]string, len(l))
for i, err := range l {
errs[i] = err.Message
}
return errs
}
// Suger function that does both sanitize and collect.
func (i issueHelpers) SanitizeListAndCollect(l ZogIssueList) []string {
errs := i.SanitizeList(l)
i.CollectList(l)
return errs
}
// Collects a ZogIssueMap to be reused by Zog. This will "free" the issues in the map. This can help make Zog more performant by reusing issue structs.
func (i *issueHelpers) CollectMap(issues ZogIssueMap) {
for _, list := range issues {
i.CollectList(list)
}
}
// Collects a ZogIssueList to be reused by Zog. This will "free" the issues in the list. This can help make Zog more performant by reusing issue structs.
func (i *issueHelpers) CollectList(issues ZogIssueList) {
for _, iss := range issues {
i.Collect(iss)
}
}
// Collects a ZogIssue to be reused by Zog. This will "free" the issue. This can help make Zog more performant by reusing issue structs.
func (i *issueHelpers) Collect(issue *ZogIssue) {
p.FreeIssue(issue)
}
// Backwards Compatibility
func customTestBackwardsCompatWrapper(testFunc p.TestFunc) func(val any, ctx Ctx) bool {
return func(val any, ctx Ctx) bool {
refVal := reflect.ValueOf(val).Elem()
return testFunc(refVal.Interface(), ctx)
}
}