Hilzu / express-openapi-validate

Express middleware to validate requests based on an OpenAPI 3 document
Apache License 2.0
75 stars 12 forks source link

Add documentation for match() #63

Closed codeclown closed 4 years ago

codeclown commented 4 years ago

Documentation proposal for feature from https://github.com/Hilzu/express-openapi-validate/pull/37


Maintainers, feel free to edit any formatting or words as you wish.

coveralls commented 4 years ago

Coverage Status

Coverage remained the same at 100.0% when pulling 45a7b0d81712bbdf95b5c8a0df99716be9d16be2 on codeclown:match-docs into e0d7377f63da1a30fac9bfa78a715803bd165266 on Hilzu:master.

Hilzu commented 4 years ago

You should document what the pitfall of using match compared to validate is. With validate you get an error if you try to add validation for an operation that doesn't exists. Using match you might not have a validator even if you expect there to be one due to a typo for example.

codeclown commented 4 years ago

Very good point and one that I did not properly consider before. Now I would actually prefer to change the default behaviour so that an exception is thrown by match() if no validator was found for the request (could be disabled via option i.e. validator.match({ allowNoMatch: true })).

What do you think of this @Hilzu?

In regards to this PR... I'll update the docs with current state.

Hilzu commented 4 years ago

That might be one way of doing it.

I'm also thinking about adding a validate-like method, that needs to be explicitly added to routes but wouldn't need the operation specified. Instead it would find the validator using the same machinery as match but could error/warn if one isn't found since you explicitly asked for a validation for a route.

codeclown commented 4 years ago

Using match(), if it was changed to throw on unmatched route, that would be the behaviour if you used it on individual routes as well, like this:

app.get('/foo', validator.match(), (req, res, next) => ...);

But I think you're not talking about middleware? Could you illustrate with an example what you mean exactly?

Hilzu commented 4 years ago

No middleware is exactly what I'm talking about. Somehow I didn't realize that match could also be used like that.

I think making match throw by default and also documenting the usage with a single route is a good way forward.