-
Notifications
You must be signed in to change notification settings - Fork 153
feat(validation): Add @validator decorator for JSON Schema validation #3679
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(validation): Add @validator decorator for JSON Schema validation #3679
Conversation
No related issues found. Please ensure there is an open issue related to this change to avoid significant delays or closure. |
No acknowledgement section found. Please make sure you used the template to open a PR and didn't remove the acknowledgment section. Check the template here: https://github.com/aws-powertools/powertools-lambda-typescript/blob/develop/.github/PULL_REQUEST_TEMPLATE.md#acknowledgment |
Hi @VatsalGoel3 - thank you for opening the PR. You have modified the PR body and our automation fails because of it. Not sure why this keeps happening, but if you're using some kind of git client, please set it up so it keeps the PR body intact and just fills in the required fields. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Left a couple comments on the implementation - mainly refactoring for types, the flow is good.
…alGoel3/powertools-lambda-typescript into feature/validator-decorator
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the review, before merging we need to make a few more changes:
- In
packages/validation/src/validate.ts
change the import fromimport Ajv, { type ValidateFunction } from 'ajv';
toimport { Ajv, type ValidateFunction } from 'ajv';
- In
packages/validation/src/validate.ts
change the function signature fromexport function validate<T = unknown>(params: ValidateParams<T>): T {
toexport function validate<T = unknown>(params: ValidateParams): T {
- In
packages/validation/src/index.ts
change the first import fromexport { validate } from './validate';
toexport { validate } from './validate.js';
- In
packages/validation/src/index.ts
add a new importexport { validator } from './decorator.js';
I checked out the branch and tried to build it, and unless we make these changes it won't work.
@dreamorosi Just updated with the suggested changes. |
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for addressing all the review comments!
Let's ship it 🚢 !
Summary
Changes
This PR introduces a @validator method decorator that enables JSON Schema validation for both input (inbound) and output (outbound) parameters of class methods. It is built using the existing validate function and follows the Powertools for AWS Lambda (TypeScript) validation utility standards.
Key Features
✅ Validates method arguments using inbound schemas
✅ Validates method return values using outbound schemas
✅ Supports optional parameters like envelope, formats, and external references
✅ Allows custom AJV instances for extended schema validation
✅ Includes unit tests with 100% coverage
Implementation
Created validator.ts, a TypeScript decorator that validates the input and output of methods based on JSON Schema definitions.
Integrated the decorator with the existing validate function to ensure consistency.
Added unit tests to verify:
Successful validation for both inbound and outbound payloads.
Errors for invalid inbound and outbound payloads.
Proper handling when no schema is provided (no-op behavior).
Coverage for edge cases such as undefined method descriptors.
Issue number: closes #3608
By submitting this pull request, 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.