PayU / openapi-validator-middleware

Input validation using Swagger (Open API) and ajv
Apache License 2.0
144 stars 50 forks source link

Coercion doesn't work correctly due to Swagger 2.0 JSON Schema limitations #21

Closed kibertoad closed 6 years ago

kibertoad commented 6 years ago

Since Swagger 2.0 doesn't support specifying multiple types for a field (as JSON Schema does), it is not possible to correctly coerce numeric types as Ajv converts null integers into 0 if null is not one of possible types, and typically this is a very bad idea.

My suggestion is to provide slightly adjusted JSON schema to the Ajv for validation, using following rule: if property is not listed as required, it has one additional type - 'null'. Probably this behaviour should be regulated via a middleware option.

I can work on this functionality if approved :).

kibertoad commented 6 years ago

@idanto Any opinion on this?

igor-savin-ht commented 6 years ago

@idanto ping?..

idanto commented 6 years ago

Thanks @kibertoad. Sorry for the late response, busy weeks... You can add this functionality but please make it configurable and false by default for two reasons:

  1. backward compatibility
  2. personally I don't like when someone sends null in JSON I prefer it to not exist.

feel free to submit a pull request and I promise to review it as fast as possible.

igor-savin-ht commented 6 years ago

@idanto Thanks for the response! Will try to do it in the next few days.

kibertoad commented 6 years ago

@idanto Done! See https://github.com/idanto/express-ajv-swagger-validation/pull/22 BTW, what are the requirements for getting co-maintainer permissions on this project? Or you prefer to keep it a solo project?

kibertoad commented 6 years ago

@idanto Any chance you could publish 0.5.0 version at some point? Also see question above :)

idanto commented 6 years ago

already there :).

RE: co-maintainer permissions - I will add you soon.

igor-savin-ht commented 6 years ago

Thanks!