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

feat(zod): add option coerce #1310

Merged
merged 1 commit into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions docs/src/pages/reference/configuration/output.md
Original file line number Diff line number Diff line change
Expand Up @@ -947,6 +947,13 @@ module.exports = {
header: true,
body: true
},
coerce: {
response: true,
query: true,
param: true,
header: true,
body: true
},
},
},
},
Expand All @@ -963,6 +970,14 @@ Default Value: `false`.

Use to set the strict mode for the zod schema. If you set it to true, the schema will be generated with the strict mode.

##### coerce

Type: `Object`.

Default Value: `false`.

Use to set the coerce for the zod schema. If you set it to true, the schema will be generated with the coerce on possible types.

#### mock

Type: `Object`.
Expand Down Expand Up @@ -1466,6 +1481,8 @@ module.exports = {

Type: `Boolean`

Deprecated: Use `zod.coerce` instead.

Valid values: true or false. Defaults to false.

Use this property to enable [type coercion](https://zod.dev/?id=coercion-for-primitives) for [Zod](https://zod.dev/) schemas (only applies to query parameters schemas).
Expand Down
16 changes: 15 additions & 1 deletion packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ export type NormalizedOverrideOutput = {
) => string;
requestOptions: Record<string, any> | boolean;
useDates?: boolean;
coerceTypes?: boolean;
coerceTypes?: boolean; // deprecated
useTypeOverInterfaces?: boolean;
useDeprecatedOperations?: boolean;
useBigInt?: boolean;
Expand Down Expand Up @@ -363,6 +363,13 @@ export type ZodOptions = {
body?: boolean;
response?: boolean;
};
coerce?: {
param?: boolean;
query?: boolean;
header?: boolean;
body?: boolean;
response?: boolean;
};
};

export type NormalizedZodOptions = {
Expand All @@ -373,6 +380,13 @@ export type NormalizedZodOptions = {
body: boolean;
response: boolean;
};
coerce: {
param: boolean;
query: boolean;
header: boolean;
body: boolean;
response: boolean;
};
};

export type HonoOptions = {
Expand Down
19 changes: 11 additions & 8 deletions packages/hono/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,14 +199,17 @@ const getHandlerFix = ({
const getVerbOptionGroupByTag = (
verbOptions: Record<string, GeneratorVerbOptions>,
) => {
return Object.values(verbOptions).reduce((acc, value) => {
const tag = value.tags[0];
if (!acc[tag]) {
acc[tag] = [];
}
acc[tag].push(value);
return acc;
}, {} as Record<string, GeneratorVerbOptions[]>);
return Object.values(verbOptions).reduce(
(acc, value) => {
const tag = value.tags[0];
if (!acc[tag]) {
acc[tag] = [];
}
acc[tag].push(value);
return acc;
},
{} as Record<string, GeneratorVerbOptions[]>,
);
};

const generateHandlers = async (
Expand Down
14 changes: 14 additions & 0 deletions packages/orval/src/utils/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,13 @@ export const normalizeOptions = async (
body: outputOptions.override?.zod?.strict?.body ?? false,
response: outputOptions.override?.zod?.strict?.response ?? false,
},
coerce: {
param: outputOptions.override?.zod?.coerce?.param ?? false,
query: outputOptions.override?.zod?.coerce?.query ?? false,
header: outputOptions.override?.zod?.coerce?.header ?? false,
body: outputOptions.override?.zod?.coerce?.body ?? false,
response: outputOptions.override?.zod?.coerce?.response ?? false,
},
},
swr: {
...(outputOptions.override?.swr ?? {}),
Expand Down Expand Up @@ -342,6 +349,13 @@ const normalizeOperationsAndTags = (
body: zod.strict?.body ?? false,
response: zod.strict?.response ?? false,
},
coerce: {
param: zod.coerce?.param ?? false,
query: zod.coerce?.query ?? false,
header: zod.coerce?.header ?? false,
body: zod.coerce?.body ?? false,
response: zod.coerce?.response ?? false,
},
},
}
: {}),
Expand Down
13 changes: 12 additions & 1 deletion packages/zod/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -527,23 +527,34 @@ const generateZodRoute = (
const inputParams = parseZodValidationSchemaDefinition(
zodDefinitionsParameters.params,
override.zod.strict.param,
override.zod.coerce.param,
);

if (override.coerceTypes) {
console.warn(
'override.coerceTypes is deprecated, please use override.zod.coerce instead.',
);
}

const inputQueryParams = parseZodValidationSchemaDefinition(
zodDefinitionsParameters.queryParams,
override.zod.strict.query,
override.coerceTypes,
override.zod.coerce.query ?? override.coerceTypes,
);
const inputHeaders = parseZodValidationSchemaDefinition(
zodDefinitionsParameters.headers,
override.zod.strict.header,
override.zod.coerce.header,
);
const inputBody = parseZodValidationSchemaDefinition(
zodDefinitionsBody,
override.zod.strict.body,
override.zod.coerce.body,
);
const inputResponse = parseZodValidationSchemaDefinition(
zodDefinitionsResponse,
override.zod.strict.response,
override.zod.coerce.response,
);

if (
Expand Down
20 changes: 20 additions & 0 deletions tests/configs/zod.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,24 @@ export default defineConfig({
target: '../specifications/circular.yaml',
},
},
coerce: {
output: {
target: '../generated/zod/coerce.ts',
client: 'zod',
override: {
zod: {
coerce: {
response: true,
query: true,
header: true,
param: true,
body: true,
},
},
},
},
input: {
target: '../specifications/circular.yaml',
},
},
});