colinhacks / zod

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

Support .examples() #1439

Open kibertoad opened 2 years ago

kibertoad commented 2 years ago

JSON Schema supports examples keyword (see https://json-schema.org/draft-06/json-schema-release-notes.html) which is helpful for providing examples of a given schema. While not used for validation, it is very valuable for JSON Schema and OpenAPI documents that could be derived from zod schemas.

Zod already supports .describe(description) command, examples command could work very similarly (.examples(arrayOfExamples))

Nice-to-have - OpenAPI generally describes examples as records (see https://swagger.io/docs/specification/adding-examples/), so supporting any[] is preferable to cover all possible cases.

renatoargh commented 2 years ago

Just a small addition to @kibertoad suggestion; I think that would be nice to somehow have examples validated against the schema, to prevent wrong examples from being provided

kibertoad commented 2 years ago

@colinhacks Ping? Any thoughts on this?

StephanMeijer commented 1 year ago

I would really appreciate this. functionality like setExamples(examples), addExample(example) and a property like .examples would help within multiple implementations of webframeworks without them all having to do the same thing: adding meta externally.

StephanMeijer commented 1 year ago

@kibertoad I created a more formal proposal and a Pull Request. Curious about your opinion.

huypham50 commented 9 months ago

Bump, I think this is a good feature to bridge the gap between zod and openapi

ilya-levin-lokalise commented 4 months ago

Bump, would be useful :+1: