Skip to content

Commit

Permalink
Add template string compatibility to VALIDATOR_TYPE_REGEX
Browse files Browse the repository at this point in the history
  • Loading branch information
lambertkevin committed Jan 3, 2024
1 parent a34b9f3 commit 2141c5d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
Expand Up @@ -17,7 +17,7 @@ import { GeneratorConfig } from '../../schemas';
// "u" flag for Unicode support

export const VALIDATOR_TYPE_REGEX =
/@zod(?<import>\.import\(\[(?<imports>[\w\s"@'${}/,;:.~*-]+)\]\))?\.(?<type>[\w]+){1}(?<customErrors>\([{][\w\p{Script=Latin}\p{Script=Hiragana}\p{Script=Katakana}\p{Script=Han}\p{M} (),'ʼ"。、|\\:+*#!§$%&/{}[\]=?~><°^\\-]+[}]\))?(?<validatorPattern>[\w\p{Script=Latin}\p{Script=Hiragana}\p{Script=Katakana}\p{Script=Han}\p{Punctuation}\p{M}\p{N} (),.'"。、|\\:+*#!§$%&/{}[\]=?~><°^\\-]*[)])?/u;
/@zod(?<import>\.import\(\[(?<imports>[\w\s"@'${}/,;:.~*-]+)\]\))?\.(?<type>[\w]+){1}(?<customErrors>\([{][\w\p{Script=Latin}\p{Script=Hiragana}\p{Script=Katakana}\p{Script=Han}\p{M} (),'"。、|\\:+*#!§$%&/{}[\]=?~><°^\\-]+[}]\))?(?<validatorPattern>[\w\p{Script=Latin}\p{Script=Hiragana}\p{Script=Katakana}\p{Script=Han}\p{Punctuation}\p{M}\p{N} (),.'`"。、|\\:+*#!§$%&/{}[\]=?~><°^\\-]*[)])?/u;

/////////////////////////////////////////////////
// CLASS
Expand Down
Expand Up @@ -107,6 +107,21 @@ export function testExtendedDMMFFieldValidatorMatch<
);
});

it(`should load a class with docs and custom validator containing template string`, async () => {
const field = getField({
documentation:
'some text in docs @zod.custom.use(z.literal(`foo${string}`))',
});
const match = field?.['_validatorMatch'];
expect(match?.groups?.['validatorPattern']).toBe(
'.use(z.literal(`foo${string}`))',
);
expect(field?.clearedDocumentation).toBe('some text in docs');
expect(field.documentation).toBe(
'some text in docs @zod.custom.use(z.literal(`foo${string}`))',
);
});

it(`should match japanese characters in the regex`, async () => {
const match = VALIDATOR_TYPE_REGEX.exec(
`@zod.string({ invalid_type_error: "ひらがな、カタカナ、漢字が少なくとも1つずつ含まれる必要があります。" }).min(5, { message: "ひらがな、カタカナ、漢字が少なくとも1つずつ含まれる必要があります。" })`,
Expand Down

0 comments on commit 2141c5d

Please sign in to comment.