From 0a6162121ed679038bff0c587a8111a8887d8a59 Mon Sep 17 00:00:00 2001 From: chrishoermann <53531653+chrishoermann@users.noreply.github.com> Date: Tue, 7 Mar 2023 11:12:13 +0100 Subject: [PATCH] added support for new zod bigint and additional string validators --- Readme.md | 4 +- packages/generator/Readme.md | 8 +- packages/generator/package.json | 4 +- .../__tests__/extendedDMMFField.test.ts | 207 +++++++++++++++++- .../extendedDMMFFieldValidatorMap.test.ts | 207 +++++++++++++++++- .../extendedDMMFFieldValidatorMap.ts | 75 +++++-- packages/usage/package.json | 4 +- packages/usage/prisma/schema.prisma | 3 +- pnpm-lock.yaml | 16 +- 9 files changed, 480 insertions(+), 48 deletions(-) diff --git a/Readme.md b/Readme.md index a429f276..ca8c609d 100644 --- a/Readme.md +++ b/Readme.md @@ -868,11 +868,11 @@ model MyModel { ## BigInt validators -To add custom validators to the prisma `BigInt` field you can use the `@zod.bigint` key. Due to the fact that there are no custom validators provided by `zod` on `z.bigint()` you can only add customized type errors to the field. +To add custom validators to the prisma `BigInt` field you can use the `@zod.bigint` key. On this key you can use all string-specific validators that are mentioned in the [`zod-docs`](https://github.com/colinhacks/zod#bigints). You can also add a custom error message to each validator as stated in the docs. ```prisma model MyModel { - myField BigInt /// @zod.bigint({ invalid_type_error: "error", ... }) + myField BigInt /// @zod.bigintlt(5n, { message: "lt error" }).gt(6n, { message: "gt error" })({ invalid_type_error: "error", ... }).[...chain more validators] } ``` diff --git a/packages/generator/Readme.md b/packages/generator/Readme.md index 6c4abc93..ca8c609d 100644 --- a/packages/generator/Readme.md +++ b/packages/generator/Readme.md @@ -277,7 +277,7 @@ export const ModelWithDefaultValuesOptionalDefaultsSchema = > default: `false` -If you need a separate model type that includes all the relation fields you can pass the following option. Due do the type annotation, that is needed to have recursive types, this model has some limitations since `z.ZodType` does not allow some object methods like `.merge()`, `.omit()`, etc. +If you need a separate model type that includes all the relation fields you can pass the following option. Due to the type annotation, that is needed to have recursive types, this model has some limitations since `z.ZodType` does not allow some object methods like `.merge()`, `.omit()`, etc. ```prisma generator zod { @@ -310,7 +310,7 @@ export const UserSchema = z.object({ enum: AnotherEnumSchema, id: z.string().cuid(), email: z.string(), - name: z.string(), + name: z.string().optional(), scalarList: z.string().array(), lat: z.number(), lng: z.number(), @@ -868,11 +868,11 @@ model MyModel { ## BigInt validators -To add custom validators to the prisma `BigInt` field you can use the `@zod.bigint` key. Due to the fact that there are no custom validators provided by `zod` on `z.bigint()` you can only add customized type errors to the field. +To add custom validators to the prisma `BigInt` field you can use the `@zod.bigint` key. On this key you can use all string-specific validators that are mentioned in the [`zod-docs`](https://github.com/colinhacks/zod#bigints). You can also add a custom error message to each validator as stated in the docs. ```prisma model MyModel { - myField BigInt /// @zod.bigint({ invalid_type_error: "error", ... }) + myField BigInt /// @zod.bigintlt(5n, { message: "lt error" }).gt(6n, { message: "gt error" })({ invalid_type_error: "error", ... }).[...chain more validators] } ``` diff --git a/packages/generator/package.json b/packages/generator/package.json index 7c777814..7a605d24 100644 --- a/packages/generator/package.json +++ b/packages/generator/package.json @@ -1,6 +1,6 @@ { "name": "zod-prisma-types", - "version": "2.3.6", + "version": "2.4.0", "description": "Generates zod schemas from Prisma models with advanced validation", "author": "Chris Hörmann", "license": "MIT", @@ -49,6 +49,6 @@ "@prisma/generator-helper": "^4.10.1", "code-block-writer": "^11.0.3", "lodash": "^4.17.21", - "zod": "^3.20.6" + "zod": "^3.21.4" } } diff --git a/packages/generator/src/classes/extendedDMMFField/__tests__/extendedDMMFField.test.ts b/packages/generator/src/classes/extendedDMMFField/__tests__/extendedDMMFField.test.ts index ceb4ebf1..9af8e7a2 100644 --- a/packages/generator/src/classes/extendedDMMFField/__tests__/extendedDMMFField.test.ts +++ b/packages/generator/src/classes/extendedDMMFField/__tests__/extendedDMMFField.test.ts @@ -386,6 +386,12 @@ describe(`ExtendedDMMFFieldValidatorMap test _validatorMap`, () => { pattern: '.url()', }), ).toBe(true); + expect( + map({ + key: 'emoji', + pattern: '.emoji()', + }), + ).toBe(true); expect( map({ key: 'uuid', @@ -398,6 +404,30 @@ describe(`ExtendedDMMFFieldValidatorMap test _validatorMap`, () => { pattern: '.cuid()', }), ).toBe(true); + expect( + map({ + key: 'cuid2', + pattern: '.cuid2()', + }), + ).toBe(true); + expect( + map({ + key: 'ulid', + pattern: '.ulid()', + }), + ).toBe(true); + expect( + map({ + key: 'regex', + pattern: '.regex(/^\\d+\\s*\\d+$/)', + }), + ).toBe(true); + expect( + map({ + key: 'includes', + pattern: '.includes("some")', + }), + ).toBe(true); expect( map({ key: 'startsWith', @@ -410,6 +440,18 @@ describe(`ExtendedDMMFFieldValidatorMap test _validatorMap`, () => { pattern: '.startsWith("some")', }), ).toBe(true); + expect( + map({ + key: 'datetime', + pattern: '.datetime()', + }), + ).toBe(true); + expect( + map({ + key: 'ip', + pattern: '.ip()', + }), + ).toBe(true); expect( map({ key: 'trim', @@ -418,8 +460,14 @@ describe(`ExtendedDMMFFieldValidatorMap test _validatorMap`, () => { ).toBe(true); expect( map({ - key: 'datetime', - pattern: '.datetime()', + key: 'toLowerCase', + pattern: '.toLowerCase()', + }), + ).toBe(true); + expect( + map({ + key: 'toUpperCase', + pattern: '.toUpperCase()', }), ).toBe(true); expect( @@ -468,6 +516,12 @@ describe(`ExtendedDMMFFieldValidatorMap test _validatorMap`, () => { pattern: '.url({ message: "someMessage" })', }), ).toBe(true); + expect( + map({ + key: 'emoji', + pattern: '.emoji({ message: "someMessage" })', + }), + ).toBe(true); expect( map({ key: 'uuid', @@ -480,6 +534,30 @@ describe(`ExtendedDMMFFieldValidatorMap test _validatorMap`, () => { pattern: '.cuid({ message: "someMessage" })', }), ).toBe(true); + expect( + map({ + key: 'cuid2', + pattern: '.cuid2({ message: "someMessage" })', + }), + ).toBe(true); + expect( + map({ + key: 'ulid', + pattern: '.ulid({ message: "someMessage" })', + }), + ).toBe(true); + // expect( + // map({ + // key: 'regex', + // pattern: '.regex(/^\\d+\\s*\\d+$/)', + // }), + // ).toBe(true); + expect( + map({ + key: 'includes', + pattern: '.includes("some", { message: "someMessage" })', + }), + ).toBe(true); expect( map({ key: 'startsWith', @@ -492,6 +570,12 @@ describe(`ExtendedDMMFFieldValidatorMap test _validatorMap`, () => { pattern: '.startsWith("some", { message: "someMessage" })', }), ).toBe(true); + expect( + map({ + key: 'datetime', + pattern: '.datetime({ message: "someMessage" })', + }), + ).toBe(true); expect( map({ key: 'trim', @@ -500,8 +584,14 @@ describe(`ExtendedDMMFFieldValidatorMap test _validatorMap`, () => { ).toBe(true); expect( map({ - key: 'datetime', - pattern: '.datetime({ message: "someMessage" })', + key: 'toLowerCase', + pattern: '.toLowerCase({ message: "someMessage" })', + }), + ).toBe(true); + expect( + map({ + key: 'toUpperCase', + pattern: '.toUpperCase({ message: "someMessage" })', }), ).toBe(true); expect(map({ key: 'noDefault', pattern: '.noDefault()' })).toBe(true); @@ -805,6 +895,60 @@ describe(`ExtendedDMMFFieldValidatorMap test _validatorMap`, () => { it(`should pass valid bigint data to validator map`, async () => { const map = field?.['_validatorMap']['bigint']; + expect( + map({ + key: 'gt', + pattern: '.gt(2n)', + }), + ).toBe(true); + expect( + map({ + key: 'gte', + pattern: '.gte(2n)', + }), + ).toBe(true); + expect( + map({ + key: 'lt', + pattern: '.lt(2n)', + }), + ).toBe(true); + expect( + map({ + key: 'lte', + pattern: '.lte(2n)', + }), + ).toBe(true); + expect( + map({ + key: 'multipleOf', + pattern: '.multipleOf(2n)', + }), + ).toBe(true); + expect( + map({ + key: 'positive', + pattern: '.positive()', + }), + ).toBe(true); + expect( + map({ + key: 'nonpositive', + pattern: '.nonpositive()', + }), + ).toBe(true); + expect( + map({ + key: 'negative', + pattern: '.negative()', + }), + ).toBe(true); + expect( + map({ + key: 'nonnegative', + pattern: '.nonnegative()', + }), + ).toBe(true); expect( map({ key: 'array', @@ -815,7 +959,60 @@ describe(`ExtendedDMMFFieldValidatorMap test _validatorMap`, () => { it(`should pass ivalid data to to validator map`, async () => { const map = field?.['_validatorMap']['bigint']; - + expect( + map({ + key: 'gt', + pattern: '.gt(2n, { message: "someMessage" })', + }), + ).toBe(true); + expect( + map({ + key: 'gte', + pattern: '.gte(2n, { message: "someMessage" })', + }), + ).toBe(true); + expect( + map({ + key: 'lt', + pattern: '.lt(2n, { message: "someMessage" })', + }), + ).toBe(true); + expect( + map({ + key: 'lte', + pattern: '.lte(2n, { message: "someMessage" })', + }), + ).toBe(true); + expect( + map({ + key: 'multipleOf', + pattern: '.multipleOf(2n, { message: "someMessage" })', + }), + ).toBe(true); + expect( + map({ + key: 'positive', + pattern: '.positive({ message: "someMessage" })', + }), + ).toBe(true); + expect( + map({ + key: 'nonpositive', + pattern: '.nonpositive({ message: "someMessage" })', + }), + ).toBe(true); + expect( + map({ + key: 'negative', + pattern: '.negative({ message: "someMessage" })', + }), + ).toBe(true); + expect( + map({ + key: 'nonnegative', + pattern: '.nonnegative({ message: "someMessage" })', + }), + ).toBe(true); expect(() => map({ key: 'array', diff --git a/packages/generator/src/classes/extendedDMMFField/__tests__/extendedDMMFFieldValidatorMap.test.ts b/packages/generator/src/classes/extendedDMMFField/__tests__/extendedDMMFFieldValidatorMap.test.ts index 960dd22e..7f49f1b9 100644 --- a/packages/generator/src/classes/extendedDMMFField/__tests__/extendedDMMFFieldValidatorMap.test.ts +++ b/packages/generator/src/classes/extendedDMMFField/__tests__/extendedDMMFFieldValidatorMap.test.ts @@ -65,6 +65,12 @@ describe(`ExtendedDMMFFieldValidatorMap test _validatorMap`, () => { pattern: '.url()', }), ).toBe(true); + expect( + map({ + key: 'emoji', + pattern: '.emoji()', + }), + ).toBe(true); expect( map({ key: 'uuid', @@ -77,6 +83,30 @@ describe(`ExtendedDMMFFieldValidatorMap test _validatorMap`, () => { pattern: '.cuid()', }), ).toBe(true); + expect( + map({ + key: 'cuid2', + pattern: '.cuid2()', + }), + ).toBe(true); + expect( + map({ + key: 'ulid', + pattern: '.ulid()', + }), + ).toBe(true); + expect( + map({ + key: 'regex', + pattern: '.regex(/^\\d+\\s*\\d+$/)', + }), + ).toBe(true); + expect( + map({ + key: 'includes', + pattern: '.includes("some")', + }), + ).toBe(true); expect( map({ key: 'startsWith', @@ -89,6 +119,18 @@ describe(`ExtendedDMMFFieldValidatorMap test _validatorMap`, () => { pattern: '.startsWith("some")', }), ).toBe(true); + expect( + map({ + key: 'datetime', + pattern: '.datetime()', + }), + ).toBe(true); + expect( + map({ + key: 'ip', + pattern: '.ip()', + }), + ).toBe(true); expect( map({ key: 'trim', @@ -97,8 +139,14 @@ describe(`ExtendedDMMFFieldValidatorMap test _validatorMap`, () => { ).toBe(true); expect( map({ - key: 'datetime', - pattern: '.datetime()', + key: 'toLowerCase', + pattern: '.toLowerCase()', + }), + ).toBe(true); + expect( + map({ + key: 'toUpperCase', + pattern: '.toUpperCase()', }), ).toBe(true); expect( @@ -147,6 +195,12 @@ describe(`ExtendedDMMFFieldValidatorMap test _validatorMap`, () => { pattern: '.url({ message: "someMessage" })', }), ).toBe(true); + expect( + map({ + key: 'emoji', + pattern: '.emoji({ message: "someMessage" })', + }), + ).toBe(true); expect( map({ key: 'uuid', @@ -159,6 +213,30 @@ describe(`ExtendedDMMFFieldValidatorMap test _validatorMap`, () => { pattern: '.cuid({ message: "someMessage" })', }), ).toBe(true); + expect( + map({ + key: 'cuid2', + pattern: '.cuid2({ message: "someMessage" })', + }), + ).toBe(true); + expect( + map({ + key: 'ulid', + pattern: '.ulid({ message: "someMessage" })', + }), + ).toBe(true); + // expect( + // map({ + // key: 'regex', + // pattern: '.regex(/^\\d+\\s*\\d+$/)', + // }), + // ).toBe(true); + expect( + map({ + key: 'includes', + pattern: '.includes("some", { message: "someMessage" })', + }), + ).toBe(true); expect( map({ key: 'startsWith', @@ -171,6 +249,12 @@ describe(`ExtendedDMMFFieldValidatorMap test _validatorMap`, () => { pattern: '.startsWith("some", { message: "someMessage" })', }), ).toBe(true); + expect( + map({ + key: 'datetime', + pattern: '.datetime({ message: "someMessage" })', + }), + ).toBe(true); expect( map({ key: 'trim', @@ -179,8 +263,14 @@ describe(`ExtendedDMMFFieldValidatorMap test _validatorMap`, () => { ).toBe(true); expect( map({ - key: 'datetime', - pattern: '.datetime({ message: "someMessage" })', + key: 'toLowerCase', + pattern: '.toLowerCase({ message: "someMessage" })', + }), + ).toBe(true); + expect( + map({ + key: 'toUpperCase', + pattern: '.toUpperCase({ message: "someMessage" })', }), ).toBe(true); expect(map({ key: 'noDefault', pattern: '.noDefault()' })).toBe(true); @@ -484,6 +574,60 @@ describe(`ExtendedDMMFFieldValidatorMap test _validatorMap`, () => { it(`should pass valid bigint data to validator map`, async () => { const map = field?.['_validatorMap']['bigint']; + expect( + map({ + key: 'gt', + pattern: '.gt(2n)', + }), + ).toBe(true); + expect( + map({ + key: 'gte', + pattern: '.gte(2n)', + }), + ).toBe(true); + expect( + map({ + key: 'lt', + pattern: '.lt(2n)', + }), + ).toBe(true); + expect( + map({ + key: 'lte', + pattern: '.lte(2n)', + }), + ).toBe(true); + expect( + map({ + key: 'multipleOf', + pattern: '.multipleOf(2n)', + }), + ).toBe(true); + expect( + map({ + key: 'positive', + pattern: '.positive()', + }), + ).toBe(true); + expect( + map({ + key: 'nonpositive', + pattern: '.nonpositive()', + }), + ).toBe(true); + expect( + map({ + key: 'negative', + pattern: '.negative()', + }), + ).toBe(true); + expect( + map({ + key: 'nonnegative', + pattern: '.nonnegative()', + }), + ).toBe(true); expect( map({ key: 'array', @@ -494,7 +638,60 @@ describe(`ExtendedDMMFFieldValidatorMap test _validatorMap`, () => { it(`should pass ivalid data to to validator map`, async () => { const map = field?.['_validatorMap']['bigint']; - + expect( + map({ + key: 'gt', + pattern: '.gt(2n, { message: "someMessage" })', + }), + ).toBe(true); + expect( + map({ + key: 'gte', + pattern: '.gte(2n, { message: "someMessage" })', + }), + ).toBe(true); + expect( + map({ + key: 'lt', + pattern: '.lt(2n, { message: "someMessage" })', + }), + ).toBe(true); + expect( + map({ + key: 'lte', + pattern: '.lte(2n, { message: "someMessage" })', + }), + ).toBe(true); + expect( + map({ + key: 'multipleOf', + pattern: '.multipleOf(2n, { message: "someMessage" })', + }), + ).toBe(true); + expect( + map({ + key: 'positive', + pattern: '.positive({ message: "someMessage" })', + }), + ).toBe(true); + expect( + map({ + key: 'nonpositive', + pattern: '.nonpositive({ message: "someMessage" })', + }), + ).toBe(true); + expect( + map({ + key: 'negative', + pattern: '.negative({ message: "someMessage" })', + }), + ).toBe(true); + expect( + map({ + key: 'nonnegative', + pattern: '.nonnegative({ message: "someMessage" })', + }), + ).toBe(true); expect(() => map({ key: 'array', diff --git a/packages/generator/src/classes/extendedDMMFField/extendedDMMFFieldValidatorMap.ts b/packages/generator/src/classes/extendedDMMFField/extendedDMMFFieldValidatorMap.ts index 45bf2371..90c821f9 100644 --- a/packages/generator/src/classes/extendedDMMFField/extendedDMMFFieldValidatorMap.ts +++ b/packages/generator/src/classes/extendedDMMFField/extendedDMMFFieldValidatorMap.ts @@ -5,23 +5,33 @@ import { ZodValidatorType } from './extendedDMMFFieldValidatorType'; // TYPES ///////////////////////////////////////////////// +export type ZodArrayValidatorKeys = 'array'; + export type ZodStringValidatorKeys = - | 'min' + | ZodArrayValidatorKeys | 'max' + | 'min' | 'length' | 'email' | 'url' + | 'emoji' | 'uuid' | 'cuid' + | 'cuid2' + | 'ulid' | 'regex' + | 'includes' | 'startsWith' | 'endsWith' - | 'trim' | 'datetime' - | 'noDefault' - | 'array'; + | 'ip' + | 'trim' + | 'toLowerCase' + | 'toUpperCase' + | 'noDefault'; export type ZodNumberValidatorKeys = + | ZodArrayValidatorKeys | 'gt' | 'gte' | 'lt' @@ -33,14 +43,23 @@ export type ZodNumberValidatorKeys = | 'nonnegative' | 'multipleOf' | 'finite' - | 'noDefault' - | 'array'; + | 'noDefault'; -export type ZodDateValidatorKeys = 'min' | 'max' | 'array'; +export type ZodDateValidatorKeys = ZodArrayValidatorKeys | 'min' | 'max'; -export type ZodBigIntValidatorKeys = 'array'; +export type ZodBigIntValidatorKeys = + | ZodArrayValidatorKeys + | 'gt' + | 'gte' + | 'lt' + | 'lte' + | 'positive' + | 'nonpositive' + | 'negative' + | 'nonnegative' + | 'multipleOf'; -export type ZodCustomValidatorKeys = 'use' | 'omit' | 'array'; +export type ZodCustomValidatorKeys = ZodArrayValidatorKeys | 'use' | 'omit'; export interface ScalarValidatorFnOpts { key: string; @@ -66,12 +85,12 @@ export const STRING_VALIDATOR_NUMBER_AND_MESSAGE_REGEX = /.(?min|max|length)(?\([\d]+([,][ ]?)?(?[{][ ]?message:[ ]?['"][\w\W]+['"][ ]?[}])?\))/; export const STRING_VALIDATOR_MESSAGE_REGEX = - /(?email|url|uuid|cuid|trim|datetime|noDefault)(\((?[{][ ]?message:[ ]?['"][\w\W]+['"][ ]?[}])?\))/; + /(?email|url|emoji|uuid|cuid|cuid2|ulid|ip|toLowerCase|toUpperCase|trim|datetime|noDefault)(\((?[{][ ]?message:[ ]?['"][\w\W]+['"][ ]?[}])?\))/; export const STRING_VALIDATOR_REGEX = /.(regex)(\((?.*)\))/; export const STRING_VALIDATOR_STRING_AND_MESSAGE_REGEX = - /.(?startsWith|endsWith)\((?['"][\w\W]+['"])([,][ ]?)?(?[{][ ]?message:[ ]?['"][\w\W]+['"][ ]?[}])?\)/; + /.(?startsWith|endsWith|includes)\((?['"][\w\W]+['"])([,][ ]?)?(?[{][ ]?message:[ ]?['"][\w\W]+['"][ ]?[}])?\)/; // NUMBER // ---------------------------------------- @@ -91,8 +110,11 @@ export const DATE_VALIDATOR_NUMBER_AND_MESSAGE_REGEX = // BIGINT // ---------------------------------------- +export const BIGINT_VALIDATOR_NUMBER_AND_MESSAGE_REGEX = + /.(?gt|gte|lt|lte|multipleOf)(?\([\w]+([,][ ]?)?(?[{][ ]?message:[ ]?['"][\w\W]+['"][ ]?[}])?\))/; + export const BIGINT_VALIDATOR_MESSAGE_REGEX = - /(?array)(\((?[{][ ]?message:[ ]?['"][\w\W]+['"][ ]?[}])?\))/; + /(?positive|nonnegative|negative|nonpositive|array)(\((?[{][ ]?message:[ ]?['"][\w\W]+['"][ ]?[}])?\))/; // CUSTOM // ---------------------------------------- @@ -122,18 +144,25 @@ export const ARRAY_VALIDATOR_MESSAGE_REGEX = */ export const STRING_VALIDATOR_REGEX_MAP: ValidatorMap = { - min: STRING_VALIDATOR_NUMBER_AND_MESSAGE_REGEX, max: STRING_VALIDATOR_NUMBER_AND_MESSAGE_REGEX, + min: STRING_VALIDATOR_NUMBER_AND_MESSAGE_REGEX, length: STRING_VALIDATOR_NUMBER_AND_MESSAGE_REGEX, email: STRING_VALIDATOR_MESSAGE_REGEX, url: STRING_VALIDATOR_MESSAGE_REGEX, + emoji: STRING_VALIDATOR_MESSAGE_REGEX, uuid: STRING_VALIDATOR_MESSAGE_REGEX, cuid: STRING_VALIDATOR_MESSAGE_REGEX, + cuid2: STRING_VALIDATOR_MESSAGE_REGEX, + ulid: STRING_VALIDATOR_MESSAGE_REGEX, regex: STRING_VALIDATOR_REGEX, + includes: STRING_VALIDATOR_STRING_AND_MESSAGE_REGEX, startsWith: STRING_VALIDATOR_STRING_AND_MESSAGE_REGEX, endsWith: STRING_VALIDATOR_STRING_AND_MESSAGE_REGEX, - trim: STRING_VALIDATOR_MESSAGE_REGEX, datetime: STRING_VALIDATOR_MESSAGE_REGEX, + ip: STRING_VALIDATOR_MESSAGE_REGEX, + trim: STRING_VALIDATOR_MESSAGE_REGEX, + toLowerCase: STRING_VALIDATOR_MESSAGE_REGEX, + toUpperCase: STRING_VALIDATOR_MESSAGE_REGEX, noDefault: STRING_VALIDATOR_MESSAGE_REGEX, array: ARRAY_VALIDATOR_MESSAGE_REGEX, }; @@ -184,6 +213,15 @@ export const DATE_VALIDATOR_REGEX_MAP: ValidatorMap = { export const BIGINT_VALIDATOR_REGEX_MAP: ValidatorMap = { + gt: BIGINT_VALIDATOR_NUMBER_AND_MESSAGE_REGEX, + gte: BIGINT_VALIDATOR_NUMBER_AND_MESSAGE_REGEX, + lt: BIGINT_VALIDATOR_NUMBER_AND_MESSAGE_REGEX, + lte: BIGINT_VALIDATOR_NUMBER_AND_MESSAGE_REGEX, + positive: BIGINT_VALIDATOR_MESSAGE_REGEX, + nonpositive: BIGINT_VALIDATOR_MESSAGE_REGEX, + negative: BIGINT_VALIDATOR_MESSAGE_REGEX, + nonnegative: BIGINT_VALIDATOR_MESSAGE_REGEX, + multipleOf: BIGINT_VALIDATOR_NUMBER_AND_MESSAGE_REGEX, array: ARRAY_VALIDATOR_MESSAGE_REGEX, }; @@ -194,14 +232,13 @@ export const CUSTOM_VALIDATOR_REGEX_MAP: ValidatorMap = array: ARRAY_VALIDATOR_MESSAGE_REGEX, }; -export const ENUM_VALIDATOR_REGEX_MAP: ValidatorMap = { +export const ENUM_VALIDATOR_REGEX_MAP: ValidatorMap = { array: ARRAY_VALIDATOR_MESSAGE_REGEX, }; -export const OBJECT_VALIDATOR_REGEX_MAP: ValidatorMap = - { - array: ARRAY_VALIDATOR_MESSAGE_REGEX, - }; +export const OBJECT_VALIDATOR_REGEX_MAP: ValidatorMap = { + array: ARRAY_VALIDATOR_MESSAGE_REGEX, +}; ///////////////////////////////////////////////// // CLASS diff --git a/packages/usage/package.json b/packages/usage/package.json index efeca5d7..07326ec2 100644 --- a/packages/usage/package.json +++ b/packages/usage/package.json @@ -29,7 +29,7 @@ "ts-node-dev": "^2.0.0", "validator": "^13.9.0", "vitest": "^0.25.8", - "zod": "^3.20.6", - "zod-prisma-types": "workspace:2.3.4" + "zod": "^3.21.4", + "zod-prisma-types": "workspace:2.3.6" } } diff --git a/packages/usage/prisma/schema.prisma b/packages/usage/prisma/schema.prisma index 9721e5f7..8351d599 100644 --- a/packages/usage/prisma/schema.prisma +++ b/packages/usage/prisma/schema.prisma @@ -107,7 +107,8 @@ model MyPrismaScalarsType { decimal Decimal decimalOpt Decimal? date DateTime @default(now()) - dateOpt DateTime? /// @zod.date({ invalid_type_error: "wrong date type" }) bigInt BigInt /// @zod.bigint({ invalid_type_error: "error" }) + dateOpt DateTime? /// @zod.date({ invalid_type_error: "wrong date type" }) + bigInt BigInt /// @zod.bigint({ invalid_type_error: "error" }).gt(5n, { message: "gt error" }) bigIntOpt BigInt? /// @zod.custom.use(z.lazy(() => InputJsonValue).refine((val) => myFunction(val), { message: 'Is not valid' })) json Json diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 49778531..47670fbd 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -25,12 +25,12 @@ importers: prettier: ^2.8.4 typescript: ^4.9.5 vitest: ^0.24.5 - zod: ^3.20.6 + zod: ^3.21.4 dependencies: '@prisma/generator-helper': 4.10.1 code-block-writer: 11.0.3 lodash: 4.17.21 - zod: 3.20.6 + zod: 3.21.4 devDependencies: '@prisma/internals': 4.10.1 '@types/lodash': 4.14.191 @@ -61,8 +61,8 @@ importers: typescript: ^4.9.5 validator: ^13.9.0 vitest: ^0.25.8 - zod: ^3.20.6 - zod-prisma-types: workspace:2.3.4 + zod: ^3.21.4 + zod-prisma-types: workspace:2.3.6 dependencies: '@prisma/client': 4.11.0_prisma@4.11.0 '@trpc/client': 10.14.0_@trpc+server@10.14.0 @@ -75,7 +75,7 @@ importers: ts-node-dev: 2.0.0_2cogyjchoknpkalymtikkc6nay validator: 13.9.0 vitest: 0.25.8 - zod: 3.20.6 + zod: 3.21.4 zod-prisma-types: link:../generator devDependencies: '@types/node': 18.14.5 @@ -3154,7 +3154,7 @@ packages: '@prisma/internals': 4.11.0 prettier: 2.8.4 tslib: 2.5.0 - zod: 3.20.6 + zod: 3.21.4 transitivePeerDependencies: - encoding - prisma @@ -4138,6 +4138,6 @@ packages: compress-commons: 4.1.1 readable-stream: 3.6.1 - /zod/3.20.6: - resolution: {integrity: sha512-oyu0m54SGCtzh6EClBVqDDlAYRz4jrVtKwQ7ZnsEmMI9HnzuZFj8QFwAY1M5uniIYACdGvv0PBWPF2kO0aNofA==} + /zod/3.21.4: + resolution: {integrity: sha512-m46AKbrzKVzOzs/DZgVnG5H55N1sv1M8qZU3A8RIKbs3mrACDNeIOeilDymVb2HdmP8uwshOCF4uJ8uM9rCqJw==} dev: false