@@ -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,37 @@ 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+ const options : [ z . ZodSchema , z . ZodSchema , ...z . ZodSchema [ ] ] = [
283+ z . number ( ) ,
284+ z . instanceof ( Decimal ) ,
285+ z . string ( ) ,
286+ ] ;
287+ if ( this . extraValidationsEnabled ) {
288+ for ( let i = 0 ; i < options . length ; i ++ ) {
289+ options [ i ] = addDecimalValidation ( options [ i ] ! , attributes ) ;
290+ }
291+ }
292+ return z . union ( options ) ;
293+ } )
271294 . with ( 'DateTime' , ( ) => z . union ( [ z . date ( ) , z . string ( ) . datetime ( ) ] ) )
272295 . with ( 'Bytes' , ( ) => z . instanceof ( Uint8Array ) )
273296 . otherwise ( ( ) => z . unknown ( ) ) ;
0 commit comments