colinhacks / zod

TypeScript-first schema validation with static type inference
https://zod.dev
MIT License
33.78k stars 1.17k forks source link

Feature request: add .example to ZodType #2642

Open gerardolima opened 1 year ago

gerardolima commented 1 year ago

Motivation

Zod schemas can be leveraged to automatically generate API information (ie, nestjs-zod). This is very convenient because it allows defining the validation schema and the API documentation in a single place. As Zod does not have the concept of an .example value, these toolings rely on .default values to present sample values. Unfortunately, though, this breaks the semantics of .default, on .parse.

Goal

Create the new method .example() on ZodType.

Suggestion

NextThread commented 1 year ago

hi, I'd like to work on this

gerardolima commented 1 year ago

hi, I'd like to work on this

if this feat should be accepted to add to Zod, I'd be available to work on this too

olalonde commented 1 year ago

I'm using zod to validate my app's configuration and it could be useful to automatically generate a help message, e.g. display which values correspond to which environment variables.

I wonder if instead, it wouldn't be better to have something more generic like meta which other tools could use to retrieved metadata.

x.meta({
  example: "some example",
  help: "Some help message, e.g. this value corresponds to this ENV_VARIABLE"
}); 
x.meta()