11'use strict' ;
22
3+ const SchemaEndpointResolver = require ( './utils/schemaEndpointResolver' ) ;
4+
35const InputValidationError = require ( './inputValidationError' ) ,
46 apiSchemaBuilder = require ( 'api-schema-builder' ) ;
57const allowedFrameworks = [ 'express' , 'koa' ] ;
68
79let schemas = { } ;
810let middlewareOptions ;
911let framework ;
12+ let schemaEndpointResolver ;
1013
1114function init ( swaggerPath , options ) {
1215 middlewareOptions = options || { } ;
@@ -15,9 +18,10 @@ function init(swaggerPath, options) {
1518 } ) ;
1619
1720 framework = frameworkToLoad ? require ( `./frameworks/${ frameworkToLoad } ` ) : require ( './frameworks/express' ) ;
21+ schemaEndpointResolver = new SchemaEndpointResolver ( ) ;
1822
1923 // build schema for requests only
20- let schemaBuilderOptions = Object . assign ( { } , options , { buildRequests : true , buildResponses : false } ) ;
24+ let schemaBuilderOptions = Object . assign ( { } , options , { buildRequests : true , buildResponses : false } ) ;
2125 return apiSchemaBuilder . buildSchema ( swaggerPath , schemaBuilderOptions ) . then ( ( receivedSchemas ) => {
2226 schemas = receivedSchemas ;
2327 } ) ;
@@ -52,17 +56,19 @@ function _validateRequest(requestOptions) {
5256
5357function _validateBody ( body , path , method ) {
5458 return new Promise ( function ( resolve , reject ) {
55- if ( schemas [ path ] && schemas [ path ] [ method ] && schemas [ path ] [ method ] . body && ! schemas [ path ] [ method ] . body . validate ( body ) ) {
56- return reject ( schemas [ path ] [ method ] . body . errors ) ;
59+ const methodSchema = schemaEndpointResolver . getMethodSchema ( schemas , path , method ) ;
60+ if ( methodSchema && methodSchema . body && ! methodSchema . body . validate ( body ) ) {
61+ return reject ( methodSchema . body . errors ) ;
5762 }
5863 return resolve ( ) ;
5964 } ) ;
6065}
6166
6267function _validateParams ( headers , pathParams , query , files , path , method ) {
6368 return new Promise ( function ( resolve , reject ) {
64- if ( schemas [ path ] && schemas [ path ] [ method ] && schemas [ path ] [ method ] . parameters && ! schemas [ path ] [ method ] . parameters . validate ( { query : query , headers : headers , path : pathParams , files : files } ) ) {
65- return reject ( schemas [ path ] [ method ] . parameters . errors ) ;
69+ const methodSchema = schemaEndpointResolver . getMethodSchema ( schemas , path , method ) ;
70+ if ( methodSchema && methodSchema . parameters && ! methodSchema . parameters . validate ( { query : query , headers : headers , path : pathParams , files : files } ) ) {
71+ return reject ( methodSchema . parameters . errors ) ;
6672 }
6773
6874 return resolve ( ) ;
0 commit comments