Skip to content

Commit 8cf61ca

Browse files
committed
test: add union validation errors test
1 parent c3849b8 commit 8cf61ca

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

packages/next-safe-action/src/__tests__/validation-errors.test.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,44 @@ test("action with invalid input gives back an object with correct `validationErr
7171
assert.deepStrictEqual(actualResult, expectedResult);
7272
});
7373

74+
test("action with invalid enum input gives back an object with correct `validationErrors` (default formatted shape)", async () => {
75+
const schema = z.object({
76+
foo: z.object({
77+
bar: z.union([z.literal("a"), z.literal("b")]),
78+
}),
79+
baz: z.string().min(3),
80+
});
81+
82+
const action = dac.inputSchema(schema).action(async () => {
83+
return {
84+
ok: true,
85+
};
86+
});
87+
88+
const actualResult = await action({
89+
foo: {
90+
// @ts-expect-error
91+
bar: "c",
92+
},
93+
baz: "a",
94+
});
95+
96+
const expectedResult = {
97+
validationErrors: {
98+
foo: {
99+
bar: {
100+
_errors: ['Invalid literal value, expected "a"', 'Invalid literal value, expected "b"'],
101+
},
102+
},
103+
baz: {
104+
_errors: ["String must contain at least 3 character(s)"],
105+
},
106+
},
107+
};
108+
109+
assert.deepStrictEqual(actualResult, expectedResult);
110+
});
111+
74112
test("action with root level schema error gives back an object with correct `validationErrors` (default formatted shape)", async () => {
75113
const userId = "invalid_uuid";
76114

0 commit comments

Comments
 (0)