-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathutilsOptions.go
96 lines (82 loc) · 2.59 KB
/
utilsOptions.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
package zog
import (
"github.com/Oudwins/zog/conf"
p "github.com/Oudwins/zog/internals"
"github.com/Oudwins/zog/zconst"
)
// Options that can be passed to a test
type TestOption = p.TestOption
// Message is a function that allows you to set a custom message for the test.
func Message(msg string) TestOption {
return func(test *p.Test) {
test.IssueFmtFunc = func(e *ZogIssue, p Ctx) {
e.SetMessage(msg)
}
}
}
// MessageFunc is a function that allows you to set a custom message formatter for the test.
func MessageFunc(fn IssueFmtFunc) TestOption {
return func(test *Test) {
test.IssueFmtFunc = fn
}
}
// IssueCode is a function that allows you to set a custom issue code for the test. Most useful for TestFuncs:
/*
z.String().TestFunc(..., z.IssueCode("just_provide_a_string" or use values in zconst))
*/
func IssueCode(code zconst.ZogIssueCode) TestOption {
return func(test *Test) {
test.IssueCode = code
}
}
// IssuePath is a function that allows you to set a custom issue path for the test.
// Beware with using this as it is not typesafe and can lead to unexpected behavior if you change the schema or have a typo.
// Usage:
/*
z.Struct(
z.Schema {
"Name": z.String().Required(z.IssuePath("fullname")),
"Fullname": z.String(),
}
)
*/
func IssuePath(path string) TestOption {
return func(test *Test) {
test.IssuePath = path
}
}
// Params is a function that allows you to set a custom params for the test.
// You may then access these values when formatting test errors in the IssueFmtFunc
func Params(params map[string]any) TestOption {
return func(test *Test) {
test.Params = params
}
}
// Options that can be passed to a `schema.New()` call
type SchemaOption = func(s ZogSchema)
func WithCoercer(c conf.CoercerFunc) SchemaOption {
return func(s ZogSchema) {
s.setCoercer(c)
}
}
// Options that can be passed to a `schema.Parse()` call
type ExecOption = func(p *p.ExecCtx)
// Deprecated: use ExecOption instead
type ParsingOption = ExecOption
// Deprecated: use WithIssueFormatter instead
// Deprecated for naming consistency
func WithErrFormatter(fmter IssueFmtFunc) ExecOption {
return WithIssueFormatter(fmter)
}
// Sets the issue formatter for the execution context. This is used to format the issues messages during execution.
// This follows principle of most specific wins. So default formatter < execution formatter < test specific formatter (i.e MessageFunc)
func WithIssueFormatter(fmter IssueFmtFunc) ExecOption {
return func(p *p.ExecCtx) {
p.SetIssueFormatter(fmter)
}
}
func WithCtxValue(key string, val any) ExecOption {
return func(p *p.ExecCtx) {
p.Set(key, val)
}
}