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
4
votes
1 answer

Is there a way to use Zod to validate that a number has up to 2 decimal digits?

I have an object with a numeric property. I'd like to make sure that the number has only up to 2 decimal digit. e.g: 1 // good 1.1 // good 1.11 // good 1.111 //bad Is there way to do that? Looked at Zod's documentation and searched the web. Found…
Uri
  • 43
  • 1
  • 4
4
votes
1 answer

zod enum and object as const

I have the following object that is autogenerated export const ReportDimensions = { CHANNELS: 'CHANNELS', DAY: 'DAY', DOW: 'DOW', MONTH: 'MONTH', WEEK: 'WEEK' } as const; I would like to use zod, and allow only value that are in this…
Bobby
  • 4,372
  • 8
  • 47
  • 103
4
votes
2 answers

How to write a zod validator where the inferred type of a property is number|undefined?

Some definitions exist for an API (types are generated using protocol buffers). I'd rather not touch these. One of these types, lets call it SomeInterfaceOutOfMyControl has a property that is union type undefined. Like so: interface…
birgersp
  • 3,909
  • 8
  • 39
  • 79
4
votes
2 answers

How to transform object to array before parsing in Zod

I do have an external URL endpoint that returns an array of field object when it is more than 2 and an object when there is only one, see the snippet below: Return when the field count is one: { "fields": { "fullName": "fieldFullname", "type":…
xTchan
  • 75
  • 1
  • 6
4
votes
2 answers

Zod error handling with custom error messages on enums

I want to validate a gender field with Zod using z.nativeEnum(), But my custom error messages don't apply: gender: z.nativeEnum(Gender, { invalid_type_error: 'Le sexe doit être homme ou femme.', required_error: 'Le sexe est…
Ismail_Aj
  • 332
  • 1
  • 4
  • 17
4
votes
2 answers

What is the difference between z.optional(z.string()) and z.ostring() of zod?

To make any schema optional, I've seen that zod provides two methods: z.optional(z.string()) and z.ostring(). I wonder what is the difference between them? And what should I use for most cases?
Veewoo
  • 65
  • 4
4
votes
1 answer

Infer type from key of object inside an array Zod

So I'd like to grab the type from a key of an object within an array in Zod. That array is also nested within an object, just to make things extra difficult. This is an abstract view of the problem I'm having: const obj = z.object({ nestedArray:…
Herbie Vine
  • 1,643
  • 3
  • 17
  • 32
4
votes
1 answer

zod (or toZod): how to model "type" field in discriminated union

I have this type: export interface ConnectorForModel { _type: "connector.for.model", connectorDefinitionId: string } I want to model is as a zod schema. Actually I am using toZod, like this: export const ConnectorForModelZod:…
Mike Hogan
  • 9,933
  • 9
  • 41
  • 71
3
votes
1 answer

Zod validation schema make field required based on another array field

I have a Zod validation schema with an array field and a string field: const zodSchema = z.object({ goals: z.array(z.string()).nonempty("At least one goal is required"), goals_other: z.string(), }); How do I make the goals_other field required…
SevenSouls
  • 539
  • 3
  • 12
3
votes
1 answer

How to iterate over properties and get the types in Zod?

Say you have a basic system like this in Zod: import { z } from 'zod' const Post = z.object({ title: z.string(), }) const User = z.object({ name: z.string(), email: z.string().optional(), posts: z.array(Post), loginCount:…
Lance
  • 75,200
  • 93
  • 289
  • 503
3
votes
1 answer

Zod - Property 'infer' does not exist on type 'typeof import(.../node_modules/zod/lib/external)

I got the Typescript error: Property 'infer' does not exist on type 'typeof import(.../node_modules/zod/lib/external) on the code: const CampaignForm = z.infer; Looking at the docs and node_modules the property is there.
Itay Tur
  • 683
  • 1
  • 15
  • 40
3
votes
0 answers

How to parse output zod schema without redeclaring?

When I have this schema export const Schema = z .object({ cover_image_path: z.string().nullish(), }) .transform((prev) => ({ coverImagePath: prev.cover_image_path, })); I can do Schema.parse(input); What if I want to do something…
Coding Edgar
  • 1,285
  • 1
  • 8
  • 22
3
votes
1 answer

Is it possible to chain regex expressions in Zod?

I was trying to chain multiple regex expressions for a single input field. Although it checked for every regex expression but only the error message for the first regex expression was being displayed on the screen. Is it even possible to chain regex…
Zerodha
  • 31
  • 1
  • 3
3
votes
1 answer

Can I validate an exact value with zod?

Validating a string can be done with a regex. That's easy. const myString = z.string().regex(/A string/); But what about other data types? I guess the following could work for number, but it doesn't seem idiomatic. const myNumber =…
Stephen Horvath
  • 5,188
  • 3
  • 24
  • 31
3
votes
1 answer

Email validation with zod

I have an email input and i want to validate that the user entered a specific email "abcd@fg.com" and if not to show specific error message "This email is not in our database". I am using zod validation to do that, but how can it be done? const…
1 2
3
21 22