Closed anthony-o closed 6 years ago
The validator throws the error: request.query.page should be integer
. With the default express query string parser the properties are either strings, objects or arrays. It does no magic to turn strings into numbers. That means that with query parameters you can't specify that they are numbers in your openapi document. A workaround is to define your numeric parameters like this:
page:
name: page
in: query
description: Page number.
required: false
schema:
type: string
pattern: "^[0-9]*$"
and convert the parameters to numbers in your route code with Number(req.query.page)
.
OK... It is just that with other Swagger (v2) validators such as https://github.com/apigee-127/swagger-tools , when you define an Integer, it is checked and validated as an Integer, so I was expecting other validators to behave the same way.
That could be done but I'm hesitant in having a validator library modifying the request. If I add a feature like that it would be opt-in.
Is it mandatory to modify the request? Couldn't you "just" add specific RegExp validator for Integers, that assumes that req.query will always have strings and check those string to match an Integer, before it goes forward to the controller function?
I would find it confusing that the properties would be validated as numbers but the route code gets strings. For consistency the validator should work on the same values that are provided to the route.
For people still seeing this (like I just did):
You can use new OpenApiValidator(openApiDocument, { ajvOptions: { coerceTypes: true }})
, which will validate numbers, booleans, etc. correctly.
(taken from #42 in this comment).
Here is my schema:
Here is my router code:
and my
validator.js
:When I try to access my API at http://localhost:3000/api/v1/cities?page=1&perPage=2&postalCode=32165 I have the following error: