astahmer / openapi-zod-client

Generate a zodios (typescript http client with zod validation) from an OpenAPI spec (json/yaml)

Home Page:openapi-zod-client.vercel.app

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Validation not taken into account with `anyOf`

dmckeone opened this issue · 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()]),
  })
.object({
    first_name: z.string().min(1).max(30).nullable(),
  })