/opt/canhelp/node_modules/zod/src/v4/classic/tests
NameSizeModeActions
anyunknown.test.ts6390644editdlrm
array.test.ts72840644editdlrm
assignability.test.ts67900644editdlrm
async-parsing.test.ts123110644editdlrm
async-refinements.test.ts18670644editdlrm
base.test.ts1790644editdlrm
bigint.test.ts19560644editdlrm
brand.test.ts22060644editdlrm
catch.test.ts72240644editdlrm
coalesce.test.ts7430644editdlrm
coerce.test.ts77360644editdlrm
continuability.test.ts74780644editdlrm
custom.test.ts12150644editdlrm
date.test.ts9620644editdlrm
datetime.test.ts115110644editdlrm
default.test.ts77170644editdlrm
description.test.ts12940644editdlrm
discriminated-unions.test.ts168100644editdlrm
enum.test.ts84880644editdlrm
error-utils.test.ts125720644editdlrm
error.test.ts195020644editdlrm
file.test.ts24600644editdlrm
firstparty.test.ts29770644editdlrm
function.test.ts62990644editdlrm
generics.test.ts21620644editdlrm
index.test.ts275620644editdlrm
instanceof.test.ts10520644editdlrm
intersection.test.ts47630644editdlrm
json.test.ts33090644editdlrm
lazy.test.ts47560644editdlrm
literal.test.ts24780644editdlrm
map.test.ts48170644editdlrm
nan.test.ts6450644editdlrm
nested-refine.test.ts36230644editdlrm
nonoptional.test.ts24050644editdlrm
nullable.test.ts5910644editdlrm
number.test.ts78270644editdlrm
object.test.ts159990644editdlrm
optional.test.ts41300644editdlrm
partial.test.ts48350644editdlrm
pickomit.test.ts44220644editdlrm
pipe.test.ts18480644editdlrm
prefault.test.ts10070644editdlrm
preprocess.test.ts69590644editdlrm
primitive.test.ts76490644editdlrm
promise.test.ts23150644editdlrm
prototypes.test.ts4980644editdlrm
readonly.test.ts117770644editdlrm
record.test.ts81180644editdlrm
recursive-types.test.ts73350644editdlrm
refine.test.ts147740644editdlrm
registries.test.ts59990644editdlrm
set.test.ts55720644editdlrm
standard-schema.test.ts13690644editdlrm
string-formats.test.ts30680644editdlrm
string.test.ts641240644editdlrm
stringbool.test.ts23330644editdlrm
template-literal.test.ts348360644editdlrm
to-json-schema.test.ts592760644editdlrm
transform.test.ts60000644editdlrm
tuple.test.ts47020644editdlrm
union.test.ts24970644editdlrm
validations.test.ts67360644editdlrm
void.test.ts3060644editdlrm
Edit: /opt/canhelp/node_modules/zod/src/v4/classic/tests/coerce.test.ts (7736B)
import { expect, expectTypeOf, test } from "vitest"; import * as z from "zod/v4"; test("string coercion", () => { const schema = z.coerce.string(); expect(schema.parse("sup")).toEqual("sup"); expect(schema.parse("")).toEqual(""); expect(schema.parse(12)).toEqual("12"); expect(schema.parse(0)).toEqual("0"); expect(schema.parse(-12)).toEqual("-12"); expect(schema.parse(3.14)).toEqual("3.14"); expect(schema.parse(BigInt(15))).toEqual("15"); expect(schema.parse(Number.NaN)).toEqual("NaN"); expect(schema.parse(Number.POSITIVE_INFINITY)).toEqual("Infinity"); expect(schema.parse(Number.NEGATIVE_INFINITY)).toEqual("-Infinity"); expect(schema.parse(true)).toEqual("true"); expect(schema.parse(false)).toEqual("false"); expect(schema.parse(null)).toEqual("null"); expect(schema.parse(undefined)).toEqual("undefined"); expect(schema.parse({ hello: "world!" })).toEqual("[object Object]"); expect(schema.parse(["item", "another_item"])).toEqual("item,another_item"); expect(schema.parse([])).toEqual(""); expect(schema.parse(new Date("2022-01-01T00:00:00.000Z"))).toEqual(new Date("2022-01-01T00:00:00.000Z").toString()); }); test("number coercion", () => { const schema = z.coerce.number(); expect(schema.parse("12")).toEqual(12); expect(schema.parse("0")).toEqual(0); expect(schema.parse("-12")).toEqual(-12); expect(schema.parse("3.14")).toEqual(3.14); expect(schema.parse("")).toEqual(0); expect(() => schema.parse("NOT_A_NUMBER")).toThrow(); // z.ZodError expect(schema.parse(12)).toEqual(12); expect(schema.parse(0)).toEqual(0); expect(schema.parse(-12)).toEqual(-12); expect(schema.parse(3.14)).toEqual(3.14); expect(schema.parse(BigInt(15))).toEqual(15); expect(() => schema.parse(Number.NaN)).toThrow(); // z.ZodError // expect(schema.parse(Number.POSITIVE_INFINITY)).toEqual(Number.POSITIVE_INFINITY); // expect(schema.parse(Number.NEGATIVE_INFINITY)).toEqual(Number.NEGATIVE_INFINITY); expect(schema.parse(true)).toEqual(1); expect(schema.parse(false)).toEqual(0); expect(schema.parse(null)).toEqual(0); expect(() => schema.parse(undefined)).toThrow(); // z.ZodError expect(() => schema.parse({ hello: "world!" })).toThrow(); // z.ZodError expect(() => schema.parse(["item", "another_item"])).toThrow(); // z.ZodError expect(schema.parse([])).toEqual(0); expect(schema.parse(new Date(1670139203496))).toEqual(1670139203496); }); test("boolean coercion", () => { const schema = z.coerce.boolean(); expect(schema.parse("true")).toEqual(true); expect(schema.parse("false")).toEqual(true); expect(schema.parse("0")).toEqual(true); expect(schema.parse("1")).toEqual(true); expect(schema.parse("")).toEqual(false); expect(schema.parse(1)).toEqual(true); expect(schema.parse(0)).toEqual(false); expect(schema.parse(-1)).toEqual(true); expect(schema.parse(3.14)).toEqual(true); expect(schema.parse(BigInt(15))).toEqual(true); expect(schema.parse(Number.NaN)).toEqual(false); expect(schema.parse(Number.POSITIVE_INFINITY)).toEqual(true); expect(schema.parse(Number.NEGATIVE_INFINITY)).toEqual(true); expect(schema.parse(true)).toEqual(true); expect(schema.parse(false)).toEqual(false); expect(schema.parse(null)).toEqual(false); expect(schema.parse(undefined)).toEqual(false); expect(schema.parse({ hello: "world!" })).toEqual(true); expect(schema.parse(["item", "another_item"])).toEqual(true); expect(schema.parse([])).toEqual(true); expect(schema.parse(new Date(1670139203496))).toEqual(true); }); test("bigint coercion", () => { const schema = z.coerce.bigint(); expect(schema.parse("5")).toEqual(BigInt(5)); expect(schema.parse("0")).toEqual(BigInt(0)); expect(schema.parse("-5")).toEqual(BigInt(-5)); expect(() => schema.parse("3.14")).toThrow(); // not a z.ZodError! expect(schema.parse("")).toEqual(BigInt(0)); expect(() => schema.parse("NOT_A_NUMBER")).toThrow(); // not a z.ZodError! expect(schema.parse(5)).toEqual(BigInt(5)); expect(schema.parse(0)).toEqual(BigInt(0)); expect(schema.parse(-5)).toEqual(BigInt(-5)); expect(() => schema.parse(3.14)).toThrow(); // not a z.ZodError! expect(schema.parse(BigInt(5))).toEqual(BigInt(5)); expect(() => schema.parse(Number.NaN)).toThrow(); // not a z.ZodError! expect(() => schema.parse(Number.POSITIVE_INFINITY)).toThrow(); // not a z.ZodError! expect(() => schema.parse(Number.NEGATIVE_INFINITY)).toThrow(); // not a z.ZodError! expect(schema.parse(true)).toEqual(BigInt(1)); expect(schema.parse(false)).toEqual(BigInt(0)); expect(() => schema.parse(null)).toThrow(); // not a z.ZodError! expect(() => schema.parse(undefined)).toThrow(); // not a z.ZodError! expect(() => schema.parse({ hello: "world!" })).toThrow(); // not a z.ZodError! expect(() => schema.parse(["item", "another_item"])).toThrow(); // not a z.ZodError! expect(schema.parse([])).toEqual(BigInt(0)); expect(schema.parse(new Date(1670139203496))).toEqual(BigInt(1670139203496)); }); test("date coercion", () => { const schema = z.coerce.date(); expect(schema.parse(new Date().toDateString())).toBeInstanceOf(Date); expect(schema.parse(new Date().toISOString())).toBeInstanceOf(Date); expect(schema.parse(new Date().toUTCString())).toBeInstanceOf(Date); expect(schema.parse("5")).toBeInstanceOf(Date); expect(schema.parse("2000-01-01")).toBeInstanceOf(Date); // expect(schema.parse("0")).toBeInstanceOf(Date); // expect(schema.parse("-5")).toBeInstanceOf(Date); // expect(schema.parse("3.14")).toBeInstanceOf(Date); expect(() => schema.parse("")).toThrow(); // z.ZodError expect(() => schema.parse("NOT_A_DATE")).toThrow(); // z.ZodError expect(schema.parse(5)).toBeInstanceOf(Date); expect(schema.parse(0)).toBeInstanceOf(Date); expect(schema.parse(-5)).toBeInstanceOf(Date); expect(schema.parse(3.14)).toBeInstanceOf(Date); expect(() => schema.parse(BigInt(5))).toThrow(); // not a z.ZodError! expect(() => schema.parse(Number.NaN)).toThrow(); // z.ZodError expect(() => schema.parse(Number.POSITIVE_INFINITY)).toThrow(); // z.ZodError expect(() => schema.parse(Number.NEGATIVE_INFINITY)).toThrow(); // z.ZodError expect(schema.parse(true)).toBeInstanceOf(Date); expect(schema.parse(false)).toBeInstanceOf(Date); expect(schema.parse(null)).toBeInstanceOf(Date); expect(() => schema.parse(undefined)).toThrow(); // z.ZodError expect(() => schema.parse({ hello: "world!" })).toThrow(); // z.ZodError expect(() => schema.parse(["item", "another_item"])).toThrow(); // z.ZodError expect(() => schema.parse([])).toThrow(); // z.ZodError expect(schema.parse(new Date())).toBeInstanceOf(Date); }); // test("template literal coercion", () => { // const schema = z.coerce // .templateLiteral() // .interpolated(z.number().finite()) // .interpolated( // z.enum(["px", "em", "rem", "vh", "vw", "vmin", "vmax"]).optional() // ); // expect(schema.parse(300)).toEqual("300"); // expect(schema.parse(BigInt(300))).toEqual("300"); // expect(schema.parse("300")).toEqual("300"); // expect(schema.parse("300px")).toEqual("300px"); // expect(schema.parse("300em")).toEqual("300em"); // expect(schema.parse("300rem")).toEqual("300rem"); // expect(schema.parse("300vh")).toEqual("300vh"); // expect(schema.parse("300vw")).toEqual("300vw"); // expect(schema.parse("300vmin")).toEqual("300vmin"); // expect(schema.parse("300vmax")).toEqual("300vmax"); // expect(schema.parse(["300px"])).toEqual("300px"); // }); test("override input type", () => { const a = z.coerce.string(); type input = z.input; expectTypeOf().toEqualTypeOf(); type output = z.infer; expectTypeOf().toEqualTypeOf(); });