Questions tagged [zod]

A JavaScript/TypeScript library for TypeScript-first schema validation with static type inference.

A JavaScript/TypeScript library for TypeScript-first schema validation with static type inference.

https://zod.dev/

332 questions
24
votes
3 answers

How to check confirm password with zod

How do I check for confirm password with zod?. I want to validate for confirm password with Zod. I want Zod to compare my password with comparePassword export const registerUSerSchema = z.object({ firstName: z.string(), lastName:…
Ushahemba Shir
  • 335
  • 1
  • 3
  • 12
20
votes
4 answers

Zod Schema: How to make a field optional OR have a minimum string contraint?

I have a field where I want the value to either be optional OR have the field have a minimum length of 4. I've tried the following: export const SocialsSchema = z.object({ myField: z.optional(z.string().min(4, "Please enter a valid…
Barry Michael Doyle
  • 9,333
  • 30
  • 83
  • 143
19
votes
3 answers

Why does Zod make all my schema fields optional?

I am using Zod inside my Express & TypeScript & Mongoose API project and when trying to validate my user input against the user schema it returns types conflicts: Argument of type '{ firstName?: string; lastName?: string; password?: string;…
Ali H. Kudeir
  • 756
  • 2
  • 9
  • 19
18
votes
2 answers

How to validate a string literal type using zod

I have this Type export type PaymentType = 'CHECK' | 'DIRECT DEPOSIT' | 'MONEY ORDER'; I want to validate this literal string type in zod. Currently, I have is as a string, but that wrong is not a string. I don't know what to put. const schema =…
Millenial2020
  • 2,465
  • 9
  • 38
  • 83
17
votes
1 answer

How do I create a zod object with dynamic keys?

We can create Zod object that validates an object against the keys that are defined in the schema, but I only want to validate if the key is a string not if the key == something In typescript we can achieve this by using Record; But…
Amjad sibili
  • 580
  • 1
  • 7
  • 15
17
votes
4 answers

Zod: create a schema using an existing type

I have an endpoint that should get a parameter method which should comply with the Axios type Method. How can I create a schema with Zod that validates that the value is using the type Schema? import { Method } from 'axios'; const Schema =…
Dotan
  • 6,602
  • 10
  • 34
  • 47
14
votes
1 answer

Creating a zod enum from an object

I have this object: const properties = [ { value: "entire_place", label: "The entire place" }, { value: "private_room", label: "A private room" }, { value: "shared_room", label: "A shared room" }, ] as const; I need to use it with zod in…
user19910212
14
votes
2 answers

Specify a Zod schema with a non-optional but possibly undefined field

Is it possible to define a Zod schema with a field that is possibly undefined, but non-optional. In TypeScript this is the difference between: interface IFoo1 { somefield: string | undefined; } interface IFoo2 { somefield?: string |…
Souperman
  • 5,057
  • 1
  • 14
  • 39
13
votes
1 answer

Do not allow extra properties with zod parse

I'm using zod for validation. It seems like if I define a schema and then parse() some input with some extra properties that aren't even in the schema, zod parses the input as valid but just removes those keys. import { z } from 'zod' const schema…
davnicwil
  • 28,487
  • 16
  • 107
  • 123
10
votes
2 answers

Typescript, losing Zod and tRPC types across monorepo projects, types result in any

I am in a bit of a weird situation. For the past 2 weeks I've been trying to debug as to why I am losing types between my projects inside a monorepo. My backend exposes the types that my client uses, but for some reason, certain types just don't get…
Nikola-Milovic
  • 1,393
  • 1
  • 12
  • 35
9
votes
1 answer

Conditional keys based on value of another key with Zod

I'm making a project with the TMDB API and trying to make it super type-safe to reinforce some of the TypeScript stuff I'm learning. I'm using Zod to describe the shape of the data returned by the API. However, I've noticed that depending on the…
tinkoh
  • 366
  • 4
  • 10
9
votes
2 answers

Zod: Create a primitive object from defaults

I'm pretty sure this exists, but I haven't been able to find anything about it despite some digging. Say that I have a zod Schema like such: const Person = zod.object({ name: z.string().default(''), age: z.number().nullable(); }); Is there…
Dragonsnap
  • 834
  • 10
  • 25
8
votes
1 answer

ZOD validation based on another field

Imagine I have an object like { field1: 'test', field2: 'test1' } How I can create the following validation: If field1 and field2 both are empty - it is not valid If field1 and field2 both are not empty - it is not valid Other cases are valid.
Kirill Novikov
  • 2,576
  • 4
  • 20
  • 33
8
votes
3 answers

Zod validator / validate image

I have a problem, I need to validate an image with zod. I am searching for 3 hours. I can't find out on validating the image? Can anyone help me out to fix this? zod must have image validate yes? const payloadSchema = z.object({ image:…
BasDriver
  • 171
  • 3
  • 9
8
votes
1 answer

how to use zod with validator.js

I have an application using zod but I'd like to use some methods from a different library (validator.js) zod documentation says: Check out validator.js for a bunch of other useful string validation functions. Not sure if that means this functions…
llermaly
  • 2,331
  • 2
  • 16
  • 29
1
2 3
21 22