File size: 3,623 Bytes
31e8457
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import { expect, test } from "vitest";
import * as z from "zod/v4";

test("nested refinements", () => {
  const zodSchema = z
    .object({
      password: z.string().min(1),
      nested: z
        .object({
          confirm: z
            .string()
            .min(1)
            .refine((value) => value.length > 2, {
              message: "Confirm length should be > 2",
            }),
        })
        .refine(
          (data) => {
            return data.confirm === "bar";
          },
          {
            path: ["confirm"],
            error: 'Value must be "bar"',
          }
        ),
    })
    .refine(
      (data) => {
        return data.nested.confirm === data.password;
      },
      {
        path: ["nested", "confirm"],
        error: "Password and confirm must match",
      }
    );

  const DATA = {
    password: "bar",
    nested: { confirm: "" },
  };
  expect(zodSchema.safeParse(DATA)).toMatchInlineSnapshot(`
    {
      "error": [ZodError: [
      {
        "origin": "string",
        "code": "too_small",
        "minimum": 1,
        "inclusive": true,
        "path": [
          "nested",
          "confirm"
        ],
        "message": "Too small: expected string to have >=1 characters"
      },
      {
        "code": "custom",
        "path": [
          "nested",
          "confirm"
        ],
        "message": "Confirm length should be > 2"
      },
      {
        "code": "custom",
        "path": [
          "nested",
          "confirm"
        ],
        "message": "Value must be \\"bar\\""
      },
      {
        "code": "custom",
        "path": [
          "nested",
          "confirm"
        ],
        "message": "Password and confirm must match"
      }
    ]],
      "success": false,
    }
  `);

  expect(zodSchema.safeParse(DATA, { jitless: true })).toMatchInlineSnapshot(`
    {
      "error": [ZodError: [
      {
        "origin": "string",
        "code": "too_small",
        "minimum": 1,
        "inclusive": true,
        "path": [
          "nested",
          "confirm"
        ],
        "message": "Too small: expected string to have >=1 characters"
      },
      {
        "code": "custom",
        "path": [
          "nested",
          "confirm"
        ],
        "message": "Confirm length should be > 2"
      },
      {
        "code": "custom",
        "path": [
          "nested",
          "confirm"
        ],
        "message": "Value must be \\"bar\\""
      },
      {
        "code": "custom",
        "path": [
          "nested",
          "confirm"
        ],
        "message": "Password and confirm must match"
      }
    ]],
      "success": false,
    }
  `);

  expect(zodSchema["~standard"].validate(DATA)).toMatchInlineSnapshot(`
    {
      "issues": [
        {
          "code": "too_small",
          "inclusive": true,
          "message": "Too small: expected string to have >=1 characters",
          "minimum": 1,
          "origin": "string",
          "path": [
            "nested",
            "confirm",
          ],
        },
        {
          "code": "custom",
          "message": "Confirm length should be > 2",
          "path": [
            "nested",
            "confirm",
          ],
        },
        {
          "code": "custom",
          "message": "Value must be "bar"",
          "path": [
            "nested",
            "confirm",
          ],
        },
        {
          "code": "custom",
          "message": "Password and confirm must match",
          "path": [
            "nested",
            "confirm",
          ],
        },
      ],
    }
  `);
});