express-openapi-validator icon indicating copy to clipboard operation
express-openapi-validator copied to clipboard

Coerce types by default for multipart

Open aviskase opened this issue 4 years ago • 1 comments

Is your feature request related to a problem? Please describe. Per validateRequests.coerceTypes we automatically always coerce request query, path params, headers, and cookies.

When we use multipart request body, properties are always sent in string format, thus, without coerceTypes=true, values defines in schema as integers or booleans fail validation.

Describe the solution you'd like Two possible solutions:

  • always coerce for multipart request bodies (same as query params and others), because they are always strings
  • split coerceTypes into coerceTypes and coerceTypesForMultipart

Describe alternatives you've considered Setting coerceTypes=true solves the problem, but it affects other request bodies, which I'd want to avoid.


I'd be happy to prepare PR depending on which solution you think is better (and where is the best place to start). Also, is there a workaround to keep coerceTypes only for a particular route?

aviskase avatar Sep 22 '21 17:09 aviskase

@aviskase thanks for this issue.

please submit a PR. that will be super helpful. Let's take a variation of approach 2. modify coerceTypes to take an array as an alternative to the boolean. e.g. coerceTypes: [ 'multipart' ]

coerceTypes: true will continue to have current behavior, that is coerce all request bodies.

similarly, coerceTypes: false will disable this behavior

finally, coerceTypes: [ 'multipart' ] will enable coercion for multiple bodies only

cdimascio avatar Nov 27 '21 15:11 cdimascio