Z
Zod8mo ago
zerokelvin

zerokelvin - Why is MySchema.parse making optio...

Why is MySchema.parse making optional all of the properties of my schema? How do I stop it from doing this? Stackblitz: https://stackblitz.com/edit/typescript-pnkeqy?file=index.ts,package.json
import { z } from 'zod';

export const CreateMessage = z.object({
personId: z.number(),
fullName: z.string(),
email: z.string().email(),
});

const data = CreateMessage.parse({
personId: 1,
fullName: 'John Doe',
email: 'john.doe@example.com',
});

console.log(data);

/*
The type of "data" above is:
const data: {
personId?: number;
fullName?: string;
email?: string;
}


But instead it should be:

const data: {
personId: number;
fullName: string;
email: string;
}
*/
import { z } from 'zod';

export const CreateMessage = z.object({
personId: z.number(),
fullName: z.string(),
email: z.string().email(),
});

const data = CreateMessage.parse({
personId: 1,
fullName: 'John Doe',
email: 'john.doe@example.com',
});

console.log(data);

/*
The type of "data" above is:
const data: {
personId?: number;
fullName?: string;
email?: string;
}


But instead it should be:

const data: {
personId: number;
fullName: string;
email: string;
}
*/
StackBlitz
TypeScript Zod Playground (forked) - StackBlitz
Blank starter project for building TypeScript apps.
2 Replies
Unknown User
Unknown User8mo ago
Message Not Public
Sign In & Join Server To View
zerokelvin
zerokelvin8mo ago
Ah, that's it! That fixed my issue, thanks @Sikari !!