zod icon indicating copy to clipboard operation
zod copied to clipboard

Get all possible ZodErrors from a Schema

Open Lawlzer opened this issue 2 years ago • 2 comments

Hello! I am creating an API framework that uses Zod to guarantee full type-safety, and the unique part in my framework is the ability to output "types" for every route, automatically. (This allows a frontend/developer to interact with the backend/API, and have 100% type-safety, automatically)

This type-safety includes errors that can come from the userInput being invalid.

I'm able to get the "input" from zod-to-ts, but this doesn't have anything for the expected "output" types, or errors.

Effectively, I'm not sure how to do any of the following. Any tips or advice would be appreciated. I'm not sure if it's even possible, so :)

const example = z.object({
	simple: z.string(), // How can I get the possible "z.string()" error?

	// How can I get the "default" value? "my-default"
	default: z.string().default('my-default'),

	// Doing it with messages wouldn't be a fun way to do it, but that's a backup method I could do (if absolutely necessary)
        // but... I'm not sure how to even get access to the message :P
	message: z.string().refine((value) => value.length > 10, { message: 'The length must be greater than 10.' }),

	// How can I get access to the "refine" errors?
	refinedToLength: z.string().refine((value) => value.length > 10),

	// The initial input is a String, but this isn't specified? Is it possible to specify this?
	// Additionally, how can I get the "input", AND the "output"?
	preprocessedToNumber: z.preprocess((val) => Number(val), z.number()), // todo I have no idea how this works

	// Ideally, it would work for a combination of these as well (preprocesses, transforms, refines, etc)
	multipleTransforms: z
		.string()
		.transform((value) => value.length)
		.transform((value) => value + 1),
});

The format of the errors doesn't really matter; I just need some way to know what all valid errors are :) Thanks in advance!

Lawlzer avatar Dec 05 '22 19:12 Lawlzer

So, Zod has a very limited number of errors for a reason.

Take for example Joi. Joi has like more than 30 error codes on the string schema only.

I'd say that's not hard to infer the origin of an error by looking at the issues array.

Theres no currently way of doing this programmatically though.

A map ZodType -> Possible errors should take you a little more than 30 minutes.

Do you want the programmatic way?

santosmarco-caribou avatar Dec 13 '22 01:12 santosmarco-caribou

Thanks ^ Yeah, when you mention that, that would be kind of ridiculous haha.

I think the only realistic way to solve this (for my case) will be to add the optional "message" property to each field.

I'm not entirely sure what you mean though, what's the "programmatic" way to do it?

Lawlzer avatar Dec 13 '22 05:12 Lawlzer

Has this issue been resolved?

If so, can I close it?

JacobWeisenburger avatar Dec 26 '22 13:12 JacobWeisenburger