@@ -16,6 +16,7 @@ import { enumerate } from '../../../utils/enumerate';
1616import { extractFields } from '../../../utils/object-utils' ;
1717import { formatError } from '../../../utils/zod-utils' ;
1818import { AGGREGATE_OPERATORS , LOGICAL_COMBINATORS , NUMERIC_FIELD_TYPES } from '../../constants' ;
19+ import type { ClientContract } from '../../contract' ;
1920import {
2021 type AggregateArgs ,
2122 type CountArgs ,
@@ -53,7 +54,15 @@ type GetSchemaFunc<Schema extends SchemaDef, Options> = (model: GetModels<Schema
5354export class InputValidator < Schema extends SchemaDef > {
5455 private schemaCache = new Map < string , ZodType > ( ) ;
5556
56- constructor ( private readonly schema : Schema ) { }
57+ constructor ( private readonly client : ClientContract < Schema > ) { }
58+
59+ private get schema ( ) {
60+ return this . client . $schema ;
61+ }
62+
63+ private get extraValidationsEnabled ( ) {
64+ return this . client . $options . validateInput !== false ;
65+ }
5766
5867 validateFindArgs ( model : GetModels < Schema > , args : unknown , options : { unique : boolean ; findOne : boolean } ) {
5968 return this . validate <
@@ -251,23 +260,31 @@ export class InputValidator<Schema extends SchemaDef> {
251260 return this . makeTypeDefSchema ( type ) ;
252261 } else {
253262 return match ( type )
254- . with ( 'String' , ( ) => addStringValidation ( z . string ( ) , attributes ) )
255- . with ( 'Int' , ( ) => addNumberValidation ( z . number ( ) . int ( ) , attributes ) )
256- . with ( 'Float' , ( ) => addNumberValidation ( z . number ( ) , attributes ) )
263+ . with ( 'String' , ( ) =>
264+ this . extraValidationsEnabled ? addStringValidation ( z . string ( ) , attributes ) : z . string ( ) ,
265+ )
266+ . with ( 'Int' , ( ) =>
267+ this . extraValidationsEnabled ? addNumberValidation ( z . number ( ) . int ( ) , attributes ) : z . number ( ) . int ( ) ,
268+ )
269+ . with ( 'Float' , ( ) =>
270+ this . extraValidationsEnabled ? addNumberValidation ( z . number ( ) , attributes ) : z . number ( ) ,
271+ )
257272 . with ( 'Boolean' , ( ) => z . boolean ( ) )
258273 . with ( 'BigInt' , ( ) =>
259274 z . union ( [
260- addNumberValidation ( z . number ( ) . int ( ) , attributes ) ,
261- addBigIntValidation ( z . bigint ( ) , attributes ) ,
262- ] ) ,
263- )
264- . with ( 'Decimal' , ( ) =>
265- z . union ( [
266- addNumberValidation ( z . number ( ) , attributes ) ,
267- addDecimalValidation ( z . instanceof ( Decimal ) , attributes ) ,
268- addDecimalValidation ( z . string ( ) , attributes ) ,
275+ this . extraValidationsEnabled
276+ ? addNumberValidation ( z . number ( ) . int ( ) , attributes )
277+ : z . number ( ) . int ( ) ,
278+ this . extraValidationsEnabled ? addBigIntValidation ( z . bigint ( ) , attributes ) : z . bigint ( ) ,
269279 ] ) ,
270280 )
281+ . with ( 'Decimal' , ( ) => {
282+ return z . union ( [
283+ this . extraValidationsEnabled ? addNumberValidation ( z . number ( ) , attributes ) : z . number ( ) ,
284+ addDecimalValidation ( z . instanceof ( Decimal ) , attributes , this . extraValidationsEnabled ) ,
285+ addDecimalValidation ( z . string ( ) , attributes , this . extraValidationsEnabled ) ,
286+ ] ) ;
287+ } )
271288 . with ( 'DateTime' , ( ) => z . union ( [ z . date ( ) , z . string ( ) . datetime ( ) ] ) )
272289 . with ( 'Bytes' , ( ) => z . instanceof ( Uint8Array ) )
273290 . otherwise ( ( ) => z . unknown ( ) ) ;
@@ -913,8 +930,12 @@ export class InputValidator<Schema extends SchemaDef> {
913930 }
914931 } ) ;
915932
916- const uncheckedCreateSchema = addCustomValidation ( z . strictObject ( uncheckedVariantFields ) , modelDef . attributes ) ;
917- const checkedCreateSchema = addCustomValidation ( z . strictObject ( checkedVariantFields ) , modelDef . attributes ) ;
933+ const uncheckedCreateSchema = this . extraValidationsEnabled
934+ ? addCustomValidation ( z . strictObject ( uncheckedVariantFields ) , modelDef . attributes )
935+ : z . strictObject ( uncheckedVariantFields ) ;
936+ const checkedCreateSchema = this . extraValidationsEnabled
937+ ? addCustomValidation ( z . strictObject ( checkedVariantFields ) , modelDef . attributes )
938+ : z . strictObject ( checkedVariantFields ) ;
918939
919940 if ( ! hasRelation ) {
920941 return this . orArray ( uncheckedCreateSchema , canBeArray ) ;
@@ -1193,8 +1214,12 @@ export class InputValidator<Schema extends SchemaDef> {
11931214 }
11941215 } ) ;
11951216
1196- const uncheckedUpdateSchema = addCustomValidation ( z . strictObject ( uncheckedVariantFields ) , modelDef . attributes ) ;
1197- const checkedUpdateSchema = addCustomValidation ( z . strictObject ( checkedVariantFields ) , modelDef . attributes ) ;
1217+ const uncheckedUpdateSchema = this . extraValidationsEnabled
1218+ ? addCustomValidation ( z . strictObject ( uncheckedVariantFields ) , modelDef . attributes )
1219+ : z . strictObject ( uncheckedVariantFields ) ;
1220+ const checkedUpdateSchema = this . extraValidationsEnabled
1221+ ? addCustomValidation ( z . strictObject ( checkedVariantFields ) , modelDef . attributes )
1222+ : z . strictObject ( checkedVariantFields ) ;
11981223 if ( ! hasRelation ) {
11991224 return uncheckedUpdateSchema ;
12001225 } else {
0 commit comments