-
Notifications
You must be signed in to change notification settings - Fork 11
Open
Description
I'm getting the following error for request validation:
error while validating request: request should have required property '.headers'
The stack I am using is:
- express 4.17.1
- express-openapi-validate 0.5.1
- OpenAPI spec 3.0.1
- NodeJS 15.12.0
- ES6 implementation
Doing some research I narrowed it down to the following code part of the validate function:
const reqToValidate = {
...req,
cookies: req.cookies
? { ...req.cookies, ...req.signedCookies }
: undefined,
};Which compiles to:
const reqToValidate = Object.assign({}, req, { cookies: req.cookies
? Object.assign({}, req.cookies, req.signedCookies) : undefined });The problem here is express uses accessors get/set for the headers, as a result the property headers is not present as part of reqToValidate.
As a quick fix the issue I implemented the following:
const validator = this.openApiValidator.validate(method, path);
return (req, res, next) => {
const reqToValidate = Object.assign({}, req, {
cookies: req.cookies
? Object.assign({}, req.cookies, req.signedCookies) : undefined
});
if (!reqToValidate.hasOwnProperty('headers')) {
reqToValidate.headers = req.headers;
}
return validator(reqToValidate, res, next);
};which works pretty well.
Thanks,
~Q
Metadata
Metadata
Assignees
Labels
No labels