@@ -10,8 +10,7 @@ import {
1010 ValidateNested ,
1111 ValidatorConstraint ,
1212 IsOptional ,
13- IsNotEmpty ,
14- Allow ,
13+ Min ,
1514} from '../../src/decorator/decorators' ;
1615import { Validator } from '../../src/validation/Validator' ;
1716import {
@@ -20,6 +19,7 @@ import {
2019 ValidationError ,
2120 ValidationOptions ,
2221 ValidatorConstraintInterface ,
22+ isValidationOptions ,
2323} from '../../src' ;
2424
2525const validator = new Validator ( ) ;
@@ -1285,3 +1285,70 @@ describe('context', () => {
12851285 return Promise . all ( [ hasStopAtFirstError , hasNotStopAtFirstError ] ) ;
12861286 } ) ;
12871287} ) ;
1288+
1289+ describe ( 'validateIf' , ( ) => {
1290+ class MyClass {
1291+ @Min ( 5 , {
1292+ message : 'min' ,
1293+ validateIf : ( obj : MyClass , value ) => {
1294+ return ! obj . someOtherProperty || obj . someOtherProperty === 'min' ;
1295+ } ,
1296+ } )
1297+ @Max ( 3 , {
1298+ message : 'max' ,
1299+ validateIf : ( o : MyClass ) => ! o . someOtherProperty || o . someOtherProperty === 'max' ,
1300+ } )
1301+ someProperty : number ;
1302+
1303+ someOtherProperty : string ;
1304+ }
1305+
1306+ describe ( 'should validate if validateIf return true.' , ( ) => {
1307+ it ( 'should be true' , ( ) => {
1308+ const result = isValidationOptions ( {
1309+ validateIf : ( obj : MyClass , value ) => {
1310+ return obj . someOtherProperty ;
1311+ } ,
1312+ } ) ;
1313+ expect ( result ) . toEqual ( true ) ;
1314+ } ) ;
1315+
1316+ it ( 'should only validate min' , ( ) => {
1317+ const model = new MyClass ( ) ;
1318+ model . someProperty = 4 ;
1319+ model . someOtherProperty = 'min' ;
1320+ return validator . validate ( model ) . then ( errors => {
1321+ expect ( errors . length ) . toEqual ( 1 ) ;
1322+ expect ( errors [ 0 ] . constraints [ 'min' ] ) . toBe ( 'min' ) ;
1323+ expect ( errors [ 0 ] . constraints [ 'max' ] ) . toBe ( undefined ) ;
1324+ } ) ;
1325+ } ) ;
1326+ it ( 'should only validate max' , ( ) => {
1327+ const model = new MyClass ( ) ;
1328+ model . someProperty = 4 ;
1329+ model . someOtherProperty = 'max' ;
1330+ return validator . validate ( model ) . then ( errors => {
1331+ expect ( errors . length ) . toEqual ( 1 ) ;
1332+ expect ( errors [ 0 ] . constraints [ 'min' ] ) . toBe ( undefined ) ;
1333+ expect ( errors [ 0 ] . constraints [ 'max' ] ) . toBe ( 'max' ) ;
1334+ } ) ;
1335+ } ) ;
1336+ it ( 'should validate both' , ( ) => {
1337+ const model = new MyClass ( ) ;
1338+ model . someProperty = 4 ;
1339+ return validator . validate ( model ) . then ( errors => {
1340+ expect ( errors . length ) . toEqual ( 1 ) ;
1341+ expect ( errors [ 0 ] . constraints [ 'min' ] ) . toBe ( 'min' ) ;
1342+ expect ( errors [ 0 ] . constraints [ 'max' ] ) . toBe ( 'max' ) ;
1343+ } ) ;
1344+ } ) ;
1345+ it ( 'should validate none' , ( ) => {
1346+ const model = new MyClass ( ) ;
1347+ model . someProperty = 4 ;
1348+ model . someOtherProperty = 'other' ;
1349+ return validator . validate ( model ) . then ( errors => {
1350+ expect ( errors . length ) . toEqual ( 0 ) ;
1351+ } ) ;
1352+ } ) ;
1353+ } ) ;
1354+ } ) ;
0 commit comments