Skip to content

Zod: required doesn't work with allOf #2306

@sannomiya7

Description

@sannomiya7

The output Zod schema has optional added to it.
Is my OpenAPI definition incorrect?

orval: 7.11.2
zod: 4.0.11

paths:
  /api/v1/users:
    post:
      operationId: createUser
      requestBody:
        required: true
        content:
          application/json:
            schema: { $ref: '#/components/schemas/UserCreate' }
      responses:
        ...

components:
  schemas:
    User:
      type: object
      properties:
        email:
          type: string
          format: email
          maxLength: 255
          description: email
        name:
          type: string
          maxLength: 255
          description: name

    UserCreate:
      allOf:
        - $ref: "#/components/schemas/User"
        - type: object
          required:
            - email
            - name

The output zod schema has optional

export const createUserBody = zod.object({
  "email": zod.email().max(createUserBodyEmailMax).optional().describe('email'),
  "name": zod.string().max(createUserBodyNameMax).optional().describe('name')
}).and(zod.object({

}))

The output typescript schema does not have optional

export type UserCreate = User & UserCreateAllOf & Required<Pick<User & UserCreateAllOf, 'username' | 'name'>>;

Metadata

Metadata

Assignees

No one assigned

    Labels

    help wantedExtra attention is neededworkaroundA workaround has been providedzodZod related issue

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions