colinhacks / zod

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

Add support for `example` #3596

Open maryam-saeidi opened 2 months ago

maryam-saeidi commented 2 months ago

In our project, we would like to use Zod in combination with zod-to-json-schema. One feature that we are missing is a parameter to pass an example value for a field.

Here is an example:

const apiSchema = {
  params: z.object({
    id: z.string({
      description: 'An identifier for the resource.',
      example: '9c235211-6834-11ea-a78c-6feb38a34414',
    }),
  }),
};

Which after conversion, it will look like this:


 {
    "name": "id",
    "in": "path",
    "required": true,
    "schema": {
       "type": "string",
       "example": "9c235211-6834-11ea-a78c-6feb38a34414",
    },
    "description": "An identifier for the resource."
 }

And here is the representation of this schema in Swagger:

maryam-saeidi commented 2 months ago

This PR implements this feature.