Skip to content

Commit 1fa718b

Browse files
committed
Regex check fix
1 parent b379c0b commit 1fa718b

File tree

3 files changed

+21
-20
lines changed

3 files changed

+21
-20
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@povio/openapi-codegen-cli",
3-
"version": "1.1.1",
3+
"version": "1.1.2",
44
"main": "./dist/index.js",
55
"bin": {
66
"openapi-codegen": "./dist/sh.js"

src/generators/core/zod/getZodChain.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { OpenAPIV3 } from "openapi-types";
22
import { GenerateOptions } from "src/generators/types/options";
3-
import { unwrapQuotesIfNeeded } from "src/generators/utils/openapi.utils";
3+
import { escapeControlCharacters, unwrapQuotesIfNeeded } from "src/generators/utils/openapi.utils";
44
import { match } from "ts-pattern";
55
import { ZodSchemaMetaData } from "./ZodSchema.class";
66

@@ -88,9 +88,23 @@ function getZodChainableStringValidations(schema: OpenAPIV3.SchemaObject) {
8888
}
8989
}
9090

91+
if (schema.pattern) {
92+
validations.push(`regex(${formatPatternIfNeeded(schema.pattern)})`);
93+
}
94+
9195
return validations.join(".");
9296
}
9397

98+
function formatPatternIfNeeded(pattern: string) {
99+
if (pattern.startsWith("/") && pattern.endsWith("/")) {
100+
pattern = pattern.slice(1, -1);
101+
}
102+
103+
pattern = escapeControlCharacters(pattern);
104+
105+
return `/${pattern}/`;
106+
}
107+
94108
function getZodChainableNumberValidations(schema: OpenAPIV3.SchemaObject) {
95109
const validations: string[] = [];
96110

src/generators/core/zod/getZodSchema.ts

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import {
1919
isReferenceObject,
2020
isSchemaObject,
2121
} from "src/generators/utils/openapi-schema.utils";
22-
import { escapeControlCharacters, isPrimitiveType, wrapWithQuotesIfNeeded } from "src/generators/utils/openapi.utils";
22+
import { isPrimitiveType, wrapWithQuotesIfNeeded } from "src/generators/utils/openapi.utils";
2323
import { match } from "ts-pattern";
2424
import { getParentRef, ZodSchema, ZodSchemaMetaData } from "./ZodSchema.class";
2525
import { getZodChain } from "./getZodChain";
@@ -345,33 +345,20 @@ function getPrimitiveZodSchema({ schema, zodSchema, resolver, meta, tag }: GetPa
345345
.with("integer", () => INT_SCHEMA)
346346
.otherwise(() => NUMBER_SCHEMA),
347347
)
348-
.with("string", () => {
349-
if (schema.pattern) {
350-
return `z.regex(${formatPatternIfNeeded(schema.pattern)})`;
351-
}
352-
return match(schema.format)
348+
.with("string", () =>
349+
match(schema.format)
353350
.with("binary", () => BLOB_SCHEMA)
354351
.with("email", () => EMAIL_SCHEMA)
355352
.with("hostname", "uri", () => URL_SCHEMA)
356353
.with("uuid", () => UUID_SCHEMA)
357354
.with("date-time", () => DATETIME_SCHEMA)
358-
.otherwise(() => STRING_SCHEMA);
359-
})
355+
.otherwise(() => STRING_SCHEMA),
356+
)
360357
.otherwise((type) => `z.${type}()`),
361358
);
362359
}
363360
}
364361

365-
function formatPatternIfNeeded(pattern: string) {
366-
if (pattern.startsWith("/") && pattern.endsWith("/")) {
367-
pattern = pattern.slice(1, -1);
368-
}
369-
370-
pattern = escapeControlCharacters(pattern);
371-
372-
return `/${pattern}/`;
373-
}
374-
375362
function getEnumZodSchema({ resolver, schema, zodSchema, meta, tag }: GetPartialZodSchemaParams) {
376363
if (!isSchemaObject(schema)) {
377364
return;

0 commit comments

Comments
 (0)