Skip to content

Commit

Permalink
minor fix in japanese custom error message handling
Browse files Browse the repository at this point in the history
  • Loading branch information
chrishoermann committed May 4, 2023
1 parent 62ba58d commit 9778132
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 6 deletions.
2 changes: 1 addition & 1 deletion packages/generator/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "zod-prisma-types",
"version": "2.5.10",
"version": "2.5.11",
"description": "Generates zod schemas from Prisma models with advanced validation",
"author": "Chris Hörmann",
"license": "MIT",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ import {
ARRAY_VALIDATOR_NUMBER_OR_STRING_AND_MESSAGE_REGEX,
ARRAY_VALIDATOR_WITH_MESSAGE_REGEX,
} from '../extendedDMMFFieldArrayValidatorString';
import {
VALIDATOR_CUSTOM_ERROR_MESSAGE_REGEX,
VALIDATOR_CUSTOM_ERROR_REGEX,
VALIDATOR_CUSTOM_ERROR_SPLIT_KEYS_REGEX,
} from '../extendedDMMFFieldValidatorCustomErrors';
import {
STRING_VALIDATOR_NUMBER_AND_MESSAGE_REGEX,
STRING_VALIDATOR_MESSAGE_REGEX,
Expand Down Expand Up @@ -329,6 +334,32 @@ describe(`ExtendedDMMFFieldDefaultValidators`, () => {
// VALIDATOR CUSTOM ERRORS TESTS
// ----------------------------------------------

describe(`ExtendedDMMFFieldValidatorCustomErrors`, () => {
it(`array validator number should return match for regex with japanese chars`, async () => {
const result = VALIDATOR_CUSTOM_ERROR_REGEX.exec(
"({ invalid_type_error: 'カタカナ漢字ひらがな', required_error: 'カタカナ漢字ひらがな', description: 'カタカナ漢字ひらがな' })",
);
expect(result?.groups?.object).toBe(
"{ invalid_type_error: 'カタカナ漢字ひらがな', required_error: 'カタカナ漢字ひらがな', description: 'カタカナ漢字ひらがな' }",
);
expect(result?.groups?.messages).toBe(
" invalid_type_error: 'カタカナ漢字ひらがな', required_error: 'カタカナ漢字ひらがな', description: 'カタカナ漢字ひらがな' ",
);
});
it(`array validator number should return match for regex with japanese chars`, async () => {
const messageArray =
" invalid_type_error: 'カタカナ漢字ひらがな', required_error: 'カタカナ漢字ひらがな', description: 'カタカナ漢字ひらがな' "
.replace(VALIDATOR_CUSTOM_ERROR_MESSAGE_REGEX, '')
.match(VALIDATOR_CUSTOM_ERROR_SPLIT_KEYS_REGEX);

expect(messageArray).toEqual([
'invalid_type_error',
'required_error',
'description',
]);
});
});

describe(`ExtendedDMMFFieldValidatorCustomErrors`, () => {
it(`should load a class without docs`, async () => {
const field = getField();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ import { DMMF } from '@prisma/generator-helper';
import { it, expect, describe } from 'vitest';

import { DEFAULT_GENERATOR_CONFIG, FIELD_BASE } from './setup';
import { ExtendedDMMFFieldValidatorCustomErrors } from '../extendedDMMFFieldValidatorCustomErrors';
import {
ExtendedDMMFFieldValidatorCustomErrors,
VALIDATOR_CUSTOM_ERROR_MESSAGE_REGEX,
VALIDATOR_CUSTOM_ERROR_REGEX,
VALIDATOR_CUSTOM_ERROR_SPLIT_KEYS_REGEX,
} from '../extendedDMMFFieldValidatorCustomErrors';

const getField = (field?: Partial<DMMF.Field>) =>
new ExtendedDMMFFieldValidatorCustomErrors(
Expand All @@ -11,6 +16,32 @@ const getField = (field?: Partial<DMMF.Field>) =>
'ModelName',
);

describe(`ExtendedDMMFFieldValidatorCustomErrors`, () => {
it(`array validator number should return match for regex with japanese chars`, async () => {
const result = VALIDATOR_CUSTOM_ERROR_REGEX.exec(
"({ invalid_type_error: 'カタカナ漢字ひらがな', required_error: 'カタカナ漢字ひらがな', description: 'カタカナ漢字ひらがな' })",
);
expect(result?.groups?.object).toBe(
"{ invalid_type_error: 'カタカナ漢字ひらがな', required_error: 'カタカナ漢字ひらがな', description: 'カタカナ漢字ひらがな' }",
);
expect(result?.groups?.messages).toBe(
" invalid_type_error: 'カタカナ漢字ひらがな', required_error: 'カタカナ漢字ひらがな', description: 'カタカナ漢字ひらがな' ",
);
});
it(`array validator number should return match for regex with japanese chars`, async () => {
const messageArray =
" invalid_type_error: 'カタカナ漢字ひらがな', required_error: 'カタカナ漢字ひらがな', description: 'カタカナ漢字ひらがな' "
.replace(VALIDATOR_CUSTOM_ERROR_MESSAGE_REGEX, '')
.match(VALIDATOR_CUSTOM_ERROR_SPLIT_KEYS_REGEX);

expect(messageArray).toEqual([
'invalid_type_error',
'required_error',
'description',
]);
});
});

describe(`ExtendedDMMFFieldValidatorCustomErrors`, () => {
it(`should load a class without docs`, async () => {
const field = getField();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ export type ZodCustomErrorKey =
/////////////////////////////////////////////////

export const VALIDATOR_CUSTOM_ERROR_REGEX =
/(\()(?<object>\{(?<messages>[\w (),'":+\-*#!§$%&/{}[\]=?~><°^]+)\})(\))/;
/(\()(?<object>\{(?<messages>[\w\p{Script=Hiragana}\p{Script=Katakana}\p{Script=Han} (),'":+\-*#!§$%&/{}[\]=?~><°^]+)\})(\))/u;

export const VALIDATOR_CUSTOM_ERROR_MESSAGE_REGEX =
/[ ]?"[\w (),.':+\-*#!§$%&/{}[\]=?~><°^]+"[,]?[ ]?/g;
/[ ]?"[\w\p{Script=Hiragana}\p{Script=Katakana}\p{Script=Han} (),.':+\-*#!§$%&/{}[\]=?~><°^]+"[,]?[ ]?/gu;

export const VALIDATOR_CUSTOM_ERROR_SPLIT_KEYS_REGEX = /[\w]+(?=:)/g;
export const VALIDATOR_CUSTOM_ERROR_SPLIT_KEYS_REGEX = /[\w]+(?=:)/gu;

/////////////////////////////////////////////////
// CONSTANTS
Expand Down
3 changes: 2 additions & 1 deletion packages/usage/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,8 @@ model SelfReference {

model JapaneseChars {
id Int @id @default(autoincrement())
string String /// @zod.string.min(5, { message: "カタカナ漢字ひらがな" })
///@zod.string({ invalid_type_error: "カタカナ漢字ひらがな", required_error: "カタカナ漢字ひらがな", description: "カタカナ漢字ひらがな" }).min(5, { message: "カタカナ漢字ひらがな" })
string String
}

// model Post {
Expand Down

0 comments on commit 9778132

Please sign in to comment.