openapi-zod-client icon indicating copy to clipboard operation
openapi-zod-client copied to clipboard

Validation not taken into account with `anyOf`

Open dmckeone opened this issue 1 year ago • 1 comments

Describe the bug

When using anyOf for an optional field, validation criteria are not generated.

Minimal reproduction

"first_name": {
   "type": "string",
  "minLength": 1
  "maxLength": 30
}

The above works, and produces:

.object({
    first_name: z.string().min(1).max(30),
  })

The below does not work:

"first_name": {
    "anyOf": [
        {
            "type": "string",
            "maxLength": 30,
            "minLength": 1
        },
        {
            "type": "null"
        }
    ],
    "default": null
}

and produces:

.object({
    first_name: z.union([z.string(), z.null()]),
  })

Expected behavior

"first_name": {
    "anyOf": [
        {
            "type": "string",
            "maxLength": 30,
            "minLength": 1
        },
        {
            "type": "null"
        }
    ],
    "default": null
}

should produce:

.object({
    first_name: z.union([z.string().min(1).max(30), z.null()]),
  })

dmckeone avatar Oct 05 '23 16:10 dmckeone

.object({
    first_name: z.string().min(1).max(30).nullable(),
  })

dror-trail avatar Jun 09 '24 13:06 dror-trail