-
Notifications
You must be signed in to change notification settings - Fork 154
Feature request: validator
Middy.js middleware for JSON Schema validation
#3609
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
Comments
Would take up this issue in continuation of #3608 |
This issue is now closed. Please be mindful that future comments are hard for our team to see. If you need more assistance, please either tag a team member or open a new issue that references this one. If you wish to keep having a conversation with other community members under this issue feel free to do so. |
This is now released under v2.17.0 version! |
Use case
To continue the implementation of the Validation utility started in #3607, we should implement a
validator
Middy.js middleware that can be used by customers who are already invested in Middy.js to wrap a function handler and validate both inputs and outputs against a set of JSON Schemas.Solution/User Experience
The middleware will use the
validate
function implemented in #3607 under the hood, and as such it should accept similar parameters to that function, albeit with some changes to accommodate the fact that 1/ it's a middleware, and 2/ it can validate both input and outputs of the decorated method.The middleware will take a single
params
object that has the following items:inboundSchema
- this is the JSON Schema used to validate the incoming payloadoutboundSchema
- this is the JSON Schema used to validate the response payloadenvelope
parameter of typestring
(more on this later)formats
optional parameter to pass custom formatsexternalRefs
array of schemas that can be used to supply external references to the main schemaajv
parameter that customers can use to provide their own instance - this is useful when wanting to use a different JSON Schema version/draftBoth
inboundSchema
andoutboundSchema
are optional, this is intentional because we want to allow customers to validate one, the other, or both. When one of them is provided, the correspondingonBefore
andonAfter
hooks in the middleware will be no-ops.In terms of implementation, the
validator
decorator will set two Middy.js hooks:onBefore
: ifinboundSchema
is present, call thevalidate
function using the schema asschema
and the first argument of the decorated method aspayload
, as well as all the other optional params to validate the incoming payload. Replace theevent
with the result of thevalidate
function callonAfter
: ifoutboundSchema
is present, call thevalidate
function using the schema asschema
and theresponse
of the handler aspayload
, as well as all the other optional params except forenvelope
to validate itI'm unsure if it's even possible, but if it is, the middleware should be a generic function and accept types for both the
event
and response, and set these types on the handler - as I mentioned, I am unsure if this is doable. If not, or too time consuming, feel free to skip it for now and prioritize the overall implementation.As part of the implementation the implementer should also write unit tests to cover 100% of the files, lines, branches of the code added as part of this issue. All the dependencies needed should already be present in the workspace, ideally the implementation should follow existing project conventions as much as possible. The middleware should be in its own file, i.e.
packages/validation/src/middleware.ts
.When it comes to references, you can find an example of how this middleware would be used in the
docs/utilities/validation.md
file.If instead you're unfamiliar with how to implement Middy.js middlewares, I've added an example below
Additionally, you can also refer to the Middy.js guide on how to write middlewares.
Alternative solutions
Acknowledgment
Future readers
Please react with 👍 and your use case to help us understand customer demand.
The text was updated successfully, but these errors were encountered: