/
opt
/
canhelp
/
node_modules
/
zod
/
src
/
v4
/
classic
/
tests
/
/opt/canhelp/node_modules/zod/src/v4/classic/tests
mkdir
upload
Name
Size
Mode
Actions
anyunknown.test.ts
639
0644
edit
dl
rm
array.test.ts
7284
0644
edit
dl
rm
assignability.test.ts
6790
0644
edit
dl
rm
async-parsing.test.ts
12311
0644
edit
dl
rm
async-refinements.test.ts
1867
0644
edit
dl
rm
base.test.ts
179
0644
edit
dl
rm
bigint.test.ts
1956
0644
edit
dl
rm
brand.test.ts
2206
0644
edit
dl
rm
catch.test.ts
7224
0644
edit
dl
rm
coalesce.test.ts
743
0644
edit
dl
rm
coerce.test.ts
7736
0644
edit
dl
rm
continuability.test.ts
7478
0644
edit
dl
rm
custom.test.ts
1215
0644
edit
dl
rm
date.test.ts
962
0644
edit
dl
rm
datetime.test.ts
11511
0644
edit
dl
rm
default.test.ts
7717
0644
edit
dl
rm
description.test.ts
1294
0644
edit
dl
rm
discriminated-unions.test.ts
16810
0644
edit
dl
rm
enum.test.ts
8488
0644
edit
dl
rm
error-utils.test.ts
12572
0644
edit
dl
rm
error.test.ts
19502
0644
edit
dl
rm
file.test.ts
2460
0644
edit
dl
rm
firstparty.test.ts
2977
0644
edit
dl
rm
function.test.ts
6299
0644
edit
dl
rm
generics.test.ts
2162
0644
edit
dl
rm
index.test.ts
27562
0644
edit
dl
rm
instanceof.test.ts
1052
0644
edit
dl
rm
intersection.test.ts
4763
0644
edit
dl
rm
json.test.ts
3309
0644
edit
dl
rm
lazy.test.ts
4756
0644
edit
dl
rm
literal.test.ts
2478
0644
edit
dl
rm
map.test.ts
4817
0644
edit
dl
rm
nan.test.ts
645
0644
edit
dl
rm
nested-refine.test.ts
3623
0644
edit
dl
rm
nonoptional.test.ts
2405
0644
edit
dl
rm
nullable.test.ts
591
0644
edit
dl
rm
number.test.ts
7827
0644
edit
dl
rm
object.test.ts
15999
0644
edit
dl
rm
optional.test.ts
4130
0644
edit
dl
rm
partial.test.ts
4835
0644
edit
dl
rm
pickomit.test.ts
4422
0644
edit
dl
rm
pipe.test.ts
1848
0644
edit
dl
rm
prefault.test.ts
1007
0644
edit
dl
rm
preprocess.test.ts
6959
0644
edit
dl
rm
primitive.test.ts
7649
0644
edit
dl
rm
promise.test.ts
2315
0644
edit
dl
rm
prototypes.test.ts
498
0644
edit
dl
rm
readonly.test.ts
11777
0644
edit
dl
rm
record.test.ts
8118
0644
edit
dl
rm
recursive-types.test.ts
7335
0644
edit
dl
rm
refine.test.ts
14774
0644
edit
dl
rm
registries.test.ts
5999
0644
edit
dl
rm
set.test.ts
5572
0644
edit
dl
rm
standard-schema.test.ts
1369
0644
edit
dl
rm
string-formats.test.ts
3068
0644
edit
dl
rm
string.test.ts
64124
0644
edit
dl
rm
stringbool.test.ts
2333
0644
edit
dl
rm
template-literal.test.ts
34836
0644
edit
dl
rm
to-json-schema.test.ts
59276
0644
edit
dl
rm
transform.test.ts
6000
0644
edit
dl
rm
tuple.test.ts
4702
0644
edit
dl
rm
union.test.ts
2497
0644
edit
dl
rm
validations.test.ts
6736
0644
edit
dl
rm
void.test.ts
306
0644
edit
dl
rm
Edit:
/opt/canhelp/node_modules/zod/src/v4/classic/tests/refine.test.ts
(14774B)
import { describe, expect, test } from "vitest"; import * as z from "zod/v4"; describe("basic refinement functionality", () => { test("should create a new schema instance when refining", () => { const obj1 = z.object({ first: z.string(), second: z.string(), }); const obj2 = obj1.partial().strict(); const obj3 = obj2.refine((data) => data.first || data.second, "Either first or second should be filled in."); expect(obj1 === (obj2 as any)).toEqual(false); expect(obj2 === (obj3 as any)).toEqual(false); }); test("should validate according to refinement logic", () => { const schema = z .object({ first: z.string(), second: z.string(), }) .partial() .strict() .refine((data) => data.first || data.second, "Either first or second should be filled in."); // Should fail on empty object expect(() => schema.parse({})).toThrow(); // Should pass with first property expect(schema.parse({ first: "a" })).toEqual({ first: "a" }); // Should pass with second property expect(schema.parse({ second: "a" })).toEqual({ second: "a" }); // Should pass with both properties expect(schema.parse({ first: "a", second: "a" })).toEqual({ first: "a", second: "a" }); }); test("should validate strict mode correctly", () => { const schema = z .object({ first: z.string(), second: z.string(), }) .partial() .strict(); // Should throw on extra properties expect(() => schema.parse({ third: "adsf" })).toThrow(); }); }); describe("refinement with custom error messages", () => { test("should use custom error message when validation fails", () => { const validationSchema = z .object({ email: z.string().email(), password: z.string(), confirmPassword: z.string(), }) .refine((data) => data.password === data.confirmPassword, "Both password and confirmation must match"); const result = validationSchema.safeParse({ email: "aaaa@gmail.com", password: "aaaaaaaa", confirmPassword: "bbbbbbbb", }); expect(result.success).toEqual(false); if (!result.success) { expect(result.error.issues[0].message).toEqual("Both password and confirmation must match"); } }); }); describe("async refinements", () => { test("should support async refinement functions", async () => { const validationSchema = z .object({ email: z.string().email(), password: z.string(), confirmPassword: z.string(), }) .refine( (data) => Promise.resolve().then(() => data.password === data.confirmPassword), "Both password and confirmation must match" ); // Should pass with matching passwords const validData = { email: "aaaa@gmail.com", password: "password", confirmPassword: "password", }; await expect(validationSchema.parseAsync(validData)).resolves.toEqual(validData); // Should fail with non-matching passwords await expect( validationSchema.parseAsync({ email: "aaaa@gmail.com", password: "password", confirmPassword: "different", }) ).rejects.toThrow(); }); }); describe("early termination options", () => { test("should abort early with continue: false", () => { const schema = z .string() .superRefine((val, ctx) => { if (val.length < 2) { ctx.addIssue({ code: "custom", message: "BAD", continue: false, }); } }) .refine((_) => false); const result = schema.safeParse(""); expect(result.success).toEqual(false); if (!result.success) { expect(result.error.issues.length).toEqual(1); expect(result.error.issues[0].message).toEqual("BAD"); } }); test("should abort early with fatal: true", () => { const schema = z .string() .superRefine((val, ctx) => { if (val.length < 2) { ctx.addIssue({ code: "custom", fatal: true, message: "BAD", }); } }) .refine((_) => false); const result = schema.safeParse(""); expect(result.success).toEqual(false); if (!result.success) { expect(result.error.issues.length).toEqual(1); expect(result.error.issues[0].message).toEqual("BAD"); } }); test("should abort early with abort flag", () => { const schema = z .string() .refine((_) => false, { abort: true }) .refine((_) => false); const result = schema.safeParse(""); expect(result.success).toEqual(false); if (!result.success) { expect(result.error.issues.length).toEqual(1); } }); }); describe("custom error paths", () => { test("should use custom path in error message", async () => { const result = await z .object({ password: z.string(), confirm: z.string() }) .refine((data) => data.confirm === data.password, { path: ["confirm"] }) .safeParse({ password: "asdf", confirm: "qewr" }); expect(result.success).toEqual(false); if (!result.success) { expect(result.error.issues[0].path).toEqual(["confirm"]); } }); }); describe("superRefine functionality", () => { test("should support multiple validation rules", () => { const Strings = z.array(z.string()).superRefine((val, ctx) => { if (val.length > 3) { ctx.addIssue({ input: val, code: "too_big", origin: "array", maximum: 3, inclusive: true, exact: true, message: "Too many items 😡", }); } if (val.length !== new Set(val).size) { ctx.addIssue({ input: val, code: "custom", message: `No duplicates allowed.`, }); } }); // Should fail with too many items and duplicates const result = Strings.safeParse(["asfd", "asfd", "asfd", "asfd"]); expect(result.success).toEqual(false); if (!result.success) { expect(result.error.issues.length).toEqual(2); expect(result.error.issues[0].message).toEqual("Too many items 😡"); expect(result.error.issues[1].message).toEqual("No duplicates allowed."); } // Should pass with valid input const validArray = ["asfd", "qwer"]; expect(Strings.parse(validArray)).toEqual(validArray); }); test("should support async superRefine", async () => { const Strings = z.array(z.string()).superRefine(async (val, ctx) => { if (val.length > 3) { ctx.addIssue({ input: val, code: "too_big", origin: "array", maximum: 3, inclusive: true, message: "Too many items 😡", }); } if (val.length !== new Set(val).size) { ctx.addIssue({ input: val, code: "custom", message: `No duplicates allowed.`, }); } }); // Should fail with too many items and duplicates const result = await Strings.safeParseAsync(["asfd", "asfd", "asfd", "asfd"]); expect(result.success).toEqual(false); if (!result.success) { expect(result.error.issues.length).toEqual(2); } // Should pass with valid input const validArray = ["asfd", "qwer"]; await expect(Strings.parseAsync(validArray)).resolves.toEqual(validArray); }); test("should accept string as shorthand for custom error message", () => { const schema = z.string().superRefine((_, ctx) => { ctx.addIssue("bad stuff"); }); const result = schema.safeParse("asdf"); expect(result.success).toEqual(false); if (!result.success) { expect(result.error.issues).toHaveLength(1); expect(result.error.issues[0].message).toEqual("bad stuff"); } }); test("should respect fatal flag in superRefine", () => { const schema = z .string() .superRefine((val, ctx) => { if (val === "") { ctx.addIssue({ input: val, code: "custom", message: "foo", fatal: true, }); } }) .superRefine((val, ctx) => { if (val !== " ") { ctx.addIssue({ input: val, code: "custom", message: "bar", }); } }); const result = schema.safeParse(""); expect(result.success).toEqual(false); if (!result.success) { expect(result.error.issues.length).toEqual(1); expect(result.error.issues[0].message).toEqual("foo"); } }); }); describe("chained refinements", () => { test("should collect all validation errors when appropriate", () => { const objectSchema = z .object({ length: z.number(), size: z.number(), }) .refine(({ length }) => length > 5, { path: ["length"], message: "length greater than 5", }) .refine(({ size }) => size > 7, { path: ["size"], message: "size greater than 7", }); // Should fail with one error const r1 = objectSchema.safeParse({ length: 4, size: 9, }); expect(r1.success).toEqual(false); if (!r1.success) { expect(r1.error.issues.length).toEqual(1); expect(r1.error.issues[0].path).toEqual(["length"]); } // Should fail with two errors const r2 = objectSchema.safeParse({ length: 4, size: 3, }); expect(r2.success).toEqual(false); if (!r2.success) { expect(r2.error.issues.length).toEqual(2); } // Should pass with valid input const validData = { length: 6, size: 8, }; expect(objectSchema.parse(validData)).toEqual(validData); }); }); // Commented tests can be uncommented once type-checking issues are resolved /* describe("type refinement", () => { test("refinement type guard", () => { const validationSchema = z.object({ a: z.string().refine((s): s is "a" => s === "a"), }); type Input = z.input<typeof validationSchema>; type Schema = z.infer<typeof validationSchema>; expectTypeOf<Input["a"]>().not.toEqualTypeOf<"a">(); expectTypeOf<Input["a"]>().toEqualTypeOf<string>(); expectTypeOf<Schema["a"]>().toEqualTypeOf<"a">(); expectTypeOf<Schema["a"]>().not.toEqualTypeOf<string>(); }); test("superRefine - type narrowing", () => { type NarrowType = { type: string; age: number }; const schema = z .object({ type: z.string(), age: z.number(), }) .nullable() .superRefine((arg, ctx): arg is NarrowType => { if (!arg) { // still need to make a call to ctx.addIssue ctx.addIssue({ input: arg, code: "custom", message: "cannot be null", fatal: true, }); return false; } return true; }); expectTypeOf<z.infer<typeof schema>>().toEqualTypeOf<NarrowType>(); expect(schema.safeParse({ type: "test", age: 0 }).success).toEqual(true); expect(schema.safeParse(null).success).toEqual(false); }); test("chained mixed refining types", () => { type firstRefinement = { first: string; second: number; third: true }; type secondRefinement = { first: "bob"; second: number; third: true }; type thirdRefinement = { first: "bob"; second: 33; third: true }; const schema = z .object({ first: z.string(), second: z.number(), third: z.boolean(), }) .nullable() .refine((arg): arg is firstRefinement => !!arg?.third) .superRefine((arg, ctx): arg is secondRefinement => { expectTypeOf<typeof arg>().toEqualTypeOf<firstRefinement>(); if (arg.first !== "bob") { ctx.addIssue({ input: arg, code: "custom", message: "`first` property must be `bob`", }); return false; } return true; }) .refine((arg): arg is thirdRefinement => { expectTypeOf<typeof arg>().toEqualTypeOf<secondRefinement>(); return arg.second === 33; }); expectTypeOf<z.infer<typeof schema>>().toEqualTypeOf<thirdRefinement>(); }); }); */ test("when", () => { const schema = z .strictObject({ password: z.string().min(8), confirmPassword: z.string(), other: z.string(), }) .refine( (data) => { console.log("running check..."); console.log(data); console.log(data.password); return data.password === data.confirmPassword; }, { message: "Passwords do not match", path: ["confirmPassword"], when(payload) { if (payload.value === undefined) return false; if (payload.value === null) return false; // no issues with confirmPassword or password return payload.issues.every((iss) => iss.path?.[0] !== "confirmPassword" && iss.path?.[0] !== "password"); }, } ); expect(schema.safeParse(undefined)).toMatchInlineSnapshot(` { "error": [ZodError: [ { "expected": "object", "code": "invalid_type", "path": [], "message": "Invalid input: expected object, received undefined" } ]], "success": false, } `); expect(schema.safeParse(null)).toMatchInlineSnapshot(` { "error": [ZodError: [ { "expected": "object", "code": "invalid_type", "path": [], "message": "Invalid input: expected object, received null" } ]], "success": false, } `); expect( schema.safeParse({ password: "asdf", confirmPassword: "asdfg", other: "qwer", }) ).toMatchInlineSnapshot(` { "error": [ZodError: [ { "origin": "string", "code": "too_small", "minimum": 8, "inclusive": true, "path": [ "password" ], "message": "Too small: expected string to have >=8 characters" } ]], "success": false, } `); expect( schema.safeParse({ password: "asdf", confirmPassword: "asdfg", other: 1234, }) ).toMatchInlineSnapshot(` { "error": [ZodError: [ { "origin": "string", "code": "too_small", "minimum": 8, "inclusive": true, "path": [ "password" ], "message": "Too small: expected string to have >=8 characters" }, { "expected": "string", "code": "invalid_type", "path": [ "other" ], "message": "Invalid input: expected string, received number" } ]], "success": false, } `); });
Save
cmd:
run