Open catadch opened 5 years ago
My init is this, btw:
swaggerValidation.init('./api/openapi.yaml', {
ajvConfigParams: { useDefaults: true },
ajvConfigBody: { useDefaults: true }
}
);
It is supported, but you need custom ajv configuration for that. See https://github.com/Zooz/express-ajv-swagger-validation/blob/master/test/express/test-simple-server-with-coercion.js for an example.
Thanks kibertoad - looks like I'm doing the right thing by passing on useDefaults to ajv, but still no joy. An example of a not used default from my schema is:
cost:
type: number
format: int32
enum: [ 35 ]
default: 35
express-openapi-validator also uses ajv and inserts this and other defaults ok, but does not support discriminators.
Ok. Done some testing.
If I modify the schema so the Pet type is this:
Pet:
required:
- id
- name
- tag
properties:
id:
type: integer
format: int64
name:
type: string
tag:
type: string
default: Taggy McTagFace
And create an extra test in test/express/middleware-test.js
it('request with missing parameter should pass validation thanks to default', function (done) {
request(app)
.put('/pets')
.send([{
name: 1,
//tag: 'tag',
}])
.expect(200, function (err, res) {
if (err) {
throw err;
}
expect(res.body.tag).to.equal('Taggy McTagface');
done();
});
});
The "tag" value shoud be defaulted to 'Taggy McTagface', but it isn't, instead we get:
Uncaught AssertionError: expected undefined to equal 'Taggy McTagface'
As an aside, as 'tag' is now required, this test should fail with a 400.
@catadch thanks for reporting the issue.
First of all this is a bug, as we should pass useDefaults
by default to ajv.
We'll need to investigate this further.
Any news on this topic? 😄
I need a validator that inserts default values in the request body AND honors with discriminators, yours does the latter, but not the former. I have found others that are the other way around.
Am I doing something wrong, or is it a know issue that this package does not insert default values from the schema?