-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy patherror_example_test.go
More file actions
46 lines (39 loc) · 981 Bytes
/
Copy patherror_example_test.go
File metadata and controls
46 lines (39 loc) · 981 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
45
46
package gozod_test
import (
"errors"
"fmt"
"github.com/kaptinlin/gozod"
"github.com/kaptinlin/gozod/core"
)
func ExamplePrettifyError() {
schema := gozod.Object(gozod.ObjectSchema{
"name": gozod.String().Min(2),
})
_, err := schema.Parse(map[string]any{"name": "A"})
zodErr, ok := errors.AsType[*gozod.ZodError](err)
if !ok {
panic(err)
}
pretty := gozod.PrettifyError(zodErr)
tree := gozod.TreeifyError(zodErr)
flat := gozod.FlattenError(zodErr)
fmt.Println(len(pretty) > 0)
fmt.Println(len(tree.Properties["name"].Errors))
fmt.Println(len(flat.FieldErrors["name"]))
// Output:
// true
// 1
// 1
}
func ExampleString_parseContext() {
ctx := core.NewParseContext().WithCustomError(func(core.ZodRawIssue) string {
return "request-specific message"
})
_, err := gozod.String().Parse(42, ctx)
zodErr, ok := errors.AsType[*gozod.ZodError](err)
if !ok {
panic(err)
}
fmt.Println(zodErr.Issues[0].Message)
// Output: request-specific message
}