Skip to content

Commit

Permalink
feat: add ability to just generate specified zod validations (#1480)
Browse files Browse the repository at this point in the history
* feat: add ability to just generate specified zod validations

Now we can configure zod generator to generate only selected type of schemas

Closes: #1477

* docs: add documentation for new feature zod generate

* fix: typing issues and code format
  • Loading branch information
mortezakarimi authored Jul 18, 2024
1 parent e30c7fc commit c70fe7b
Show file tree
Hide file tree
Showing 5 changed files with 421 additions and 14 deletions.
34 changes: 34 additions & 0 deletions docs/src/pages/reference/configuration/output.md
Original file line number Diff line number Diff line change
Expand Up @@ -1041,6 +1041,40 @@ 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.

##### generate

Type: `Object`.

Default Value: `true`.

Use to set the which type of schemas you want to generate for the zod schema.

example:

```js
module.exports = {
petstore: {
output: {
...
override: {
zod: {
generate: {
param: true,
body: true,
response: false,
query: true,
header: true,
}
},
},
},
...
},
};
```

In the above example exclude response body validations not generated

##### coerce

Type: `Object`.
Expand Down
14 changes: 14 additions & 0 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,13 @@ export type ZodOptions = {
body?: boolean;
response?: boolean;
};
generate: {
param: boolean;
query: boolean;
header: boolean;
body: boolean;
response: boolean;
};
coerce?: {
param?: boolean | ZodCoerceType[];
query?: boolean | ZodCoerceType[];
Expand Down Expand Up @@ -411,6 +418,13 @@ export type NormalizedZodOptions = {
body: boolean;
response: boolean;
};
generate: {
param: boolean;
query: boolean;
header: boolean;
body: boolean;
response: boolean;
};
coerce: {
param: boolean | ZodCoerceType[];
query: boolean | ZodCoerceType[];
Expand Down
17 changes: 14 additions & 3 deletions packages/orval/src/utils/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import {
NormalizedOperationOptions,
NormalizedOptions,
NormalizedQueryOptions,
NormalizedZodOptions,
OperationOptions,
OptionsExport,
OutputClient,
Expand All @@ -31,11 +30,9 @@ import {
RefComponentSuffix,
SwaggerParserOptions,
upath,
ZodOptions,
} from '@orval/core';
import { DEFAULT_MOCK_OPTIONS } from '@orval/mock';
import chalk from 'chalk';
import { InfoObject } from 'openapi3-ts/oas30';
import pkg from '../../package.json';
import { githubResolver } from './github';
import { loadPackageJson } from './package-json';
Expand Down Expand Up @@ -241,6 +238,13 @@ export const normalizeOptions = async (
body: outputOptions.override?.zod?.strict?.body ?? false,
response: outputOptions.override?.zod?.strict?.response ?? false,
},
generate: {
param: outputOptions.override?.zod?.generate?.param ?? true,
query: outputOptions.override?.zod?.generate?.query ?? true,
header: outputOptions.override?.zod?.generate?.header ?? true,
body: outputOptions.override?.zod?.generate?.body ?? true,
response: outputOptions.override?.zod?.generate?.response ?? true,
},
coerce: {
param: outputOptions.override?.zod?.coerce?.param ?? false,
query: outputOptions.override?.zod?.coerce?.query ?? false,
Expand Down Expand Up @@ -420,6 +424,13 @@ const normalizeOperationsAndTags = (
body: zod.strict?.body ?? false,
response: zod.strict?.response ?? false,
},
generate: {
param: zod.generate?.param ?? true,
query: zod.generate?.query ?? true,
header: zod.generate?.header ?? true,
body: zod.generate?.body ?? true,
response: zod.generate?.response ?? true,
},
coerce: {
param: zod.coerce?.param ?? false,
query: zod.coerce?.query ?? false,
Expand Down
40 changes: 31 additions & 9 deletions packages/zod/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
import {
camel,
ClientBuilder,
ClientGeneratorsBuilder,
ContextSpecs,
escape,
generateMutator,
GeneratorDependency,
GeneratorMutator,
GeneratorOptions,
GeneratorVerbOptions,
ZodCoerceType,
camel,
escape,
generateMutator,
getNumberWord,
isBoolean,
isObject,
isString,
jsStringEscape,
pascal,
resolveRef,
ZodCoerceType,
} from '@orval/core';
import uniq from 'lodash.uniq';
import {
Expand Down Expand Up @@ -460,11 +460,13 @@ const parseBodyAndResponse = ({
context,
name,
strict,
generate,
}: {
data: ResponseObject | RequestBodyObject | ReferenceObject | undefined;
context: ContextSpecs;
name: string;
strict: boolean;
generate: boolean;
}): {
input: ZodValidationSchemaDefinition;
isArray: boolean;
Expand All @@ -473,7 +475,7 @@ const parseBodyAndResponse = ({
max?: number;
};
} => {
if (!data) {
if (!data || !generate) {
return {
input: { functions: [], consts: [] },
isArray: false,
Expand Down Expand Up @@ -545,6 +547,7 @@ const parseParameters = ({
context,
operationName,
strict,
generate,
}: {
data: (ParameterObject | ReferenceObject)[] | undefined;
context: ContextSpecs;
Expand All @@ -556,6 +559,13 @@ const parseParameters = ({
body: boolean;
response: boolean;
};
generate: {
param: boolean;
query: boolean;
header: boolean;
body: boolean;
response: boolean;
};
}): {
headers: ZodValidationSchemaDefinition;
queryParams: ZodValidationSchemaDefinition;
Expand Down Expand Up @@ -594,6 +604,12 @@ const parseParameters = ({
header: strict.header,
};

const mapGenerate = {
path: generate.param,
query: generate.query,
header: generate.header,
};

const definition = generateZodValidationSchemaDefinition(
schema,
context,
Expand All @@ -604,21 +620,21 @@ const parseParameters = ({
},
);

if (parameter.in === 'header') {
if (parameter.in === 'header' && mapGenerate.header) {
return {
...acc,
headers: { ...acc.headers, [parameter.name]: definition },
};
}

if (parameter.in === 'query') {
if (parameter.in === 'query' && mapGenerate.query) {
return {
...acc,
queryParams: { ...acc.queryParams, [parameter.name]: definition },
};
}

if (parameter.in === 'path') {
if (parameter.in === 'path' && mapGenerate.path) {
return {
...acc,
params: { ...acc.params, [parameter.name]: definition },
Expand Down Expand Up @@ -691,12 +707,16 @@ const generateZodRoute = async (
| PathItemObject
| undefined;

const parameters = spec?.[verb]?.parameters;
const parameters = spec?.[verb]?.parameters as (
| ParameterObject
| ReferenceObject
)[];
const parsedParameters = parseParameters({
data: parameters,
context,
operationName,
strict: override.zod.strict,
generate: override.zod.generate,
});

const requestBody = spec?.[verb]?.requestBody;
Expand All @@ -705,6 +725,7 @@ const generateZodRoute = async (
context,
name: camel(`${operationName}-body`),
strict: override.zod.strict.body,
generate: override.zod.generate.body,
});

const responses = (
Expand All @@ -718,6 +739,7 @@ const generateZodRoute = async (
context,
name: camel(`${operationName}-${code}-response`),
strict: override.zod.strict.response,
generate: override.zod.generate.response,
}),
);

Expand Down
Loading

0 comments on commit c70fe7b

Please sign in to comment.