Some checks failed
Build & Deploy / 🔍 Prepare (push) Successful in 20s
Build & Deploy / 🧪 QA (push) Failing after 34s
Build & Deploy / 🏗️ Build (push) Has started running
Build & Deploy / 🚀 Deploy (push) Has been cancelled
Build & Deploy / 🧪 Smoke Test (push) Has been cancelled
Build & Deploy / ⚡ Lighthouse (push) Has been cancelled
Build & Deploy / 🔔 Notify (push) Has been cancelled
148 lines
4.7 KiB
Plaintext
148 lines
4.7 KiB
Plaintext
import { expect, expectTypeOf, test } from "vitest";
|
|
import * as z from "zod/v4";
|
|
|
|
const nested = z.object({
|
|
name: z.string(),
|
|
age: z.number(),
|
|
outer: z.object({
|
|
inner: z.string(),
|
|
}),
|
|
array: z.array(z.object({ asdf: z.string() })),
|
|
});
|
|
|
|
test("shallow inference", () => {
|
|
const shallow = nested.partial();
|
|
type shallow = z.infer<typeof shallow>;
|
|
|
|
expectTypeOf<shallow>().toEqualTypeOf<{
|
|
name?: string | undefined;
|
|
age?: number | undefined;
|
|
outer?: { inner: string } | undefined;
|
|
array?: { asdf: string }[] | undefined;
|
|
}>();
|
|
});
|
|
|
|
test("shallow partial parse", () => {
|
|
const shallow = nested.partial();
|
|
shallow.parse({});
|
|
shallow.parse({
|
|
name: "asdf",
|
|
age: 23143,
|
|
});
|
|
});
|
|
|
|
test("required", () => {
|
|
const object = z.object({
|
|
name: z.string(),
|
|
age: z.number().optional(),
|
|
field: z.string().optional().default("asdf"),
|
|
nullableField: z.number().nullable(),
|
|
nullishField: z.string().nullish(),
|
|
});
|
|
|
|
const requiredObject = object.required();
|
|
expect(requiredObject.shape.name).toBeInstanceOf(z.ZodNonOptional);
|
|
expect(requiredObject.shape.name.unwrap()).toBeInstanceOf(z.ZodString);
|
|
expect(requiredObject.shape.age).toBeInstanceOf(z.ZodNonOptional);
|
|
expect(requiredObject.shape.age.unwrap()).toBeInstanceOf(z.ZodOptional);
|
|
expect(requiredObject.shape.field).toBeInstanceOf(z.ZodNonOptional);
|
|
expect(requiredObject.shape.field.unwrap()).toBeInstanceOf(z.ZodDefault);
|
|
expect(requiredObject.shape.nullableField).toBeInstanceOf(z.ZodNonOptional);
|
|
expect(requiredObject.shape.nullableField.unwrap()).toBeInstanceOf(z.ZodNullable);
|
|
expect(requiredObject.shape.nullishField).toBeInstanceOf(z.ZodNonOptional);
|
|
expect(requiredObject.shape.nullishField.unwrap()).toBeInstanceOf(z.ZodOptional);
|
|
expect(requiredObject.shape.nullishField.unwrap().unwrap()).toBeInstanceOf(z.ZodNullable);
|
|
});
|
|
|
|
test("required inference", () => {
|
|
const object = z.object({
|
|
name: z.string(),
|
|
age: z.number().optional(),
|
|
field: z.string().optional().default("asdf"),
|
|
nullableField: z.number().nullable(),
|
|
nullishField: z.string().nullish(),
|
|
});
|
|
|
|
const requiredObject = object.required();
|
|
|
|
type required = z.infer<typeof requiredObject>;
|
|
type expected = {
|
|
name: string;
|
|
age: number;
|
|
field: string;
|
|
nullableField: number | null;
|
|
nullishField: string | null;
|
|
};
|
|
expectTypeOf<expected>().toEqualTypeOf<required>();
|
|
});
|
|
|
|
test("required with mask", () => {
|
|
const object = z.object({
|
|
name: z.string(),
|
|
age: z.number().optional(),
|
|
field: z.string().optional().default("asdf"),
|
|
country: z.string().optional(),
|
|
});
|
|
|
|
const requiredObject = object.required({ age: true });
|
|
expect(requiredObject.shape.name).toBeInstanceOf(z.ZodString);
|
|
expect(requiredObject.shape.age).toBeInstanceOf(z.ZodNonOptional);
|
|
expect(requiredObject.shape.field).toBeInstanceOf(z.ZodDefault);
|
|
expect(requiredObject.shape.country).toBeInstanceOf(z.ZodOptional);
|
|
});
|
|
|
|
test("required with mask -- ignore falsy values", () => {
|
|
const object = z.object({
|
|
name: z.string(),
|
|
age: z.number().optional(),
|
|
field: z.string().optional().default("asdf"),
|
|
country: z.string().optional(),
|
|
});
|
|
|
|
// @ts-expect-error
|
|
const requiredObject = object.required({ age: true, country: false });
|
|
expect(requiredObject.shape.name).toBeInstanceOf(z.ZodString);
|
|
expect(requiredObject.shape.age).toBeInstanceOf(z.ZodNonOptional);
|
|
expect(requiredObject.shape.field).toBeInstanceOf(z.ZodDefault);
|
|
expect(requiredObject.shape.country).toBeInstanceOf(z.ZodOptional);
|
|
});
|
|
|
|
test("partial with mask", async () => {
|
|
const object = z.object({
|
|
name: z.string(),
|
|
age: z.number().optional(),
|
|
field: z.string().optional().default("asdf"),
|
|
country: z.string(),
|
|
});
|
|
|
|
const masked = object.partial({ age: true, field: true, name: true }).strict();
|
|
|
|
expect(masked.shape.name).toBeInstanceOf(z.ZodOptional);
|
|
expect(masked.shape.age).toBeInstanceOf(z.ZodOptional);
|
|
expect(masked.shape.field).toBeInstanceOf(z.ZodOptional);
|
|
expect(masked.shape.country).toBeInstanceOf(z.ZodString);
|
|
|
|
masked.parse({ country: "US" });
|
|
await masked.parseAsync({ country: "US" });
|
|
});
|
|
|
|
test("partial with mask -- ignore falsy values", async () => {
|
|
const object = z.object({
|
|
name: z.string(),
|
|
age: z.number().optional(),
|
|
field: z.string().optional().default("asdf"),
|
|
country: z.string(),
|
|
});
|
|
|
|
// @ts-expect-error
|
|
const masked = object.partial({ name: true, country: false }).strict();
|
|
|
|
expect(masked.shape.name).toBeInstanceOf(z.ZodOptional);
|
|
expect(masked.shape.age).toBeInstanceOf(z.ZodOptional);
|
|
expect(masked.shape.field).toBeInstanceOf(z.ZodDefault);
|
|
expect(masked.shape.country).toBeInstanceOf(z.ZodString);
|
|
|
|
masked.parse({ country: "US" });
|
|
await masked.parseAsync({ country: "US" });
|
|
});
|