Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

## Version 24

### v24.7.4

- This patch fixes the issue for users facing `TypeError: .example is not a function`, but not using that method:
- It was reported that in some environments Zod plugin is not loaded correctly so that the usage of
`ZodType::example()` (plugin method) internally by the framework itself was causing this issue;
- This version fixes the problem by replacing the usage of `.example()` with `globalRegistry.add()`.

### v24.7.3

- Fixed the depiction of the negative response to `HEAD` requests:
Expand Down
8 changes: 8 additions & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ const importConcerns = [
})),
];

const compatibilityConerns = [
{
selector: "CallExpression > MemberExpression[property.name='example']",
message: "avoid using example() method to operate without zod plugin",
},
];

const performanceConcerns = [
{
selector: "ImportDeclaration[source.value=/assert/]", // #2169
Expand Down Expand Up @@ -210,6 +217,7 @@ export default tsPlugin.config(
"warn",
...importConcerns,
...performanceConcerns,
...compatibilityConerns,
],
},
},
Expand Down
24 changes: 14 additions & 10 deletions express-zod-api/src/result-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,15 +112,17 @@ export const defaultResultHandler = new ResultHandler({
}
return responseSchema;
},
negative: z
.object({
negative: () => {
const schema = z.object({
status: z.literal("error"),
error: z.object({ message: z.string() }),
})
.example({
status: "error",
error: { message: "Sample error message" },
}),
});
const examples: z.output<typeof schema>[] = [
{ status: "error", error: { message: "Sample error message" } },
];
globalRegistry.add(schema, { examples });
return schema;
},
handler: ({ error, input, output, request, response, logger }) => {
if (error) {
const httpError = ensureHttpError(error);
Expand Down Expand Up @@ -169,9 +171,11 @@ export const arrayResultHandler = new ResultHandler({
}
return responseSchema;
},
negative: {
schema: z.string().example("Sample error message"),
mimeType: "text/plain",
negative: () => {
const schema = z.string();
const examples: z.output<typeof schema>[] = ["Sample error message"];
globalRegistry.add(schema, { examples });
return { schema, mimeType: "text/plain" };
},
handler: ({ response, output, error, logger, request, input }) => {
if (error) {
Expand Down