Skip to content

Commit fb32cf5

Browse files
committed
test(zod): generateZodValidationSchemaDefinition required and strict mode
1 parent 4cfb246 commit fb32cf5

File tree

2 files changed

+131
-1
lines changed

2 files changed

+131
-1
lines changed

packages/zod/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ let constsUniqueCounter: Record<string, number> = {};
6868
// https://github.com/colinhacks/zod#coercion-for-primitives
6969
const COERCEABLE_TYPES = ['string', 'number', 'boolean', 'bigint', 'date'];
7070

71-
const generateZodValidationSchemaDefinition = (
71+
export const generateZodValidationSchemaDefinition = (
7272
schema: SchemaObject | undefined,
7373
_required: boolean | undefined,
7474
name: string,

packages/zod/src/zod.test.ts

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ import { describe, expect, it } from 'vitest';
22
import {
33
type ZodValidationSchemaDefinitionInput,
44
parseZodValidationSchemaDefinition,
5+
generateZodValidationSchemaDefinition,
56
} from '.';
7+
import { SchemaObject } from 'openapi3-ts/oas30';
68

79
const queryParams: ZodValidationSchemaDefinitionInput = {
810
functions: [
@@ -63,3 +65,131 @@ describe('parseZodValidationSchemaDefinition', () => {
6365
});
6466
});
6567
});
68+
69+
const objectIntoObjectSchema: SchemaObject = {
70+
type: 'object',
71+
properties: {
72+
pet: {
73+
type: 'object',
74+
properties: {
75+
name: {
76+
type: 'string',
77+
},
78+
tag: {
79+
type: 'string',
80+
},
81+
},
82+
},
83+
},
84+
};
85+
86+
const deepRequiredSchema: SchemaObject = {
87+
type: 'object',
88+
properties: {
89+
pet: {
90+
type: 'object',
91+
required: ['name'],
92+
properties: {
93+
name: {
94+
type: 'string',
95+
},
96+
tag: {
97+
type: 'string',
98+
},
99+
},
100+
},
101+
},
102+
};
103+
104+
describe('generateZodValidationSchemaDefinition`', () => {
105+
it('required', () => {
106+
const result = generateZodValidationSchemaDefinition(
107+
deepRequiredSchema,
108+
true,
109+
'strict',
110+
true,
111+
);
112+
113+
expect(result).toEqual({
114+
functions: [
115+
[
116+
'object',
117+
{
118+
pet: {
119+
functions: [
120+
[
121+
'object',
122+
{
123+
name: {
124+
functions: [['string', undefined]],
125+
consts: [],
126+
},
127+
tag: {
128+
functions: [
129+
['string', undefined],
130+
['optional', undefined],
131+
],
132+
consts: [],
133+
},
134+
},
135+
],
136+
['strict', undefined],
137+
['optional', undefined],
138+
],
139+
consts: [],
140+
},
141+
},
142+
],
143+
['strict', undefined],
144+
],
145+
consts: [],
146+
});
147+
});
148+
149+
it('generates a strict zod schema', () => {
150+
const result = generateZodValidationSchemaDefinition(
151+
objectIntoObjectSchema,
152+
true,
153+
'strict',
154+
true,
155+
);
156+
157+
expect(result).toEqual({
158+
functions: [
159+
[
160+
'object',
161+
{
162+
pet: {
163+
functions: [
164+
[
165+
'object',
166+
{
167+
name: {
168+
functions: [
169+
['string', undefined],
170+
['optional', undefined],
171+
],
172+
consts: [],
173+
},
174+
tag: {
175+
functions: [
176+
['string', undefined],
177+
['optional', undefined],
178+
],
179+
consts: [],
180+
},
181+
},
182+
],
183+
['strict', undefined],
184+
['optional', undefined],
185+
],
186+
consts: [],
187+
},
188+
},
189+
],
190+
['strict', undefined],
191+
],
192+
consts: [],
193+
});
194+
});
195+
});

0 commit comments

Comments
 (0)