Skip to content

Commit 068531e

Browse files
Fixes #321 - append unicode flag to regex patterns (#322)
* Fixes #321 - append unicode flag to regex patterns * chore: adding changeset * Fixing other failing tests
1 parent 76d86f6 commit 068531e

File tree

4 files changed

+42
-2
lines changed

4 files changed

+42
-2
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"openapi-zod-client": patch
3+
---
4+
5+
Append unicode flag for schema regular expression patterns when the pattern includes common unicode escape classes

lib/src/openApiToZod.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ const formatPatternIfNeeded = (pattern: string) => {
372372

373373
pattern = escapeControlCharacters(pattern);
374374

375-
return `/${pattern}/`;
375+
return pattern.includes("\\u") || pattern.includes("\\p") ? `/${pattern}/u` : `/${pattern}/`;
376376
};
377377

378378
const getZodChainableStringValidations = (schema: SchemaObject) => {

lib/tests/invalid-pattern-regex.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,6 @@ test("invalid-pattern-regex", () => {
2626
expect(
2727
getZodSchema({ schema: controlCharacters }) + getZodChain({ schema: controlCharacters })
2828
).toMatchInlineSnapshot(
29-
'"z.string().regex(/[\\x01\\x02\\x03\\x04\\x05\\x06\\x07\\x08\\t\\n\\x0b\\x0c\\r\\x0e\\x0f\\x10\\x11\\x12\\x13\\x14\\x15\\x16\\x17\\x18\\x19\\x1a\\x1b\\x1c\\x1d\\x1e\\x1f\\x7f\\x80\\x81\\x82\\x83\\x84\\x85\\x86\\x87\\x88\\x89\\x8a\\x8b\\x8c\\x8d\\x8e\\x8f\\x90\\x91\\x92\\x93\\x94\\x95\\x96\\x97\\x98\\x99\\x9a\\x9b\\x9c\\x9d\\x9e\\x9f\\ufffe\\uffff]+/).optional()"'
29+
'"z.string().regex(/[\\x01\\x02\\x03\\x04\\x05\\x06\\x07\\x08\\t\\n\\x0b\\x0c\\r\\x0e\\x0f\\x10\\x11\\x12\\x13\\x14\\x15\\x16\\x17\\x18\\x19\\x1a\\x1b\\x1c\\x1d\\x1e\\x1f\\x7f\\x80\\x81\\x82\\x83\\x84\\x85\\x86\\x87\\x88\\x89\\x8a\\x8b\\x8c\\x8d\\x8e\\x8f\\x90\\x91\\x92\\x93\\x94\\x95\\x96\\x97\\x98\\x99\\x9a\\x9b\\x9c\\x9d\\x9e\\x9f\\ufffe\\uffff]+/u).optional()"'
3030
);
3131
});
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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("unicode-pattern-regex", () => {
7+
const schema: SchemaObject = {
8+
type: "string",
9+
pattern: "\\p{L}+",
10+
};
11+
const schemaWithSlashes: SchemaObject = {
12+
type: "string",
13+
pattern: "/\\p{L}+/",
14+
};
15+
const schemaWithComplexUnicodePattern: SchemaObject = {
16+
type: "string",
17+
pattern: "$|^[\\p{L}\\d]+[\\p{L}\\d\\s.&()\\*'',-;#]*|$",
18+
};
19+
const schemaWithSlashU: SchemaObject = {
20+
type: "string",
21+
pattern: "\\u{1F600}+",
22+
}
23+
expect(getZodSchema({ schema: schema }) + getZodChain({ schema })).toMatchInlineSnapshot(
24+
'"z.string().regex(/\\p{L}+/u).optional()"'
25+
);
26+
expect(getZodSchema({ schema: schemaWithSlashes }) + getZodChain({ schema: schemaWithSlashes })).toMatchInlineSnapshot(
27+
'"z.string().regex(/\\p{L}+/u).optional()"'
28+
);
29+
expect(getZodSchema({ schema: schemaWithComplexUnicodePattern }) + getZodChain({ schema: schemaWithComplexUnicodePattern })).toMatchInlineSnapshot(
30+
'"z.string().regex(/$|^[\\p{L}\\d]+[\\p{L}\\d\\s.&()\\*\'\',-;#]*|$/u).optional()"'
31+
);
32+
expect(getZodSchema({ schema: schemaWithSlashU }) + getZodChain({ schema: schemaWithSlashU })).toMatchInlineSnapshot(
33+
'"z.string().regex(/\\u{1F600}+/u).optional()"'
34+
);
35+
});

0 commit comments

Comments
 (0)