Skip to content

Zod: Strict with allOf OpenAPI is not producing usable zod schemas #2520

@IP-In

Description

@IP-In

Hello,
Using orval to generate zod schemas, using the option 'strict' for complex bodies is applying strict to individual part of the schema but not on the final one.

Reproduction

orval: 7.15.0
node version: 22.21.0

https://github.com/IP-In/orval-zod-bug-report

In openapi.yaml, the request POST - /fish has it's body which is the merge of 2 items
A. object with 1 mandatory property 'name'
B. object with 1 mandatory property 'swimming'

openapi: 3.1.0
info:
  title: Random API
  version: 1.0.0
paths:
  /fish:
    post:
      requestBody:
        content:
          application/json:
            schema:
              allOf:
                - type: object
                  required:
                    - name
                  properties:
                    name:
                      type: string
                - type: object
                  required:
                    - swimming
                  properties:
                    swimming:
                      type: boolean
...

npm run orval
Generate the zod schema
orval.config.js

...
output: {
			client: 'zod',
			target: './generated',
			override: {
				zod: {
					strict: {
						response: true,
						query: true,
						param: true,
						header: false,
						body: true
					}
				}
			}
		}
...

npm run test
Try to parse {name: "Nemo", swimming: true} which gives the following

Image

Expected

A single object is generated with the combination objects with the strict option.

export const postFishBody = zod.object({
    "name": zod.string(),
    "swimming": zod.boolean()
}).strict();

I couldn't find an alternative and Zod has already it's opinion on this use case it seems.
colinhacks/zod#1284

Actual

export const postFishBody = zod.object({
  "name": zod.string()
}).strict().and(zod.object({
  "swimming": zod.boolean()
}).strict())

Metadata

Metadata

Assignees

Labels

zodZod related issue

Type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions