Skip to content
Open
Show file tree
Hide file tree
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
16 changes: 8 additions & 8 deletions lib/src/generateZodClientFromOpenAPI.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -736,7 +736,7 @@ describe("generateZodClientFromOpenAPI", () => {
path: "/store/inventory",
description: \`Returns a map of status codes to quantities\`,
requestFormat: "json",
response: z.record(z.number()),
response: z.record(z.number().int()),
},
{
method: "post",
Expand Down Expand Up @@ -1238,7 +1238,7 @@ describe("generateZodClientFromOpenAPI", () => {
alias: "getInventory",
description: \`Returns a map of status codes to quantities\`,
requestFormat: "json",
response: z.record(z.number()),
response: z.record(z.number().int()),
},
{
method: "post",
Expand Down Expand Up @@ -1741,7 +1741,7 @@ describe("generateZodClientFromOpenAPI", () => {
path: "/store/inventory",
description: \`Returns a map of status codes to quantities\`,
requestFormat: "json",
response: z.record(z.number()),
response: z.record(z.number().int()),
},
{
method: "post",
Expand Down Expand Up @@ -1955,8 +1955,8 @@ describe("generateZodClientFromOpenAPI", () => {
return new Zodios(baseUrl, endpoints, options);
}
"
`)
})
`);
});

test("withAlias as a custom function", async () => {
const prettyOutput = await generateZodClientFromOpenAPI({
Expand Down Expand Up @@ -2248,7 +2248,7 @@ describe("generateZodClientFromOpenAPI", () => {
alias: "getInventory",
description: \`Returns a map of status codes to quantities\`,
requestFormat: "json",
response: z.record(z.number()),
response: z.record(z.number().int()),
},
{
method: "post",
Expand Down Expand Up @@ -2753,7 +2753,7 @@ describe("generateZodClientFromOpenAPI", () => {
path: "/store/inventory",
description: \`Returns a map of status codes to quantities\`,
requestFormat: "json",
response: z.record(z.number()),
response: z.record(z.number().int()),
},
{
method: "post",
Expand Down Expand Up @@ -3245,7 +3245,7 @@ describe("generateZodClientFromOpenAPI", () => {
path: "/store/inventory",
description: \`Returns a map of status codes to quantities\`,
requestFormat: "json",
response: z.record(z.number()),
response: z.record(z.number().int()),
},
{
method: "post",
Expand Down
4 changes: 2 additions & 2 deletions lib/src/openApiToZod.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ test("getSchemaAsZodString", () => {
expect(getSchemaAsZodString({ type: "boolean" })).toMatchInlineSnapshot('"z.boolean()"');
expect(getSchemaAsZodString({ type: "string" })).toMatchInlineSnapshot('"z.string()"');
expect(getSchemaAsZodString({ type: "number" })).toMatchInlineSnapshot('"z.number()"');
expect(getSchemaAsZodString({ type: "integer" })).toMatchInlineSnapshot('"z.number()"');
expect(getSchemaAsZodString({ type: "integer" })).toMatchInlineSnapshot('"z.number().int()"');
// expect(getSchemaAsZodString({ type: "string", format: "date-time" })).toMatchInlineSnapshot('"z.string().datetime()"');
// expect(getSchemaAsZodString({ type: "number", nullable: true, minimum: 0 })).toMatchInlineSnapshot('"z.number().nullable().gte(0)"');

Expand Down Expand Up @@ -172,7 +172,7 @@ test("getSchemaAsZodString", () => {
});

test("getSchemaWithChainableAsZodString", () => {
expect(getSchemaAsZodString({ type: "string", nullable: true })).toMatchInlineSnapshot('"z.string()"');
expect(getSchemaAsZodString({ type: "string", nullable: true })).toMatchInlineSnapshot('"z.string().nullable()"');
expect(getSchemaAsZodString({ type: "string", nullable: false })).toMatchInlineSnapshot('"z.string()"');

expect(getSchemaAsZodString({ type: "string", nullable: false }, { isRequired: true })).toMatchInlineSnapshot(
Expand Down
15 changes: 1 addition & 14 deletions lib/src/openApiToZod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,20 +109,7 @@ export function getZodSchema({ schema, ctx, meta: inheritedMeta, options }: Conv

const types = schema.anyOf
.map((prop) => getZodSchema({ schema: prop, ctx, meta, options }))
.map((type) => {
let isObject = true;

if ("type" in type.schema) {
if (Array.isArray(type.schema.type)) {
isObject = false;
} else {
const schemaType = type.schema.type.toLowerCase() as NonNullable<typeof schema.type>;
isObject = !isPrimitiveType(schemaType);
}
}

return type.toString();
})
.map((type) => type.toString())
.join(", ");

return code.assign(`z.union([${types}])`);
Expand Down
50 changes: 14 additions & 36 deletions lib/tests/missing-zod-chains-on-z-object-with-refs-props.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,25 +51,18 @@ test("missing-zod-chains-on-z-object-with-refs-props", async () => {
"import { makeApi, Zodios, type ZodiosOptions } from "@zodios/core";
import { z } from "zod";

const Email = z.string();
const Password = z.string();
const AddUser = z
.object({
email: Email.min(6)
.max(255)
.regex(/(EmailRegex)/),
password: Password.min(16)
.max(255)
.regex(/(PasswordRegex)/),
})
.passthrough();
const PasswordReminder = z
.object({
email: Email.min(6)
.max(255)
.regex(/(EmailRegex)/),
})
.passthrough();
const Email = z
.string()
.min(6)
.max(255)
.regex(/(EmailRegex)/);
const Password = z
.string()
.min(16)
.max(255)
.regex(/(PasswordRegex)/);
const AddUser = z.object({ email: Email, password: Password }).passthrough();
const PasswordReminder = z.object({ email: Email }).passthrough();

export const schemas = {
Email,
Expand All @@ -87,16 +80,7 @@ test("missing-zod-chains-on-z-object-with-refs-props", async () => {
{
name: "body",
type: "Body",
schema: z
.object({
email: Email.min(6)
.max(255)
.regex(/(EmailRegex)/),
password: Password.min(16)
.max(255)
.regex(/(PasswordRegex)/),
})
.passthrough(),
schema: z.object({ email: Email, password: Password }).passthrough(),
},
],
response: z.void(),
Expand All @@ -109,13 +93,7 @@ test("missing-zod-chains-on-z-object-with-refs-props", async () => {
{
name: "body",
type: "Body",
schema: z
.object({
email: Email.min(6)
.max(255)
.regex(/(EmailRegex)/),
})
.passthrough(),
schema: z.object({ email: Email }).passthrough(),
},
],
response: z.void(),
Expand Down
Loading