Type Coercion
See original GitHub issueWhat’s The Issue?
The validator is mutating request body payloads.
We think we’d like to be able to configure the validator to do validation only.
Where?
See here: specifically, coerceTypes: true
const aoav = new middlewares.RequestValidator(this.context.apiDoc, {
coerceTypes: true,
removeAdditional: true,
useDefaults: true,
});
With coerceTypes: true
— the hardcoded value — we see a property being sent in the request body being transformed from the boolean literal true
to the string literal "true"
.
"majorReferenceWork": true . // before validation
"majorReferenceWork": "true" // after validation
When I change the module so that coerceTypes: false
then the boolean property remains a boolean literal. This is our desired behaviour.
How To Reproduce?
I can, and will, add a reproducible test case… but I wanted to have a quick discussion about this behaviour before doing that because you might tell me I’m doing something dumb 😄
Fix?
I don’t think the current behaviour is wrong and coerceTypes
defaulting to true
is fine: but, it’d be nice if we could turn this off via a configuration parameter.
Totally open to other suggestions.
Issue Analytics
- State:
- Created 4 years ago
- Comments:6 (6 by maintainers)
@richdouglasevans i put together a test to try out your case. as far as i can tell it works correctly
Given the following spec for Pet (which is used in the POST request below)
I created the following test (note that
is_cat
is specified as a boolean in the spec above, however, it is passed in the request as astring
and is thus is a candidate for type coercion)The test case works as expected, the string
"is_cat": true
is properly coerced toboolean
which i echo back in the responseNext, I created a similar test but passed a boolean in the request. Here, I want to ensure that the boolean
true
remain a boolean after validation. ThThis test also looks good
All in all, it’s very possible that my tests don’t match your use case. If so, please let me know. An example is of course appreciated.
On another note. as a sanity check, it may be worth verifying that type for
majorReferenceWork
is defined with typeboolean
and notstring
Thanks @richdouglasevans. I’ll have a look at providing an option coerceTypes.