Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 7 additions & 10 deletions express-zod-api/src/documentation-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ const samples = {
object: {},
null: null,
array: [],
} satisfies Record<Extract<SchemaObjectType, string>, unknown>;
} satisfies Record<SchemaObjectType, unknown>;

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

const isSupportedType = (subject: string): subject is SchemaObjectType =>
subject in samples;

const getSupportedType = (value: unknown): SchemaObjectType | undefined => {
const detected = R.toLower(R.type(value)); // toLower is typed well unlike .toLowerCase()
const isSupported =
detected === "number" ||
detected === "string" ||
detected === "boolean" ||
detected === "object" ||
detected === "null" ||
detected === "array";
return typeof value === "bigint"
? "integer"
: isSupported
: isSupportedType(detected)
? detected
: undefined;
};
Expand Down Expand Up @@ -277,7 +273,8 @@ const makeNullableType = ({
| SchemaObjectType
| SchemaObjectType[] => {
if (type === "null") return type;
if (typeof type === "string") return [type as SchemaObjectType, "null"]; // @todo make method instead of "as"
if (typeof type === "string")
return isSupportedType(type) ? [type, "null"] : "null";
return type ? [...new Set(type).add("null")] : "null";
};

Expand Down