Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: generate zod constants for oneOf schemas #1618

Merged

Conversation

yoshi2no
Copy link
Contributor

@yoshi2no yoshi2no commented Sep 8, 2024

Status

READY

Description

close #1594

This PR adds the capability to generate Zod constants when handling oneOf schemas in the OpenAPI specification.

before

/**
 * Generated by orval v7.1.0 🍺
 * Do not edit manually.
 * Swagger Petstore
 * OpenAPI spec version: 1.0.0
 */
import { z as zod } from "zod";

/**
 * @summary Create a pet
 */
export const createPetsBodyPasswordMin = 8;

export const createPetsBody = zod.object({
  login: zod
    .string()
    .min(createPetsBodyLoginMinOne)
    .max(createPetsBodyLoginMaxOne)
    .or(zod.string().email()),
  password: zod.string().min(createPetsBodyPasswordMin),
  remember: zod.boolean(),
});

after

/**
 * Generated by orval v7.1.0 🍺
 * Do not edit manually.
 * Swagger Petstore
 * OpenAPI spec version: 1.0.0
 */
import { z as zod } from "zod";

/**
 * @summary Create a pet
 */
export const createPetsBodyLoginMinOne = 18; // generated!
export const createPetsBodyLoginMaxOne = 18; // generated!
export const createPetsBodyPasswordMin = 8;

export const createPetsBody = zod.object({
  login: zod
    .string()
    .min(createPetsBodyLoginMinOne)
    .max(createPetsBodyLoginMaxOne)
    .or(zod.string().email()),
  password: zod.string().min(createPetsBodyPasswordMin),
  remember: zod.boolean(),
});

Related PRs

Todos

  • Tests
  • Documentation
  • Changelog Entry (unreleased)

Steps to Test or Reproduce

  1. Update orval.config.js with the OpenAPI spec and client configuration.
  2. Ensure the oneOf schema definitions generate the correct constants for validation rules.
  3. Verify that the generated constants match the values specified in the OpenAPI schema.
// orval.config.js
import { defineConfig } from "orval";

export default defineConfig({
  petstore: {
    input: "./petstore.yaml",
    output: {
      client: "zod",
      target: "./src/generated/zod.ts",
      override: {},
    },
  },
});
// petstore.yaml
openapi: "3.0.0"
info:
  version: 1.0.0
  title: Swagger Petstore
  license:
    name: MIT
servers:
  - url: http://petstore.swagger.io/v1
paths:
  /pets:
    post:
      summary: Create a pet
      operationId: createPets
      requestBody:
        required: true
        content:
          application/json:
            schema:
              required:
                - login
                - password
                - remember
              properties:
                login:
                  description: ""
                  oneOf:
                    - type: string
                      maxLength: 18
                      minLength: 18
                    - type: string
                      format: email
                password:
                  type: string
                  minLength: 8
                remember:
                  type: boolean
                  example: false
              type: object
            example:
              login: "+7 (982) 556-29-77"
              password: t9NB5Z4H8Q_V
              remember: false
      responses:
        "201":
          description: Null response

@melloware melloware added this to the 7.1.1 milestone Sep 8, 2024
@melloware melloware merged commit 19ff0e0 into orval-labs:master Sep 8, 2024
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Zod: oneOf with regex and string().email() missing 'regex' variable type
2 participants