Skip to content

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

Merged

Conversation

VatsalGoel3
Copy link
Contributor

@VatsalGoel3 VatsalGoel3 commented Mar 1, 2025

Summary

Changes

Please provide a summary of what's being changed

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.

Please add the issue number below, if no issue is present the PR might get blocked and not be reviewed

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.

@VatsalGoel3 VatsalGoel3 requested review from a team as code owners March 1, 2025 07:41
@VatsalGoel3 VatsalGoel3 requested a review from dreamorosi March 1, 2025 07:41
@pull-request-size pull-request-size bot added the size/L PRs between 100-499 LOC label Mar 1, 2025
@github-actions github-actions bot added the feature PRs that introduce new features or minor changes label Mar 3, 2025
Copy link
Contributor

github-actions bot commented Mar 3, 2025

No related issues found. Please ensure there is an open issue related to this change to avoid significant delays or closure.

Copy link
Contributor

github-actions bot commented Mar 3, 2025

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

@github-actions github-actions bot added do-not-merge This item should not be merged need-issue This PR needs an issue before it can be reviewed/worked on further labels Mar 3, 2025
@dreamorosi
Copy link
Contributor

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.

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.

Left a couple comments on the implementation - mainly refactoring for types, the flow is good.

@dreamorosi dreamorosi self-requested a review March 4, 2025 08:39
@dreamorosi dreamorosi added validation This item relates to the Validation Utility and removed do-not-merge This item should not be merged need-issue This PR needs an issue before it can be reviewed/worked on further labels Mar 4, 2025
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.

Thanks for the review, before merging we need to make a few more changes:

  • In packages/validation/src/validate.ts change the import from import Ajv, { type ValidateFunction } from 'ajv'; to import { Ajv, type ValidateFunction } from 'ajv';
  • In packages/validation/src/validate.ts change the function signature from export function validate<T = unknown>(params: ValidateParams<T>): T { to export function validate<T = unknown>(params: ValidateParams): T {
  • In packages/validation/src/index.ts change the first import from export { validate } from './validate'; to export { validate } from './validate.js';
  • In packages/validation/src/index.ts add a new import export { validator } from './decorator.js';

I checked out the branch and tried to build it, and unless we make these changes it won't work.

@VatsalGoel3
Copy link
Contributor Author

@dreamorosi Just updated with the suggested changes.

Copy link

sonarqubecloud bot commented Mar 4, 2025

@dreamorosi dreamorosi self-requested a review March 4, 2025 09:02
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 addressing all the review comments!

Let's ship it 🚢 !

@dreamorosi dreamorosi merged commit ae6b7cf into aws-powertools:main Mar 4, 2025
41 checks passed
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 validation This item relates to the Validation Utility
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature request: validator class method decorator for JSON Schema validation
2 participants