@@ -62,7 +62,8 @@ import {
62
62
ArrayNotContains ,
63
63
ArrayUnique ,
64
64
IsArray ,
65
- IsDateString
65
+ IsDateString ,
66
+ IsInstance
66
67
} from "../../src/decorator/decorators" ;
67
68
import { Validator } from "../../src/validation/Validator" ;
68
69
import { ValidatorOptions } from "../../src/validation/ValidatorOptions" ;
@@ -2989,7 +2990,7 @@ describe("ArrayMaxSize", function() {
2989
2990
2990
2991
} ) ;
2991
2992
2992
- describe ( "ArrayUnique" , function ( ) {
2993
+ describe ( "ArrayUnique" , function ( ) {
2993
2994
2994
2995
const validValues = [ [ "world" , "hello" , "superman" ] , [ "world" , "superman" , "hello" ] , [ "superman" , "world" , "hello" ] ] ;
2995
2996
const invalidValues : any [ ] = [ null , undefined , [ "world" , "hello" , "hello" ] , [ "world" , "hello" , "world" ] , [ "1" , "1" , "1" ] ] ;
@@ -2999,6 +3000,43 @@ describe("ArrayUnique", function() {
2999
3000
someProperty : string [ ] ;
3000
3001
}
3001
3002
3003
+ it ( "should not fail if validator.validate said that its valid" , function ( done ) {
3004
+ checkValidValues ( new MyClass ( ) , validValues , done ) ;
3005
+ } ) ;
3006
+
3007
+ it ( "should fail if validator.validate said that its invalid" , function ( done ) {
3008
+ checkInvalidValues ( new MyClass ( ) , invalidValues , done ) ;
3009
+ } ) ;
3010
+
3011
+ it ( "should not fail if method in validator said that its valid" , function ( ) {
3012
+ validValues . forEach ( value => validator . arrayUnique ( value ) . should . be . true ) ;
3013
+ } ) ;
3014
+
3015
+ it ( "should fail if method in validator said that its invalid" , function ( ) {
3016
+ invalidValues . forEach ( value => validator . arrayUnique ( value ) . should . be . false ) ;
3017
+ } ) ;
3018
+
3019
+ it ( "should return error object with proper data" , function ( done ) {
3020
+ const validationType = "arrayUnique" ;
3021
+ const message = "All someProperty's elements must be unique" ;
3022
+ checkReturnedError ( new MyClass ( ) , invalidValues , validationType , message , done ) ;
3023
+ } ) ;
3024
+
3025
+ } ) ;
3026
+
3027
+ describe ( "isInstance" , function ( ) {
3028
+
3029
+ class MySubClass { }
3030
+ class WrongSubClass { }
3031
+
3032
+ class MyClass {
3033
+ @IsInstance ( MySubClass )
3034
+ someProperty : MySubClass ;
3035
+ }
3036
+
3037
+ const validValues = [ new MySubClass ( ) ] ;
3038
+ const invalidValues = [ null , undefined , 15 , "something" , new WrongSubClass ( ) , ( ) => < any > null ] ;
3039
+
3002
3040
it ( "should not fail if validator.validate said that its valid" , function ( done ) {
3003
3041
checkValidValues ( new MyClass ( ) , validValues , done ) ;
3004
3042
} ) ;
@@ -3008,16 +3046,16 @@ describe("ArrayUnique", function() {
3008
3046
} ) ;
3009
3047
3010
3048
it ( "should not fail if method in validator said that its valid" , function ( ) {
3011
- validValues . forEach ( value => validator . arrayUnique ( value ) . should . be . true ) ;
3049
+ validValues . forEach ( value => validator . isInstance ( value , MySubClass ) . should . be . true ) ;
3012
3050
} ) ;
3013
3051
3014
3052
it ( "should fail if method in validator said that its invalid" , function ( ) {
3015
- invalidValues . forEach ( value => validator . arrayUnique ( value ) . should . be . false ) ;
3053
+ invalidValues . forEach ( value => validator . isInstance ( value , MySubClass ) . should . be . false ) ;
3016
3054
} ) ;
3017
3055
3018
3056
it ( "should return error object with proper data" , function ( done ) {
3019
- const validationType = "arrayUnique " ;
3020
- const message = "All someProperty's elements must be unique " ;
3057
+ const validationType = "isInstance " ;
3058
+ const message = "someProperty must be an instance of MySubClass " ;
3021
3059
checkReturnedError ( new MyClass ( ) , invalidValues , validationType , message , done ) ;
3022
3060
} ) ;
3023
3061
0 commit comments