Skip to content

Commit

Permalink
fix: schema.pattern when not wrapped with /xxx/
Browse files Browse the repository at this point in the history
chore: run lint:fix
  • Loading branch information
astahmer committed Dec 7, 2022
1 parent 8e8cb53 commit bda6ecb
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 4 deletions.
2 changes: 1 addition & 1 deletion lib/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "openapi-zod-client",
"version": "1.4.4",
"version": "1.4.5",
"repository": {
"type": "git",
"url": "https://github.com/astahmer/openapi-zod-client.git"
Expand Down
6 changes: 5 additions & 1 deletion lib/src/makeSchemaResolver.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { OpenAPIObject, SchemaObject } from "openapi3-ts";
import { get } from "pastable/server";

import { normalizeString } from "./utils";

const autocorrectRef = (ref: string) => (ref[1] === "/" ? ref : "#/" + ref.slice(1));
Expand All @@ -11,7 +12,10 @@ type RefInfo = {
};

export const makeSchemaResolver = (doc: OpenAPIObject) => {
// both used for debugging purpose
// eslint-disable-next-line sonarjs/no-unused-collection
const nameByRef = new Map<string, string>();
// eslint-disable-next-line sonarjs/no-unused-collection
const refByName = new Map<string, string>();

const byRef = new Map<string, RefInfo>();
Expand All @@ -23,7 +27,7 @@ export const makeSchemaResolver = (doc: OpenAPIObject) => {
const split = correctRef.split("/");

// "#/components/schemas/Something.jsonld" -> #/components/schemas
const path = split.slice(1, split.length - 1).join("/")!;
const path = split.slice(1, -1).join("/")!;
const map = get(doc, path.replace("#/", "").replace("#", "").replaceAll("/", ".")) ?? ({} as any);

// "#/components/schemas/Something.jsonld" -> "Something.jsonld"
Expand Down
10 changes: 9 additions & 1 deletion lib/src/openApiToZod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,14 @@ const getZodChainableDefault = (schema: SchemaObject) => {
return "";
};

const wrapPatternIfNeeded = (pattern: string) => {
if (pattern.startsWith("/") && pattern.endsWith("/")) {
return pattern;
}

return `/${pattern}/`;
};

const getZodChainableStringValidations = (schema: SchemaObject) => {
const validations: string[] = [];

Expand All @@ -238,7 +246,7 @@ const getZodChainableStringValidations = (schema: SchemaObject) => {
}

if (schema.pattern) {
validations.push(`regex(${schema.pattern})`);
validations.push(`regex(${wrapPatternIfNeeded(schema.pattern)})`);
}

if (schema.format) {
Expand Down
2 changes: 1 addition & 1 deletion lib/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export function normalizeString(text: string) {
}

export const wrapWithQuotesIfNeeded = (str: string) => {
if (str.match(/^\w+$/)) {
if (/^\w+$/.test(str)) {
return str;
}

Expand Down
21 changes: 21 additions & 0 deletions lib/tests/invalid-pattern-regex.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { getZodSchema } from "../src";
import { expect, test } from "vitest";
import { getZodChain } from "../src/openApiToZod";
import { SchemaObject } from "openapi3-ts";

test("invalid-pattern-regex", () => {
const invalidSchema: SchemaObject = {
type: "string",
pattern: "[0-9]+",
};
const schema: SchemaObject = {
type: "string",
pattern: "/[0-9]+/",
};
expect(getZodSchema({ schema: schema }) + getZodChain(schema)).toMatchInlineSnapshot(
'"z.string().regex(/[0-9]+/).optional()"'
);
expect(getZodSchema({ schema: invalidSchema }) + getZodChain(invalidSchema)).toMatchInlineSnapshot(
'"z.string().regex(/[0-9]+/).optional()"'
);
});

0 comments on commit bda6ecb

Please sign in to comment.