Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: schema refiner #283

Merged
merged 2 commits into from
Apr 11, 2024
Merged
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
5 changes: 5 additions & 0 deletions .changeset/ten-poems-appear.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"openapi-zod-client": minor
---

Add `schemaRefiner` option to allow refining the OpenAPI schema before its converted to a Zod schema
6 changes: 4 additions & 2 deletions lib/src/openApiToZod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ type ConversionArgs = {
* @see https://github.com/colinhacks/zod
*/
// eslint-disable-next-line sonarjs/cognitive-complexity
export function getZodSchema({ schema, ctx, meta: inheritedMeta, options }: ConversionArgs): CodeMeta {
if (!schema) {
export function getZodSchema({ schema: $schema, ctx, meta: inheritedMeta, options }: ConversionArgs): CodeMeta {
if (!$schema) {
throw new Error("Schema is required");
}

const schema = options?.schemaRefiner?.($schema, inheritedMeta) ?? $schema;
const code = new CodeMeta(schema, ctx, inheritedMeta);
const meta = {
parent: code.inherit(inheritedMeta?.parent),
Expand Down
8 changes: 7 additions & 1 deletion lib/src/template-context.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { OpenAPIObject, OperationObject, PathItemObject, SchemaObject } from "openapi3-ts";
import type { OpenAPIObject, OperationObject, PathItemObject, ReferenceObject, SchemaObject } from "openapi3-ts";
import { sortBy, sortListFromRefArray, sortObjKeysFromArray } from "pastable/server";
import { ts } from "tanu";
import { match } from "ts-pattern";
Expand All @@ -11,6 +11,7 @@ import { getTypescriptFromOpenApi } from "./openApiToTypescript";
import { getZodSchema } from "./openApiToZod";
import { topologicalSort } from "./topologicalSort";
import { asComponentSchema, normalizeString } from "./utils";
import type { CodeMetaData } from "./CodeMeta";

const file = ts.createSourceFile("", "", ts.ScriptTarget.ESNext, true);
const printer = ts.createPrinter({ newLine: ts.NewLineKind.LineFeed });
Expand Down Expand Up @@ -403,4 +404,9 @@ export type TemplateContextOptions = {
* If 2 schemas have the same name but different types, export subsequent names with numbers appended
*/
exportAllNamedSchemas?: boolean;

/**
* A function that runs in the schema conversion process to refine the schema before it's converted to a Zod schema.
*/
schemaRefiner?: <T extends SchemaObject | ReferenceObject>(schema: T, parentMeta?: CodeMetaData) => T;
};
41 changes: 41 additions & 0 deletions lib/tests/schema-refiner.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { isReferenceObject } from "openapi3-ts";
import { getZodSchema } from "../src/openApiToZod";
import { test, expect } from "vitest";

test("schema-refiner", () => {
expect(
getZodSchema({
schema: {
properties: {
name: {
type: "string",
},
email: {
type: "string",
},
},
},
options: {
schemaRefiner(schema) {
if (isReferenceObject(schema) || !schema.properties) {
return schema;
}

if (!schema.required && schema.properties) {
for (const key in schema.properties) {
const prop = schema.properties[key];

if (!isReferenceObject(prop)) {
prop.nullable = true;
}
}
}

return schema;
},
},
})
).toMatchInlineSnapshot(
'"z.object({ name: z.string().nullable(), email: z.string().nullable() }).partial().passthrough()"'
);
});
Loading