Skip to content

Commit bda6ecb

Browse files
committed
fix: schema.pattern when not wrapped with /xxx/
chore: run lint:fix
1 parent 8e8cb53 commit bda6ecb

File tree

5 files changed

+37
-4
lines changed

5 files changed

+37
-4
lines changed

lib/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "openapi-zod-client",
3-
"version": "1.4.4",
3+
"version": "1.4.5",
44
"repository": {
55
"type": "git",
66
"url": "https://github.com/astahmer/openapi-zod-client.git"

lib/src/makeSchemaResolver.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { OpenAPIObject, SchemaObject } from "openapi3-ts";
22
import { get } from "pastable/server";
3+
34
import { normalizeString } from "./utils";
45

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

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

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

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

2933
// "#/components/schemas/Something.jsonld" -> "Something.jsonld"

lib/src/openApiToZod.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,14 @@ const getZodChainableDefault = (schema: SchemaObject) => {
226226
return "";
227227
};
228228

229+
const wrapPatternIfNeeded = (pattern: string) => {
230+
if (pattern.startsWith("/") && pattern.endsWith("/")) {
231+
return pattern;
232+
}
233+
234+
return `/${pattern}/`;
235+
};
236+
229237
const getZodChainableStringValidations = (schema: SchemaObject) => {
230238
const validations: string[] = [];
231239

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

240248
if (schema.pattern) {
241-
validations.push(`regex(${schema.pattern})`);
249+
validations.push(`regex(${wrapPatternIfNeeded(schema.pattern)})`);
242250
}
243251

244252
if (schema.format) {

lib/src/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export function normalizeString(text: string) {
1515
}
1616

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { getZodSchema } from "../src";
2+
import { expect, test } from "vitest";
3+
import { getZodChain } from "../src/openApiToZod";
4+
import { SchemaObject } from "openapi3-ts";
5+
6+
test("invalid-pattern-regex", () => {
7+
const invalidSchema: SchemaObject = {
8+
type: "string",
9+
pattern: "[0-9]+",
10+
};
11+
const schema: SchemaObject = {
12+
type: "string",
13+
pattern: "/[0-9]+/",
14+
};
15+
expect(getZodSchema({ schema: schema }) + getZodChain(schema)).toMatchInlineSnapshot(
16+
'"z.string().regex(/[0-9]+/).optional()"'
17+
);
18+
expect(getZodSchema({ schema: invalidSchema }) + getZodChain(invalidSchema)).toMatchInlineSnapshot(
19+
'"z.string().regex(/[0-9]+/).optional()"'
20+
);
21+
});

0 commit comments

Comments
 (0)