Skip to content
Open
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
14 changes: 14 additions & 0 deletions spec/types/string.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,18 @@ describe('string', () => {
}
);
});

it('maps a ZodString regex with flags to a pattern without the flags', () => {
expectSchema(
[
z
.string()
.regex(/^\p{Lu}+$/u)
.openapi('UnicodeRegexString'),
],
{
UnicodeRegexString: { type: 'string', pattern: '^\\p{Lu}+$' },
}
);
});
});
5 changes: 1 addition & 4 deletions src/transformers/string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,7 @@ function isZodCheckRegex(check: $ZodCheck<string>): check is $ZodCheckRegex {
export class StringTransformer {
transform(zodSchema: ZodString, mapNullableType: MapNullableType) {
const regexCheck = zodSchema.def.checks?.find(isZodCheckRegex);
// toString generates an additional / at the beginning and end of the pattern
const pattern = regexCheck?._zod.def.pattern
.toString()
.replace(/^\/|\/$/g, '');
const pattern = regexCheck?._zod.def.pattern.source;

const check = zodSchema.def.checks?.find(isZodCheckLengthEquals);
const length = check?._zod.def.length;
Expand Down