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

Disable Zod for Specific Objects or Allow Arbitrary Values for Specific Objects?

I have a very large JS object I want to pass to trpc, through zod. Right now I just want to play around with the object a bit, before creating my zod schema. Is there a way to allow anything to be passed into zod (at least just in dev mode)? Or to…
antonwilhelm
  • 5,768
  • 4
  • 19
  • 45
0
votes
0 answers

Zod SafeParse not returning correct success state

I'm building a basic text input that checks to see if a password is valid. The condition is that the password must be 8 characters or longer, and must contain at least one number and special character. Here's the function below: const setPassword =…
Brandon-Perry
  • 366
  • 4
  • 18
0
votes
1 answer

How to explicitly skip checking part of the schema in zod

I would like to understand if and how it is possible to skip validating parts of a schema in zod? In the following example, I would like to validate the foo schema to make sure that the object contains a property id of type number and a property…
doberkofler
  • 9,511
  • 18
  • 74
  • 126
0
votes
1 answer

Force object type based on an interface

I want to force a Zod type based on a TypeScript interface: import { z } from 'zod'; interface Foo { id: string; count: number; } // const t = z.object({ count: z.number(), id: z.string() }) How can I do it? The…
Bazinga
  • 10,716
  • 6
  • 38
  • 63
0
votes
0 answers

TypeScript generic function is not working with nested Type object

https://codesandbox.io/s/otherusersmap-typescript-problem-gpkbhf?file=/src/index.ts So my Redux reducer uses a bunch of TypeScript records to make fetching information faster. So, I created a general function for mapping an object with its id.…
Brandon-Perry
  • 366
  • 4
  • 18
0
votes
1 answer

How to customize the JSON serialization in TRPC|zod

Is there a way for me to show TRPC how to serialize bigint? I'm turning BigInt into JSON by BigInt.prototype.toJSON = function() { return this.toString() } And now the issue is when TRPC and Zod validate the big int it fails because its a…
0
votes
0 answers

In Zod add immutable defaults to an object

In Joi you can do is_email_verified: Joi.forbidden().default(false). This prevents is_email_verified from being present when validating but afterwards it will add in the property with the default value. In zod I want to do the same thing e.g. export…
OultimoCoder
  • 244
  • 2
  • 7
  • 24
0
votes
2 answers

How to make a generic getter like z.infer<> in Typescript

I've been learning Typescript for a few months and i started to discover zod recently, and i wonder how to build such a util getter such as z.infer<>, z.input<> or even z.output<> What i understand is that z is a class that has a getter "method"…
sqmasep
  • 11
  • 2
0
votes
0 answers

How to Use z.discriminatedUnion() in TRPC?

Just getting used to trpc. Say I have a standard procedure: update: t.procedure .input(z.object({ id: z.string(), type: z.string(), content: z.object({ title: z.string() }).nullish() }).nullish()) Now what I am interested…
antonwilhelm
  • 5,768
  • 4
  • 19
  • 45
0
votes
1 answer

Validating optional fields in react using Zod

Using react-hook-form, and zod within a Next.js project. Trying to get .optional() to work. It works with simple strings. See the schema below. //works const FormSchema = z.object({ website: z.string().optional() }); But it does not work when…
OWolf
  • 5,012
  • 15
  • 58
  • 93
0
votes
1 answer

Does yup or zod have a method to determine is schema is yup-schema or zod-schema?

We have a project where we are using yup and now zod for validation, in parallel. We need a way to reliably distinguish one schema from another. Are there any methods in either of these to detect this? Something like: yup.isYupSchema() or…
jayarjo
  • 16,124
  • 24
  • 94
  • 138
0
votes
2 answers

React Hook Form's register function does not work when using custom components

I'm refactoring some code in my app and I noticed that when I moved my input element to be its own reusable component, hook-form's register function stopped working. This can be fixed if you use the plain input element, but I would like to use my…
Jeremy
  • 1,447
  • 20
  • 40
0
votes
0 answers

Zod compose a String and an Object parsers

I have a parser stringParser that would take a string (of a specific format, in my case query-like k1=v1&k2=v2) and with transform (OR preprocess, which is a lot less desirable because of the bug https://github.com/colinhacks/zod/issues/1460) and…
Igor Loskutov
  • 2,157
  • 2
  • 20
  • 33
0
votes
0 answers

How to transform two strings into key/value pair Zod validation from React Form

I want to convert these two strings into an object with key/value pair and being validated in zod: {authors.allAuthors.map((author) => { return (
Marco Tidu
  • 11
  • 2
0
votes
1 answer

How do I use Zod's isEmail?

Not sure how isEmail is intended to be used? .mutation("add", { input: z.object({ email: z.string().min(1).isEmail(), <----- ? this doesn't seem to work / throws an error }), async resolve({ ctx, input }) { let user = await…
antonwilhelm
  • 5,768
  • 4
  • 19
  • 45
1 2 3
21
22