@@ -41,22 +41,39 @@ describe("custom decorators", function() {
41
41
const relatedValue = ( args . object as any ) [ relatedPropertyName ] ;
42
42
if ( relatedValue === undefined || relatedValue === null )
43
43
return true ;
44
-
45
- return typeof value === "string" &&
44
+
45
+ const result = typeof value === "string" &&
46
46
typeof relatedValue === "string" &&
47
47
value . length > relatedValue . length ;
48
+
49
+ const asPromise = validationOptions &&
50
+ validationOptions . context &&
51
+ validationOptions . context . promise ;
52
+
53
+ return asPromise ? Promise . resolve ( result ) : result ;
48
54
}
49
55
}
50
56
} ) ;
51
57
} ;
52
58
}
53
-
59
+
54
60
class MyClass {
55
61
@IsLongerThan ( "lastName" , {
62
+ context : { foo : "bar" } ,
56
63
message : "$property must be longer then $constraint1. Given value: $value"
57
64
} )
58
65
firstName : string ;
59
-
66
+
67
+ lastName : string ;
68
+ }
69
+
70
+ class MyClassWithAsyncValidator {
71
+ @IsLongerThan ( "lastName" , {
72
+ context : { foo : "bar" , promise : true } ,
73
+ message : "$property must be longer then $constraint1. Given value: $value"
74
+ } )
75
+ firstName : string ;
76
+
60
77
lastName : string ;
61
78
}
62
79
@@ -87,9 +104,25 @@ describe("custom decorators", function() {
87
104
errors [ 0 ] . constraints . should . be . eql ( { isLongerThan : "firstName must be longer then lastName. Given value: Li" } ) ;
88
105
} ) ;
89
106
} ) ;
90
-
107
+
108
+ it ( "should include context" , function ( ) {
109
+ const model = new MyClass ( ) ;
110
+ const asyncModel = new MyClassWithAsyncValidator ( ) ;
111
+ model . firstName = asyncModel . firstName = "Paul" ;
112
+ model . lastName = asyncModel . lastName = "Walker" ;
113
+
114
+ return validator . validate ( model ) . then ( errors => {
115
+ errors . length . should . be . equal ( 1 ) ;
116
+ errors [ 0 ] . contexts . should . be . eql ( { isLongerThan : { foo : "bar" } } ) ;
117
+
118
+ return validator . validate ( asyncModel ) . then ( errors => {
119
+ errors . length . should . be . equal ( 1 ) ;
120
+ errors [ 0 ] . contexts . should . have . nested . property ( "isLongerThan.foo" , "bar" ) ;
121
+ } ) ;
122
+ } ) ;
123
+ } ) ;
91
124
} ) ;
92
-
125
+
93
126
describe ( "decorator with default message" , function ( ) {
94
127
95
128
function IsLonger ( property : string , validationOptions ?: ValidationOptions ) {
@@ -106,7 +139,7 @@ describe("custom decorators", function() {
106
139
const relatedValue = ( args . object as any ) [ relatedPropertyName ] ;
107
140
if ( relatedValue === undefined || relatedValue === null )
108
141
return true ;
109
-
142
+
110
143
return typeof value === "string" &&
111
144
typeof relatedValue === "string" &&
112
145
value . length > relatedValue . length ;
@@ -118,11 +151,11 @@ describe("custom decorators", function() {
118
151
} ) ;
119
152
} ;
120
153
}
121
-
154
+
122
155
class SecondClass {
123
156
@IsLonger ( "lastName" )
124
157
firstName : string ;
125
-
158
+
126
159
lastName : string ;
127
160
}
128
161
@@ -153,7 +186,7 @@ describe("custom decorators", function() {
153
186
errors [ 0 ] . constraints . should . be . eql ( { isLonger : "firstName must be longer then lastName" } ) ;
154
187
} ) ;
155
188
} ) ;
156
-
189
+
157
190
} ) ;
158
191
159
192
describe ( "decorator with separate validation constraint class" , function ( ) {
@@ -166,7 +199,7 @@ describe("custom decorators", function() {
166
199
const relatedValue = ( args . object as any ) [ relatedPropertyName ] ;
167
200
if ( value === null || value === undefined )
168
201
return true ;
169
-
202
+
170
203
return typeof value === "string" &&
171
204
typeof relatedValue === "string" &&
172
205
value . length < relatedValue . length ;
0 commit comments