-
-
Notifications
You must be signed in to change notification settings - Fork 229
add onError to validateRequestOptions #1101
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
@cdimascio -- any thoughts on this? I'd be happy to try some cleanup if it seems like something you're interested in merging. If not, I might maintain a fork. I think I'll need this to be able to safely begin integrating |
coerceTypes, | ||
removeAdditional, | ||
onError, | ||
} = <ValidateRequestOpts>this.options.validateRequests; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this change necessary? Does the list include the complete set of user specified options? If not, it will change/ break existing behavior
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought that I did need it, yes, b/c otherwise the onError
property isn't actually passed from the constructor.
Confirmed that removing it breaks the tests I added.
I thought this would be a no-op if you weren't defining this new property. I didn't break any existing tests, so I thought this was safe.
What am I missing?
allowUnknownQueryParameters?: boolean; | ||
coerceTypes?: boolean | 'array'; | ||
removeAdditional?: boolean | 'all' | 'failing'; | ||
onError?: (err: InternalServerError, req: Request) => void; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is the intend of this change to introduce onError to requests, mirroring what exists currently (but only) for responses?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes that's right. My thinking is that we currently have three platforms hitting our APIs. I believe I've captured the the real-world usage of our APIs, but there are enough edge cases that I'd believe I've missed something. I'd like to be able to introduce validation slowly, basically making sure that we are correctly extracting the openapi interface before I begin actually serving 400s using the validator.
throw error; | ||
if (this.requestOpts.onError) { | ||
this.requestOpts.onError(error, req); | ||
next(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do we need to call next() here? we should expect onError handlers to always throw, correct? or is the intent to also allow clients to eat errors?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My thinking was that we should let clients eat the error, yeah.
In the case I'm implementing it for, eg, I want to basically know when express-openapi-validator
says there's an error, log it, but not actually cause the client request to fail.
If the client wants an error, they can re-throw. This is how I've been using the response validator, and as far as I can tell it is working.
I would like to be able to slowly and safely introduce this to an existing project. It's possible that we have some misbehaving clients, an I'd like to be able to identify cases where we aren't correctly describing types, without necessarily breaking the clients.
It seems like to do this, and be aware of bad requests, I want an
onError
option inValidateRequestOptions
, the same way it exists inValidateResponseOptions
.This PR is an attempt to accomplish that. I cribbed pretty liberally from the same tests for response validation, and I'd believe of course that I'm missing something. I welcome comments!