Skip to content

Commit 45c34f0

Browse files
authored
Tests for overriders and minor adjustments (#2556)
Instead of testing Zod's depiction, transforming current tests into the ones testing actions of overriders individually.
1 parent e3984ed commit 45c34f0

File tree

5 files changed

+512
-1417
lines changed

5 files changed

+512
-1417
lines changed

express-zod-api/src/documentation-helpers.ts

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -96,18 +96,18 @@ const samples = {
9696
export const reformatParamsInPath = (path: string) =>
9797
path.replace(routePathParamsRegex, (param) => `{${param.slice(1)}}`);
9898

99-
const onDefault: Overrider = ({ zodSchema, jsonSchema }) =>
99+
export const onDefault: Overrider = ({ zodSchema, jsonSchema }) =>
100100
(jsonSchema.default =
101101
globalRegistry.get(zodSchema)?.[metaSymbol]?.defaultLabel ??
102102
jsonSchema.default);
103103

104-
const onUpload: Overrider = ({ jsonSchema }, ctx) => {
104+
export const onUpload: Overrider = ({ jsonSchema }, ctx) => {
105105
if (ctx.isResponse)
106106
throw new DocumentationError("Please use ez.upload() only for input.", ctx);
107107
Object.assign(jsonSchema, { type: "string", format: "binary" });
108108
};
109109

110-
const onFile: Overrider = ({ jsonSchema }) => {
110+
export const onFile: Overrider = ({ jsonSchema }) => {
111111
delete jsonSchema.anyOf; // undo default
112112
Object.assign(jsonSchema, {
113113
type: "string",
@@ -120,7 +120,7 @@ const onFile: Overrider = ({ jsonSchema }) => {
120120
});
121121
};
122122

123-
const onUnion: Overrider = ({ zodSchema, jsonSchema }) => {
123+
export const onUnion: Overrider = ({ zodSchema, jsonSchema }) => {
124124
if (!zodSchema._zod.disc) return;
125125
const propertyName = Array.from(zodSchema._zod.disc.keys()).pop();
126126
jsonSchema.discriminator ??= { propertyName };
@@ -172,15 +172,15 @@ const intersect = R.tryCatch(
172172
(_err, allOf): JSONSchema.BaseSchema => ({ allOf }),
173173
);
174174

175-
const onIntersection: Overrider = ({ jsonSchema }) => {
175+
export const onIntersection: Overrider = ({ jsonSchema }) => {
176176
if (!jsonSchema.allOf) return;
177177
const attempt = intersect(jsonSchema.allOf);
178178
delete jsonSchema.allOf; // undo default
179179
Object.assign(jsonSchema, attempt);
180180
};
181181

182182
/** @since OAS 3.1 nullable replaced with type array having null */
183-
const onNullable: Overrider = ({ jsonSchema }) => {
183+
export const onNullable: Overrider = ({ jsonSchema }) => {
184184
if (!jsonSchema.anyOf) return;
185185
const original = jsonSchema.anyOf[0];
186186
Object.assign(original, { type: makeNullableType(original) });
@@ -191,7 +191,7 @@ const onNullable: Overrider = ({ jsonSchema }) => {
191191
const isSupportedType = (subject: string): subject is SchemaObjectType =>
192192
subject in samples;
193193

194-
const onDateIn: Overrider = ({ jsonSchema }, ctx) => {
194+
export const onDateIn: Overrider = ({ jsonSchema }, ctx) => {
195195
if (ctx.isResponse)
196196
throw new DocumentationError("Please use ez.dateOut() for output.", ctx);
197197
delete jsonSchema.anyOf; // undo default
@@ -206,7 +206,7 @@ const onDateIn: Overrider = ({ jsonSchema }, ctx) => {
206206
});
207207
};
208208

209-
const onDateOut: Overrider = ({ jsonSchema }, ctx) => {
209+
export const onDateOut: Overrider = ({ jsonSchema }, ctx) => {
210210
if (!ctx.isResponse)
211211
throw new DocumentationError("Please use ez.dateIn() for input.", ctx);
212212
Object.assign(jsonSchema, {
@@ -219,14 +219,18 @@ const onDateOut: Overrider = ({ jsonSchema }, ctx) => {
219219
});
220220
};
221221

222-
const onBigInt: Overrider = ({ jsonSchema }) =>
223-
Object.assign(jsonSchema, { type: "integer", format: "bigint" });
222+
export const onBigInt: Overrider = ({ jsonSchema }) =>
223+
Object.assign(jsonSchema, {
224+
type: "string",
225+
format: "bigint",
226+
pattern: /^-?\d+$/.source,
227+
});
224228

225229
/**
226230
* @since OAS 3.1 using prefixItems for depicting tuples
227231
* @since 17.5.0 added rest handling, fixed tuple type
228232
*/
229-
const onTuple: Overrider = ({ zodSchema, jsonSchema }) => {
233+
export const onTuple: Overrider = ({ zodSchema, jsonSchema }) => {
230234
if ((zodSchema as $ZodTuple)._zod.def.rest !== null) return;
231235
// does not appear to support items:false, so not:{} is a recommended alias
232236
jsonSchema.items = { not: {} };
@@ -250,7 +254,7 @@ const makeNullableType = ({
250254
return type ? [...new Set(type).add("null")] : "null";
251255
};
252256

253-
const onPipeline: Overrider = ({ zodSchema, jsonSchema }, ctx) => {
257+
export const onPipeline: Overrider = ({ zodSchema, jsonSchema }, ctx) => {
254258
const target = (zodSchema as $ZodPipe)._zod.def[
255259
ctx.isResponse ? "out" : "in"
256260
];
@@ -284,7 +288,7 @@ const onPipeline: Overrider = ({ zodSchema, jsonSchema }, ctx) => {
284288
}
285289
};
286290

287-
const onRaw: Overrider = ({ jsonSchema }) => {
291+
export const onRaw: Overrider = ({ jsonSchema }) => {
288292
Object.assign(
289293
jsonSchema,
290294
(jsonSchema as JSONSchema.ObjectSchema).properties!.raw,
@@ -479,7 +483,7 @@ const fixReferences = (
479483
};
480484

481485
// @todo rename?
482-
export const delegate = (
486+
const delegate = (
483487
schema: $ZodType,
484488
{
485489
ctx,

0 commit comments

Comments
 (0)