orval-labs / orval

orval is able to generate client with appropriate type-signatures (TypeScript) from any valid OpenAPI v3 or Swagger v2 specification, either in yaml or json formats. 🍺
https://orval.dev
MIT License
2.81k stars 313 forks source link

Zod: Request for Response Schema Validation when using react-query + Zod Option #1586

Open zi-gae opened 3 weeks ago

zi-gae commented 3 weeks ago

Description:

Hi there,

I am using Orval with the react-query option and the zod option for request validation. I have successfully validated the request parameters using the Zod schema, but I would like to validate the response schema as well.

Questions:

Is there currently a way to validate the response schema using Zod in this setup? If not, is there a particular reason why response validation with Zod is not supported?

melloware commented 3 weeks ago

@zi-gae as far as I know the Zod stuff is generated for all responses see this closed request as completed: https://github.com/orval-labs/orval/issues/1380

melloware commented 3 weeks ago

I submitted a PR to update the docs for zod but you should be able to do this.

module.exports = {
  petstore: {
    output: {
      ...
      override: {
        zod: {
          generateEachHttpStatus: true,
        },
      },
    },
    ...
  },
};
zi-gae commented 3 weeks ago

@melloware I am using the following configuration in my project:

module.exports = {
  petstore: {
    output: {
      mode: 'tags-split',
      target: 'src/gen/endpoints',
      schemas: 'src/model',
      client: 'react-query',
      mock: true,
      override: {
        zod: {
          generateEachHttpStatus: true,
        },
      },
    },
    input: {
      target: './api.json',
    },
  },
  petstoreZod: {
    input: {
      target: './api.json',
    },
    output: {
      mode: 'tags-split',
      client: 'zod',
      target: 'src/gen/endpoints',
      fileExtension: '.zod.ts',
    },
  },
};

The issue I'm encountering is that while request parameter schema validation is correctly generated, the response schema validation for the GET method is not being generated.

I suspect that I might be missing something in the configuration, but I cannot pinpoint what exactly is causing this issue. Any advice or suggestions on what might be going wrong here would be greatly appreciated!

Thank you in advance!

Environment Details

Swagger URL: https://admin-dev-api.bluekeydsat.com/v3/api-docs

melloware commented 3 weeks ago

don't you need the override on the zod client itself?


module.exports = {
  petstore: {
    output: {
      mode: 'tags-split',
      target: 'src/gen/endpoints',
      schemas: 'src/model',
      client: 'react-query',
      mock: true,
    },
    input: {
      target: './api.json',
    },
  },
  petstoreZod: {
    input: {
      target: './api.json',
    },
    output: {
      mode: 'tags-split',
      client: 'zod',
      target: 'src/gen/endpoints',
      fileExtension: '.zod.ts',
      override: {
        zod: {
          generateEachHttpStatus: true,
        },
      },
    },
  },
};
zi-gae commented 3 weeks ago

@melloware When running the command npx orval --config ./orval.config.cjs with the configuration below, the Zod schema for validating response values from GET methods is not being generated. Instead, only the Zod schemas for request parameters and body values are being created.

// orval.config.cjs
module.exports = {
  petstore: {
    output: {
      mode: 'tags-split',
      target: 'src/gen/endpoints',
      schemas: 'src/model',
      client: 'react-query',
      mock: true,
    },
    input: {
      target: './api.json',
    },
  },
  petstoreZod: {
    input: {
      target: './api.json',
    },
    output: {
      mode: 'tags-split',
      client: 'zod',
      target: 'src/gen/endpoints',
      fileExtension: '.zod.ts',
      override: {
        zod: {
          generateEachHttpStatus: true,
        },
      },
    },
  },
};

Expected Behavior: Zod schemas should be generated for both the response values and the request parameters/body values when using GET methods.

Actual Behavior: Only the request parameters and body value schemas are being generated. The Zod schema for response values from GET methods is missing.

Could you please provide guidance or a fix for ensuring that Zod schemas for GET method responses are generated correctly?

melloware commented 3 weeks ago

You will have to look at the code and provide a PR to fix. Or ask one of the other devs who made zod fixes to see if they can help...