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
7
votes
2 answers

At least one / minimum one field in Zod Schema Validation

I have implemented this code using JOI where the user has to send the userId and at least one of the keys from the body. How to implement the same using ZOD ?? params: Joi.object().keys({ userId: Joi.required().custom(objectId), }), body:…
Deepak Kumar
  • 73
  • 1
  • 1
  • 5
7
votes
1 answer

How to make an optional property with a default value in Zod

I'm using Zod and I want to define one schema for user input that has an optional field with a default value. However, it is being impossible for me to make the runtime behaviour match the inferred type. What I want, is the field to be optional, and…
Danielo515
  • 5,996
  • 4
  • 32
  • 66
6
votes
4 answers

How to create a type for file type `Record` in zod

I use input type="file" and instead of saving data in FileData I use just plain object and created a type for it: Record. It works well, but when I try to create a validator with zod – I don't get how to create the same type for…
Samat Zhetibaev
  • 482
  • 2
  • 5
  • 16
5
votes
1 answer

zod TypeError: Cannot read properties of undefined (reading '_parse')

I have a Vite library using Zod. I want to parse configurations and my folder structure is similiar to the configuration object structure. index.ts files always export all files in their own directory and everything from their subdirectories e.g.…
baitendbidz
  • 187
  • 3
  • 19
5
votes
0 answers

React-hook-form, Zod. What is the recommended way of handling image uploads in a form that supports both adding and editing?

What is the recommended way of handling image uploads in a form that supports both adding and editing of a product? Here is my current implementation with two options, is there any suggestion to improve it? Here is the first option when an image…
monotype
  • 217
  • 2
  • 10
5
votes
1 answer

Is there way to use typescript zod library to parse a class instance

I have been using Zod validation library with typescript for a little while and been loving it. This might be a very basic thing, but I am trying to figure out what would be best pattern to extend Zod Schema with class-like functionality. For the…
Gwydion
  • 252
  • 1
  • 7
5
votes
1 answer

Creating zod schema for generic interface

I've got this generic interface for paginated response: export interface PaginatedResponse { pageIndex: number; pageSize: number; totalCount: number; totalPages: number; items: Array; } And then I want to turn it in zod schema for…
ligowsky
  • 1,953
  • 4
  • 13
  • 15
5
votes
2 answers

Zod validation for phone numbers

I am new to using zod for valiadtion. I have read through the documentation but I can see anything on how to add a validation for a phone number. I used the email one to make sure a user enters a correct email, but how do I do the same for a phone…
5
votes
1 answer

ZOD [Error]: Schema with id 'Schema' already declared

I am new to Fastify and Typescript. I am working on adding Zod schemas for validation and I am getting this error: /app/node_modules/fastify/lib/schemas.js:32 throw new FST_ERR_SCH_ALREADY_PRESENT(id) ^ FastifyError [Error]: Schema…
ASR4
  • 545
  • 1
  • 8
  • 24
5
votes
1 answer

Array of self in Zod schema

I'd like to achieve the following: export const MediaResponseSchema = z.object({ mediaId: z.number(), childMedias: z.array(z.object(MediaResponseSchema)), }); I.e. the childMedia should be parsed as an array of the schema I'm declaring.
Max
  • 488
  • 8
  • 19
5
votes
1 answer

Custom validation of optional keys in zod

I'm currently evaluating the use of zod in my application and have a small problem when having to parse an object that can contain optional keys. I'm using .passthrough to allow the keys to stay in the object but would like to custom validate the…
doberkofler
  • 9,511
  • 18
  • 74
  • 126
5
votes
1 answer

wrap up zod validations for reuse

I'm working on a similar form to that of my coworker with some extra fields. Ive taken his zodObject and extended it with the extra fields. He's using a bunch of .refine calls to validate his form, but I wanted to wrap that logic and use it on mine…
Ashbury
  • 2,160
  • 3
  • 27
  • 52
5
votes
2 answers

How omit certain value from nested zod scheme?

I have the following zod schema, and in some cases there is a field I would like to omit from the schema entirely. I can't just make it optional. I suspect there is some way do it with zod directly. Is there a way to omit fields or to preprocess the…
Ksenia
  • 950
  • 6
  • 24
5
votes
1 answer

Make zod do top-level type checking on .parse() input

I understand that the point of zod is to parse untrusted input data and assert that it's of a type that matches your schema. But usually that data is coming in via web APIs that guarantee at least its top-level shape, like string or object. It seems…
davnicwil
  • 28,487
  • 16
  • 107
  • 123
4
votes
1 answer

Adding 'required_error' and 'invalid_type_error' properties to already created ZodTypes

I want to input an object with ZodTypes like z.string, z.number, z.boolean, any zodtypes, and when returning I want to return the same object but the ZodTypes will have added properties required_error and invalid_type_error. So the idea is to have…
docksdocks
  • 108
  • 9
1
2
3
21 22