@@ -42,8 +42,14 @@ describe('Validation at REST level', () => {
42
42
43
43
const PRODUCT_SPEC = jsonToSchemaObject ( getJsonSchema ( Product ) ) ;
44
44
45
+ // Add a schema that requires `description`
46
+ const PRODUCT_SPEC_WITH_DESCRIPTION = jsonToSchemaObject (
47
+ getJsonSchema ( Product ) ,
48
+ ) ;
49
+ PRODUCT_SPEC_WITH_DESCRIPTION . required ! . push ( 'description' ) ;
50
+
45
51
// This is the standard use case that most LB4 applications should use.
46
- // The request body specification is infered from a decorated model class.
52
+ // The request body specification is inferred from a decorated model class.
47
53
context ( 'for request body specified via model definition' , ( ) => {
48
54
class ProductController {
49
55
@post ( '/products' )
@@ -87,7 +93,7 @@ describe('Validation at REST level', () => {
87
93
} ) ;
88
94
} ) ;
89
95
90
- // A request body schema can be provied explicitly by the user
96
+ // A request body schema can be provided explicitly by the user
91
97
// as an inlined content[type].schema property.
92
98
context ( 'for fully-specified request body' , ( ) => {
93
99
class ProductControllerWithFullSchema {
@@ -111,6 +117,37 @@ describe('Validation at REST level', () => {
111
117
serverRejectsRequestWithMissingRequiredValues ( ) ) ;
112
118
} ) ;
113
119
120
+ context ( 'for different schemas per media type' , ( ) => {
121
+ let spec = aBodySpec ( PRODUCT_SPEC , { } , 'application/json' ) ;
122
+ spec = aBodySpec (
123
+ PRODUCT_SPEC_WITH_DESCRIPTION ,
124
+ spec ,
125
+ 'application/x-www-form-urlencoded' ,
126
+ ) ;
127
+ class ProductControllerWithFullSchema {
128
+ @post ( '/products' )
129
+ async create (
130
+ @requestBody ( spec ) data : object ,
131
+ // ^^^^^^
132
+ // use "object" instead of "Product" to verify the situation when
133
+ // body schema cannot be inferred from the argument type
134
+ ) : Promise < Product > {
135
+ return new Product ( data ) ;
136
+ }
137
+ }
138
+
139
+ before ( ( ) => givenAnAppAndAClient ( ProductControllerWithFullSchema ) ) ;
140
+ after ( ( ) => app . stop ( ) ) ;
141
+
142
+ it ( 'accepts valid values for json' , ( ) => serverAcceptsValidRequestBody ( ) ) ;
143
+
144
+ it ( 'accepts valid values for urlencoded' , ( ) =>
145
+ serverAcceptsValidRequestBodyForUrlencoded ( ) ) ;
146
+
147
+ it ( 'rejects missing required properties for urlencoded' , ( ) =>
148
+ serverRejectsMissingDescriptionForUrlencoded ( ) ) ;
149
+ } ) ;
150
+
114
151
// A request body schema can be provided explicitly by the user as a reference
115
152
// to a schema shared in the global `components.schemas` object.
116
153
context ( 'for request body specified via a reference' , ( ) => {
@@ -156,6 +193,29 @@ describe('Validation at REST level', () => {
156
193
. expect ( 200 , DATA ) ;
157
194
}
158
195
196
+ async function serverAcceptsValidRequestBodyForUrlencoded ( ) {
197
+ const DATA =
198
+ 'name=Pencil&price=10&description=An optional description of a pencil' ;
199
+ await client
200
+ . post ( '/products' )
201
+ . set ( 'Content-Type' , 'application/x-www-form-urlencoded' )
202
+ . send ( DATA )
203
+ . expect ( 200 , {
204
+ name : 'Pencil' ,
205
+ description : 'An optional description of a pencil' ,
206
+ price : 10 ,
207
+ } ) ;
208
+ }
209
+
210
+ async function serverRejectsMissingDescriptionForUrlencoded ( ) {
211
+ const DATA = 'name=Pencil&price=10' ;
212
+ await client
213
+ . post ( '/products' )
214
+ . set ( 'Content-Type' , 'application/x-www-form-urlencoded' )
215
+ . send ( DATA )
216
+ . expect ( 422 ) ;
217
+ }
218
+
159
219
async function serverRejectsRequestWithMissingRequiredValues ( ) {
160
220
await client
161
221
. post ( '/products' )
0 commit comments