orval icon indicating copy to clipboard operation
orval copied to clipboard

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

Open zi-gae opened this issue 1 year ago • 6 comments

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?

zi-gae avatar Aug 19 '24 04:08 zi-gae

@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 avatar Aug 19 '24 11:08 melloware

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,
        },
      },
    },
    ...
  },
};

melloware avatar Aug 19 '24 12:08 melloware

@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

zi-gae avatar Aug 19 '24 14:08 zi-gae

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,
        },
      },
    },
  },
};

melloware avatar Aug 19 '24 14:08 melloware

@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?

zi-gae avatar Aug 20 '24 04:08 zi-gae

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...

melloware avatar Aug 20 '24 10:08 melloware

I had the same issues with Zod not generating the response schema of the basic 200 responses, even when output.override.zod.generate.response: true

In my case, it was an issue in the openAPI spec, the routes returned a content-type of application/hal+json It seems it was not supported for the zod generation. it worked after changing the content-type of the response to application/json in the openAPI file.

ArnaudGathy avatar Oct 30 '25 15:10 ArnaudGathy

@zi-gae can you check @ArnaudGathy suggestion? I am wondering if this is something we can handle with Orval?

melloware avatar Oct 30 '25 16:10 melloware