Skip to content

Conversation

@soartec-lab
Copy link
Member

@soartec-lab soartec-lab commented Dec 16, 2025

fix #2598

Summary

Added runtime validation feature for Fetch client using Zod schemas. This feature enables runtime verification that API responses match the expected types.

Details

Usage

Enable the fetch.runtimeValidation option in your configuration file:

export default defineConfig({
  petstore: {
    output: {
      schemas: {
        type: ['typescript', 'zod'],
      },
      client: 'fetch',
      override: {
        fetch: {
          runtimeValidation: true,
        },
      },
    },
    input: {
      target: './petstore.yaml',
    },
  },
});

Generated Code

+import { PetsSchema } from './model/index.zod';

 export const listPets = async (params: ListPetsParams): Promise<listPetsResponse> => {
   const res = await fetch(getListPetsUrl(params), { method: 'GET' });
   const body = await res.text();
-  const data: listPetsResponse['data'] = body ? JSON.parse(body) : {};
+  const parsedBody = body ? JSON.parse(body) : {};
+  const data = PetsSchema.parse(parsedBody);
   return { data, status: res.status, headers: res.headers };
 }

Response data is validated with Zod schemas, and runtime errors are thrown if types don't match. Primitive types (string, number, boolean, void) are excluded from validation.

Copilot AI review requested due to automatic review settings December 16, 2025 09:34
@soartec-lab soartec-lab added enhancement New feature or request fetch Fetch client related issue labels Dec 16, 2025
@soartec-lab soartec-lab added this to the 8.0.0 milestone Dec 16, 2025
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR introduces runtime validation support for the Fetch client using Zod schemas. When enabled, API responses are validated at runtime to ensure they match the expected types, throwing errors for type mismatches.

Key Changes:

  • Added runtimeValidation configuration option to FetchOptions and NormalizedFetchOptions
  • Modified code generation to inject Zod schema parsing when runtime validation is enabled
  • Primitive types (string, number, boolean, void) are excluded from validation

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

File Description
packages/core/src/types.ts Added runtimeValidation boolean field to Fetch type definitions
packages/orval/src/utils/options.ts Added normalization logic for the new runtimeValidation option
packages/fetch/src/index.ts Implemented runtime validation logic with Zod schema parsing in generated code
tests/configs/fetch.config.ts Enabled runtimeValidation in test configurations alongside useZodSchemaResponse

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@melloware melloware merged commit 73a58a0 into orval-labs:master Dec 17, 2025
2 checks passed
@mamarku
Copy link

mamarku commented Dec 30, 2025

How can I get runtime validation with the use of fetch and react-query?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request fetch Fetch client related issue

Projects

None yet

Development

Successfully merging this pull request may close these issues.

proposal: enhanced zod Integration for fetch Client

3 participants