Skip to content

Commit 8c64782

Browse files
committed
Introducing isSchemaObjectType() helper.
1 parent a0345ae commit 8c64782

1 file changed

Lines changed: 7 additions & 10 deletions

File tree

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

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ const samples = {
9898
object: {},
9999
null: null,
100100
array: [],
101-
} satisfies Record<Extract<SchemaObjectType, string>, unknown>;
101+
} satisfies Record<SchemaObjectType, unknown>;
102102

103103
export const reformatParamsInPath = (path: string) =>
104104
path.replace(routePathParamsRegex, (param) => `{${param.slice(1)}}`);
@@ -197,18 +197,14 @@ const onNullable: Overrider = ({ jsonSchema }) => {
197197
delete jsonSchema.anyOf;
198198
};
199199

200+
const isSupportedType = (subject: string): subject is SchemaObjectType =>
201+
subject in samples;
202+
200203
const getSupportedType = (value: unknown): SchemaObjectType | undefined => {
201204
const detected = R.toLower(R.type(value)); // toLower is typed well unlike .toLowerCase()
202-
const isSupported =
203-
detected === "number" ||
204-
detected === "string" ||
205-
detected === "boolean" ||
206-
detected === "object" ||
207-
detected === "null" ||
208-
detected === "array";
209205
return typeof value === "bigint"
210206
? "integer"
211-
: isSupported
207+
: isSupportedType(detected)
212208
? detected
213209
: undefined;
214210
};
@@ -277,7 +273,8 @@ const makeNullableType = ({
277273
| SchemaObjectType
278274
| SchemaObjectType[] => {
279275
if (type === "null") return type;
280-
if (typeof type === "string") return [type as SchemaObjectType, "null"]; // @todo make method instead of "as"
276+
if (typeof type === "string")
277+
return isSupportedType(type) ? [type, "null"] : "null";
281278
return type ? [...new Set(type).add("null")] : "null";
282279
};
283280

0 commit comments

Comments
 (0)