zod icon indicating copy to clipboard operation
zod copied to clipboard

Support .examples()

Open kibertoad opened this issue 3 years ago • 7 comments
trafficstars

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.

kibertoad avatar Sep 29 '22 19:09 kibertoad

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

renatoargh avatar Sep 30 '22 14:09 renatoargh

@colinhacks Ping? Any thoughts on this?

kibertoad avatar Oct 20 '22 18:10 kibertoad

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 avatar Jun 06 '23 11:06 StephanMeijer

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

StephanMeijer avatar Jul 05 '23 22:07 StephanMeijer

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

huypham50 avatar Feb 20 '24 01:02 huypham50

Bump, would be useful :+1:

ilya-levin-lokalise avatar Jul 03 '24 09:07 ilya-levin-lokalise