@@ -29,7 +29,7 @@ function init(swaggerPath, options) {
2929 SwaggerParser . dereference ( swaggerPath ) ,
3030 SwaggerParser . parse ( swaggerPath )
3131 ] ) . then ( function ( swaggers ) {
32- var dereferenced = swaggers [ 0 ] ;
32+ const dereferenced = swaggers [ 0 ] ;
3333 Object . keys ( dereferenced . paths ) . forEach ( function ( currentPath ) {
3434 let pathParameters = dereferenced . paths [ currentPath ] . parameters || [ ] ;
3535 let parsedPath = dereferenced . basePath && dereferenced . basePath !== '/' ? dereferenced . basePath . concat ( currentPath . replace ( / { / g, ':' ) . replace ( / } / g, '' ) ) : currentPath . replace ( / { / g, ':' ) . replace ( / } / g, '' ) ;
@@ -48,25 +48,7 @@ function init(swaggerPath, options) {
4848 schemaPreprocessor . makeOptionalAttributesNullable ( bodySchema ) ;
4949 }
5050 if ( bodySchema . length > 0 ) {
51- let validatedBodySchema ;
52- if ( bodySchema [ 0 ] . in === 'body' ) {
53- validatedBodySchema = bodySchema [ 0 ] . schema ;
54- } else if ( bodySchema [ 0 ] . in === 'formData' ) {
55- validatedBodySchema = {
56- required : [ ] ,
57- properties : { }
58- } ;
59- bodySchema . forEach ( ( formField ) => {
60- if ( formField . type !== 'file' ) {
61- validatedBodySchema . properties [ formField . name ] = {
62- type : formField . type
63- } ;
64- if ( formField . required ) {
65- validatedBodySchema . required . push ( formField . name ) ;
66- }
67- }
68- } ) ;
69- }
51+ const validatedBodySchema = _getValidatedBodySchema ( bodySchema ) ;
7052 schemas [ parsedPath ] [ currentMethod ] . body = buildBodyValidation ( validatedBodySchema , dereferenced . definitions , swaggers [ 1 ] , currentPath , currentMethod , parsedPath ) ;
7153 }
7254
@@ -86,6 +68,31 @@ function init(swaggerPath, options) {
8668 } ) ;
8769}
8870
71+ function _getValidatedBodySchema ( bodySchema ) {
72+ let validatedBodySchema ;
73+ if ( bodySchema [ 0 ] . in === 'body' ) {
74+ // if we are processing schema for a simple JSON payload, no additional processing needed
75+ validatedBodySchema = bodySchema [ 0 ] . schema ;
76+ } else if ( bodySchema [ 0 ] . in === 'formData' ) {
77+ // if we are processing multipart form, assemble body schema from form field schemas
78+ validatedBodySchema = {
79+ required : [ ] ,
80+ properties : { }
81+ } ;
82+ bodySchema . forEach ( ( formField ) => {
83+ if ( formField . type !== 'file' ) {
84+ validatedBodySchema . properties [ formField . name ] = {
85+ type : formField . type
86+ } ;
87+ if ( formField . required ) {
88+ validatedBodySchema . required . push ( formField . name ) ;
89+ }
90+ }
91+ } ) ;
92+ }
93+ return validatedBodySchema ;
94+ }
95+
8996/**
9097 * The middleware - should be called for each express route
9198 * @param {any } req
0 commit comments