@@ -6,15 +6,24 @@ import {
6
6
IsEmail ,
7
7
IsInt ,
8
8
IsNotEmpty ,
9
+ IsOptional ,
10
+ IsString ,
9
11
IsUUID ,
10
12
Min ,
13
+ ValidateNested ,
11
14
} from 'class-validator' ;
12
15
import { expectTypeOf } from 'expect-type' ;
13
16
import { describe , expect , test } from 'vitest' ;
14
17
15
18
import { assert , validate , wrap } from '..' ;
16
19
17
20
describe ( 'class-validator' , ( ) => {
21
+ class NestedSchema {
22
+ @IsString ( )
23
+ @IsNotEmpty ( )
24
+ value ! : string ;
25
+ }
26
+
18
27
class Schema {
19
28
@IsInt ( )
20
29
@Min ( 0 )
@@ -34,6 +43,12 @@ describe('class-validator', () => {
34
43
35
44
@IsDateString ( )
36
45
updatedAt ! : string ;
46
+
47
+ @ValidateNested ( )
48
+ nested ! : NestedSchema ;
49
+ @IsOptional ( )
50
+ @ValidateNested ( )
51
+ nestedArray ?: NestedSchema [ ] ;
37
52
}
38
53
const schema = Schema ;
39
54
@@ -44,14 +59,26 @@ describe('class-validator', () => {
44
59
id : 'c4a760a8-dbcf-4e14-9f39-645a8e933d74' ,
45
60
name : 'John Doe' ,
46
61
updatedAt : '2021-01-01T00:00:00.000Z' ,
47
- } ;
62
+ } as Schema ;
63
+
48
64
const badData = {
49
65
age : '123' ,
50
66
createdAt : '2021-01-01T00:00:00.000Z' ,
51
67
52
68
id : 'c4a760a8-dbcf-4e14-9f39-645a8e933d74' ,
53
69
name : 'John Doe' ,
70
+ updatedAt : '2021-01-01T00:00:00.000Z'
71
+ } ;
72
+
73
+ const badNestedData = {
74
+ age : 123 ,
75
+ createdAt : '2021-01-01T00:00:00.000Z' ,
76
+
77
+ id : 'c4a760a8-dbcf-4e14-9f39-645a8e933d74' ,
78
+ name : 'John Doe' ,
54
79
updatedAt : '2021-01-01T00:00:00.000Z' ,
80
+ nested : new NestedSchema ( ) ,
81
+ nestedArray : [ new NestedSchema ( ) ] ,
55
82
} ;
56
83
57
84
test ( 'infer' , ( ) => {
@@ -67,11 +94,35 @@ describe('class-validator', () => {
67
94
expect ( await validate ( schema , badData ) ) . toStrictEqual ( {
68
95
issues : [
69
96
{
70
- message : `An instance of Schema has failed the validation:
71
- - property age has failed the following constraints: min, isInt
72
- ` ,
97
+ message : 'age must not be less than 0' ,
73
98
path : [ 'age' ] ,
74
99
} ,
100
+ {
101
+ message : 'age must be an integer number' ,
102
+ path : [ 'age' ] ,
103
+ }
104
+ ] ,
105
+ success : false ,
106
+ } ) ;
107
+
108
+ expect ( await validate ( schema , badNestedData ) ) . toStrictEqual ( {
109
+ issues : [
110
+ {
111
+ message : 'value should not be empty' ,
112
+ path : [ 'nested.value' ] ,
113
+ } ,
114
+ {
115
+ message : 'value must be a string' ,
116
+ path : [ 'nested.value' ] ,
117
+ } ,
118
+ {
119
+ message : 'value should not be empty' ,
120
+ path : [ 'nestedArray[0].value' ] ,
121
+ } ,
122
+ {
123
+ message : 'value must be a string' ,
124
+ path : [ 'nestedArray[0].value' ] ,
125
+ }
75
126
] ,
76
127
success : false ,
77
128
} ) ;
0 commit comments