3
3
4
4
import * as _ from 'lodash' ;
5
5
import Ajv , { Options as AjvOpts , ErrorObject , FormatDefinition , ValidateFunction } from 'ajv' ;
6
- import type { OpenAPIV3 , OpenAPIV3_1 } from 'openapi-types' ;
6
+ import { OpenAPIV3 , OpenAPIV3_1 } from 'openapi-types' ;
7
7
import { OpenAPIRouter , Request , Operation } from './router' ;
8
8
import OpenAPIUtils from './utils' ;
9
9
import { PickVersionElement , SetMatchType } from './backend' ;
@@ -564,7 +564,10 @@ export class OpenAPIValidator<D extends Document = Document> {
564
564
OpenAPIV3 . RequestBodyObject ,
565
565
OpenAPIV3_1 . RequestBodyObject
566
566
> ;
567
- const jsonbody = requestBody . content [ 'application/json' ] ;
567
+ const jsonbody =
568
+ requestBody . content [ 'application/json' ] ||
569
+ requestBody . content [ 'multipart/form-data' ] ||
570
+ requestBody . content [ 'application/x-www-form-urlencoded' ] ;
568
571
if ( jsonbody && jsonbody . schema ) {
569
572
const requestBodySchema : InputValidationSchema = {
570
573
title : 'Request' ,
@@ -582,6 +585,7 @@ export class OpenAPIValidator<D extends Document = Document> {
582
585
583
586
// add compiled params schema to schemas for this operation id
584
587
const requestBodyValidator = this . getAjv ( ValidationContext . RequestBody ) ;
588
+ this . removeBinaryPropertiesFromRequired ( requestBodySchema ) ;
585
589
validators . push ( OpenAPIValidator . compileSchema ( requestBodyValidator , requestBodySchema ) ) ;
586
590
}
587
591
}
@@ -669,6 +673,40 @@ export class OpenAPIValidator<D extends Document = Document> {
669
673
return validators ;
670
674
}
671
675
676
+ /**
677
+ * Removes binary properties from the required array in JSON schema, since they cannot be validated.
678
+ *
679
+ * @param {OpenAPIV3_1.SchemaObject } schema
680
+ * @memberof OpenAPIValidator
681
+ */
682
+ private removeBinaryPropertiesFromRequired ( schema : OpenAPIV3_1 . SchemaObject ) : void {
683
+ if ( typeof schema !== 'object' || ! schema ?. required ) {
684
+ return ;
685
+ }
686
+
687
+ if ( schema . properties && schema . required && Array . isArray ( schema . required ) ) {
688
+ const binaryProperties = Object . keys ( schema . properties ) . filter ( ( propName ) => {
689
+ const prop : OpenAPIV3_1 . SchemaObject = schema . properties [ propName ] ;
690
+ return prop && prop . type === 'string' && prop . format === 'binary' ;
691
+ } ) ;
692
+
693
+ if ( binaryProperties . length > 0 ) {
694
+ schema . required = schema . required . filter ( ( prop : string ) => ! binaryProperties . includes ( prop ) ) ;
695
+ }
696
+ }
697
+
698
+ // Recursively process nested objects and arrays
699
+ if ( schema . properties ) {
700
+ Object . values ( schema . properties ) . forEach ( ( prop ) => this . removeBinaryPropertiesFromRequired ( prop ) ) ;
701
+ }
702
+
703
+ [ 'allOf' , 'anyOf' , 'oneOf' ] . forEach ( ( key ) => {
704
+ if ( Array . isArray ( schema [ key ] ) ) {
705
+ schema [ key ] . forEach ( ( subSchema : any ) => this . removeBinaryPropertiesFromRequired ( subSchema ) ) ;
706
+ }
707
+ } ) ;
708
+ }
709
+
672
710
/**
673
711
* Get response validator function for an operation by operationId
674
712
*
0 commit comments