@@ -5,7 +5,7 @@ import { ConnectionType, Integration } from './integration';
5
5
import { MockIntegration } from './integrations/mock' ;
6
6
import { MethodResponse } from './methodresponse' ;
7
7
import { IModel } from './model' ;
8
- import { IRequestValidator } from './requestvalidator' ;
8
+ import { IRequestValidator , RequestValidatorOptions } from './requestvalidator' ;
9
9
import { IResource } from './resource' ;
10
10
import { RestApi } from './restapi' ;
11
11
import { validateHttpMethod } from './util' ;
@@ -73,6 +73,8 @@ export interface MethodOptions {
73
73
74
74
/**
75
75
* The ID of the associated request validator.
76
+ * Only one of `requestValidator` or `requestValidatorOptions` must be specified.
77
+ * @default - No default validator
76
78
*/
77
79
readonly requestValidator ?: IRequestValidator ;
78
80
@@ -83,6 +85,13 @@ export interface MethodOptions {
83
85
* @default - no authorization scopes
84
86
*/
85
87
readonly authorizationScopes ?: string [ ]
88
+
89
+ /**
90
+ * Request validator options to create new validator
91
+ * Only one of `requestValidator` or `requestValidatorOptions` must be specified.
92
+ * @default - No default validator
93
+ */
94
+ readonly requestValidatorOptions ?: RequestValidatorOptions ;
86
95
}
87
96
88
97
export interface MethodProps {
@@ -160,7 +169,7 @@ export class Method extends Resource {
160
169
integration : this . renderIntegration ( props . integration ) ,
161
170
methodResponses : this . renderMethodResponses ( options . methodResponses ) ,
162
171
requestModels : this . renderRequestModels ( options . requestModels ) ,
163
- requestValidatorId : options . requestValidator ? options . requestValidator . requestValidatorId : undefined ,
172
+ requestValidatorId : this . requestValidatorId ( options ) ,
164
173
authorizationScopes : options . authorizationScopes ?? defaultMethodOptions . authorizationScopes ,
165
174
} ;
166
175
@@ -302,6 +311,20 @@ export class Method extends Resource {
302
311
303
312
return models ;
304
313
}
314
+
315
+ private requestValidatorId ( options : MethodOptions ) : string | undefined {
316
+ if ( options . requestValidator && options . requestValidatorOptions ) {
317
+ throw new Error ( `Only one of 'requestValidator' or 'requestValidatorOptions' must be specified.` ) ;
318
+ }
319
+
320
+ if ( options . requestValidatorOptions ) {
321
+ const validator = this . restApi . addRequestValidator ( 'validator' , options . requestValidatorOptions ) ;
322
+ return validator . requestValidatorId ;
323
+ }
324
+
325
+ // For backward compatibility
326
+ return options . requestValidator ?. requestValidatorId ;
327
+ }
305
328
}
306
329
307
330
export enum AuthorizationType {
0 commit comments