Skip to content
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

feat(openapi): throw OpenAPIValidatorMiddlewareError #446

Merged
merged 10 commits into from
Mar 26, 2024

Conversation

mkurapov
Copy link
Contributor

@mkurapov mkurapov commented Mar 25, 2024

Changes proposed in this pull request

Changes to the createValidatorMiddleware behaviour in packages/openapi:

  • The middleware now throws a OpenAPIValidatorMiddlewareError instead of directly using Koa's ctx.throw function. This error class has message and status properties that describe the exact validation error as well as the status code that should be thrown.
  • The middleware now takes in an optional validationOptions argument that determines whether to validate just the request, the response or both. By default, both are validated, and the middleware no longer uses process.env.NODE_ENV to determine whether to validate the response or not.

Context

Fixes #445

Copy link

changeset-bot bot commented Mar 25, 2024

🦋 Changeset detected

Latest commit: a6151c5

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 2 packages
Name Type
@interledger/openapi Major
@interledger/open-payments Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link

netlify bot commented Mar 25, 2024

Deploy Preview for openpayments-preview canceled.

Name Link
🔨 Latest commit a6151c5
🔍 Latest deploy log https://app.netlify.com/sites/openpayments-preview/deploys/6601f043dc0d39000891f5a9

@mkurapov mkurapov marked this pull request as ready for review March 25, 2024 17:24
@mkurapov mkurapov changed the title 445/mk/openapi validator error feat(openapi): throw OpenAPIValidatorMiddlewareError Mar 25, 2024
@@ -0,0 +1,8 @@
---
'@interledger/openapi': major
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it should be major since it changes how createValidatorMiddleware is used.
But don't mind using minor since its just Rafiki using it

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Major sounds right, if I'm understanding correctly. This will break consumers where NODE_ENV is production and a response will fail validation.

Comment on lines -10 to -12
const validateResponse =
process.env.NODE_ENV !== 'production' &&
spec.createResponseValidator(options)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like directly using process.env.NODE_ENV is not a fun time.
Will update Rafiki to have validationOptions.validateReponse: process.env.NODE_ENV !== 'production' instead

} else if (isValidationError(err)) {
ctx.throw(err.status ?? 500, err.errors[0])
} else {
console.log('err=', err)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removing pesky console.log here

status?: number
errors: string[]
errors: ReturnType<typeof errorTransformer>[]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The return type of errorTransformer is

{
  message: string
}

Might it make sense to define that and use it here and the errorTransformer signature?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, I think I went with this since I didn't have a good name for the type 😆

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Notice that I didn't offer a name myself lol

@mkurapov mkurapov merged commit 6dde8b2 into main Mar 26, 2024
10 checks passed
@mkurapov mkurapov deleted the 445/mk/openapi-validator-error branch March 26, 2024 09:24
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.

OpenAPI validator middleware should throw a custom error
2 participants