Skip to content

Commit 4781f74

Browse files
author
Hendrik Lücke-Tieke
committed
I wish husky would do his job correctly *sigh*
1 parent 22edc13 commit 4781f74

File tree

3 files changed

+46
-40
lines changed

3 files changed

+46
-40
lines changed

packages/zod/src/index.ts

Lines changed: 44 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,9 @@ export const generateZodValidationSchemaDefinition = (
159159
]);
160160

161161
if (schema.items) {
162-
if ((max || Number.POSITIVE_INFINITY) > schema31.prefixItems.length) {
162+
if (
163+
(max || Number.POSITIVE_INFINITY) > schema31.prefixItems.length
164+
) {
163165
// only add zod.rest() if number of tuple elements can exceed provided prefixItems:
164166
functions.push([
165167
'rest',
@@ -429,14 +431,14 @@ export const parseZodValidationSchemaDefinition = (
429431
if (fn === 'object') {
430432
return `zod.object({
431433
${Object.entries(args)
432-
.map(([key, schema]) => {
433-
const value = (schema as ZodValidationSchemaDefinition).functions
434-
.map(parseProperty)
435-
.join('');
436-
consts += (schema as ZodValidationSchemaDefinition).consts.join('\n');
437-
return ` "${key}": ${value.startsWith('.') ? 'zod' : ''}${value}`;
438-
})
439-
.join(',\n')}
434+
.map(([key, schema]) => {
435+
const value = (schema as ZodValidationSchemaDefinition).functions
436+
.map(parseProperty)
437+
.join('');
438+
consts += (schema as ZodValidationSchemaDefinition).consts.join('\n');
439+
return ` "${key}": ${value.startsWith('.') ? 'zod' : ''}${value}`;
440+
})
441+
.join(',\n')}
440442
})`;
441443
}
442444
if (fn === 'array') {
@@ -481,8 +483,9 @@ ${Object.entries(args)
481483

482484
const schema = input.functions.map(parseProperty).join('');
483485
const value = preprocessResponse
484-
? `.preprocess(${preprocessResponse.name}, ${schema.startsWith('.') ? 'zod' : ''
485-
}${schema})`
486+
? `.preprocess(${preprocessResponse.name}, ${
487+
schema.startsWith('.') ? 'zod' : ''
488+
}${schema})`
486489
: schema;
487490

488491
const zod = `${value.startsWith('.') ? 'zod' : ''}${value}`;
@@ -846,12 +849,12 @@ const generateZodRoute = async (
846849

847850
const preprocessResponse = override.zod.preprocess?.response
848851
? await generateMutator({
849-
output,
850-
mutator: override.zod.preprocess.response,
851-
name: `${operationName}PreprocessResponse`,
852-
workspace: context.workspace,
853-
tsconfig: context.output.tsconfig,
854-
})
852+
output,
853+
mutator: override.zod.preprocess.response,
854+
name: `${operationName}PreprocessResponse`,
855+
workspace: context.workspace,
856+
tsconfig: context.output.tsconfig,
857+
})
855858
: undefined;
856859

857860
const inputResponses = parsedResponses.map((parsedResponse) =>
@@ -893,13 +896,15 @@ const generateZodRoute = async (
893896
...(inputBody.consts ? [inputBody.consts] : []),
894897
...(inputBody.zod
895898
? [
896-
parsedBody.isArray
897-
? `export const ${operationName}BodyItem = ${inputBody.zod}
898-
export const ${operationName}Body = zod.array(${operationName}BodyItem)${parsedBody.rules?.min ? `.min(${parsedBody.rules?.min})` : ''
899-
}${parsedBody.rules?.max ? `.max(${parsedBody.rules?.max})` : ''
900-
}`
901-
: `export const ${operationName}Body = ${inputBody.zod}`,
902-
]
899+
parsedBody.isArray
900+
? `export const ${operationName}BodyItem = ${inputBody.zod}
901+
export const ${operationName}Body = zod.array(${operationName}BodyItem)${
902+
parsedBody.rules?.min ? `.min(${parsedBody.rules?.min})` : ''
903+
}${
904+
parsedBody.rules?.max ? `.max(${parsedBody.rules?.max})` : ''
905+
}`
906+
: `export const ${operationName}Body = ${inputBody.zod}`,
907+
]
903908
: []),
904909
...inputResponses
905910
.map((inputResponse, index) => {
@@ -910,18 +915,21 @@ export const ${operationName}Body = zod.array(${operationName}BodyItem)${parsedB
910915
...(inputResponse.consts ? [inputResponse.consts] : []),
911916
...(inputResponse.zod
912917
? [
913-
parsedResponses[index].isArray
914-
? `export const ${operationResponse}Item = ${inputResponse.zod
915-
}
916-
export const ${operationResponse} = zod.array(${operationResponse}Item)${parsedResponses[index].rules?.min
917-
? `.min(${parsedResponses[index].rules?.min})`
918-
: ''
919-
}${parsedResponses[index].rules?.max
920-
? `.max(${parsedResponses[index].rules?.max})`
921-
: ''
922-
}`
923-
: `export const ${operationResponse} = ${inputResponse.zod}`,
924-
]
918+
parsedResponses[index].isArray
919+
? `export const ${operationResponse}Item = ${
920+
inputResponse.zod
921+
}
922+
export const ${operationResponse} = zod.array(${operationResponse}Item)${
923+
parsedResponses[index].rules?.min
924+
? `.min(${parsedResponses[index].rules?.min})`
925+
: ''
926+
}${
927+
parsedResponses[index].rules?.max
928+
? `.max(${parsedResponses[index].rules?.max})`
929+
: ''
930+
}`
931+
: `export const ${operationResponse} = ${inputResponse.zod}`,
932+
]
925933
: []),
926934
];
927935
})

packages/zod/src/zod.test.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -668,7 +668,6 @@ describe('generatePartOfSchemaGenerateZod', () => {
668668
});
669669
});
670670

671-
672671
describe('parsePrefixItemsArrayAsTupleZod', () => {
673672
it('generates correctly', async () => {
674673
const arrayWithPrefixItemsSchema: SchemaObject31 = {
@@ -720,14 +719,13 @@ describe('parsePrefixItemsArrayAsTupleZod', () => {
720719
});
721720
});
722721

723-
724722
describe('parsePrefixItemsArrayAsTupleZod', () => {
725723
it('correctly omits rest', async () => {
726724
const arrayWithPrefixItemsSchema: SchemaObject31 = {
727725
type: 'array',
728726
prefixItems: [{ type: 'string' }, {}],
729727
items: { type: 'string' },
730-
maxItems: 2
728+
maxItems: 2,
731729
};
732730
const result = generateZodValidationSchemaDefinition(
733731
arrayWithPrefixItemsSchema as SchemaObject,

tests/configs/zod.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ export default defineConfig({
140140
typedArraysTuplesV31: {
141141
output: {
142142
target: '../generated/zod/typed-arrays-tuples-v3-1.ts',
143-
client: 'zod'
143+
client: 'zod',
144144
},
145145
input: {
146146
target: '../specifications/typed-arrays-tuples-v3-1.yaml',

0 commit comments

Comments
 (0)