Skip to content

feat(validation): Add Middy.js middleware for JSON Schema validation #3694

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

Merged

Conversation

VatsalGoel3
Copy link
Contributor

@VatsalGoel3 VatsalGoel3 commented Mar 4, 2025

Summary

This PR introduces a validationMiddleware function for Middy.js, enabling JSON Schema validation for both inbound (event) and outbound (response) payloads in AWS Lambda functions. This middleware leverages the existing validate function to ensure input and output payloads adhere to defined schemas, improving type safety and error handling in serverless applications.


Changes

  • Implemented validationMiddleware.ts, which:
    • Supports inbound validation (before hook) to validate event payloads.
    • Supports outbound validation (after hook) to validate Lambda responses.
    • Allows optional envelope extraction, custom formats, external schema references, and custom AJV instances.
    • Returns an empty object if neither inboundSchema nor outboundSchema is provided.
  • Updated index.ts to export the validationMiddleware.
  • Added middleware.test.ts to ensure 100% test coverage, verifying:
    • ✅ Successful inbound & outbound validation.
    • ✅ Handling of validation failures (both input and output).
    • ✅ No-op behavior when no schemas are provided.
    • ✅ Compliance with repository best practices

Issue number closes #3609


Checklist

  • Follows contribution guidelines
  • Does not duplicate an existing PR
  • Passes all unit tests and meets 100% coverage threshold
  • Uses Prepare/Act/Assess format in tests
  • Ensures strict typing (no explicit any)
  • Follows repository coding and linting standards

By submitting this pull request, I confirm that I have read and followed the Powertools for AWS Lambda (TypeScript) contributing guidelines, and I confirm that you can use, modify, copy, and redistribute this contribution under the terms of your choice.

Disclaimer: We value your time and bandwidth. As such, any pull requests created on non-triaged issues might not be successful.

@pull-request-size pull-request-size bot added the size/L PRs between 100-499 LOC label Mar 4, 2025
@dreamorosi dreamorosi self-requested a review March 4, 2025 09:43
Copy link
Contributor

@dreamorosi dreamorosi left a comment

Choose a reason for hiding this comment

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

Hi @VatsalGoel3 could you please rebase the branch with the current main so it removes the conflicts?

Copy link
Contributor

@dreamorosi dreamorosi left a comment

Choose a reason for hiding this comment

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

Thank you for the PR, I left some minor comments

@github-actions github-actions bot added the feature PRs that introduce new features or minor changes label Mar 4, 2025
Copy link

sonarqubecloud bot commented Mar 4, 2025

@VatsalGoel3
Copy link
Contributor Author

@dreamorosi, I have made the requested changes and updated the test file as well, let me know if any more improvements are needed

@dreamorosi dreamorosi self-requested a review March 5, 2025 16:58
Copy link
Contributor

@dreamorosi dreamorosi left a comment

Choose a reason for hiding this comment

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

Thank you for this PR @VatsalGoel3!

We won't be releasing this module in the next release (this Friday) because we already have another feature coming up and we won't have time to review the documentation but I expect to include it in the next one (in ~2.5 weeks).

Over the next few days I'll be adding a few more issues to the backlog, in addition to the ones that are already marked as help-wanted (admittedly not many at the minute).

So keep an eye on the repo if you're interested in working on more!

@dreamorosi dreamorosi merged commit 443202b into aws-powertools:main Mar 5, 2025
41 checks passed
Copy link
Contributor

github-actions bot commented Mar 5, 2025

@aws-powertools/lambda-typescript No related issues found. Please ensure 'pending-release' label is applied before releasing.

@VatsalGoel3
Copy link
Contributor Author

@dreamorosi Thank you for the update, I would like to contribute more to the project or any relevant projects if possible, also is there any better way I can keep in contact with you, for better workflow. I'll keep a lookout for the coming issues.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature PRs that introduce new features or minor changes size/L PRs between 100-499 LOC
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature request: validator Middy.js middleware for JSON Schema validation
2 participants