Skip to content

Conversation

srsudar
Copy link

@srsudar srsudar commented Jul 23, 2025

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 in ValidateRequestOptions, the same way it exists in ValidateResponseOptions.

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!

@srsudar
Copy link
Author

srsudar commented Aug 11, 2025

@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 express-openapi-validator into a prod app.

coerceTypes,
removeAdditional,
onError,
} = <ValidateRequestOpts>this.options.validateRequests;
Copy link
Owner

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

Copy link
Author

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;
Copy link
Owner

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?

Copy link
Author

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();
Copy link
Owner

@cdimascio cdimascio Aug 22, 2025

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?

Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants