Skip to content

Feature request: standalone validate function for JSON Schema validation #3607

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

Closed
1 of 2 tasks
dreamorosi opened this issue Feb 16, 2025 · 9 comments · Fixed by #3662
Closed
1 of 2 tasks

Feature request: standalone validate function for JSON Schema validation #3607

dreamorosi opened this issue Feb 16, 2025 · 9 comments · Fixed by #3662
Assignees
Labels
completed This item is complete and has been merged/shipped feature-request This item refers to a feature request for an existing or new utility validation This item relates to the Validation Utility

Comments

@dreamorosi
Copy link
Contributor

dreamorosi commented Feb 16, 2025

Use case

To start implementing the new Validation utility, we should implement a validate standalone function, which is the core of the Validation utility and is a thin layer around ajv, the JSON Schema validation module we're taking a dependency on.

Note

This issue is open for contributions 🎉 If you're interested in contributing, please leave a comment below so that a maintainer can assign it to you. If you have questions or need clarifications, please don't hesitate to ask.

Solution/User Experience

A longer version of this can be found in the unpublished documentation page about the feature, that can be found at docs/utilities/validation.md.

The validate function should be a synchronous function that takes a single params object and acts as an assertion function which - as the name suggests - asserts that the event is of a certain type, or throws a SchemaValidationError error otherwise.

The params object should accept the following items:

  • a payload - this is the unknown object we want to validate
  • a schema - this is the JSON Schema we'll use to validate
  • an optional envelope parameter of type string (more on this later)
  • an optional formats optional parameter to pass custom formats
  • an optional externalRefs array of schemas that can be used to supply external references to the main schema
  • an optional ajv parameter that customers can use to provide their own instance - this is useful when wanting to use a different JSON Schema version/draft

The validate function should be generic and accept a type which is the type being asserted, if no type is supplied we'll default to unknown.

In terms of implementation, the validate function will rely on ajv and instantiate its own instance (new Ajv()) whenever one is not provided in the params object. When creating a new Ajv instance, we'll pass down any of the formats and externalRefs present in the params to the Ajv constructor.

If instead an ajv instance is already supplied, we'll add the formats and externalRefs using the methods addFormat() and addSchema().

Next, the validate function should call the compile() method on the ajv instance using the schema provided.

In case an envelope is passed, the function will use the search function from the @aws-lambda-powertools/jmespath module (already installed as dependency) to extract a subset of the payload using the envelope expression. The resulting object should be the one actually being validated.

Finally, it should validate the data and throw a SchemaValidationError error if the validation fails. This is a custom error that should be defined and exported for customers to use. If the validation is successful, the function should return the validated data.

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.

Alternative solutions

N/A

Acknowledgment

Future readers

Please react with 👍 and your use case to help us understand customer demand.

@dreamorosi dreamorosi added confirmed The scope is clear, ready for implementation feature-request This item refers to a feature request for an existing or new utility help-wanted We would really appreciate some support from community for this one validation This item relates to the Validation Utility labels Feb 16, 2025
@dreamorosi dreamorosi added this to the Validation utility milestone Feb 16, 2025
@dreamorosi dreamorosi moved this from Triage to Backlog in Powertools for AWS Lambda (TypeScript) Feb 16, 2025
@VatsalGoel3
Copy link
Contributor

@dreamorosi Would like to work on this Issue, please assign it to me

@dreamorosi
Copy link
Contributor Author

Hey @VatsalGoel3, great to hear thank you for picking this up!

@dreamorosi
Copy link
Contributor Author

Hi @VatsalGoel3, I wanted to check in and see if you were still working on this feature or you need any help with the PR.

@VatsalGoel3
Copy link
Contributor

Hey @dreamorosi, still working on the PR, getting some issue with a test failing, so could not push the commit

@dreamorosi
Copy link
Contributor Author

Thanks for the reply, no worries!

If it helps, feel free to push the PR and I can help fix the issues in the tests during the review.

@VatsalGoel3
Copy link
Contributor

VatsalGoel3 commented Feb 26, 2025

@dreamorosi Hey, so i was trying to publish the branch in my forked repo and I get

git push -u origin feature/validation-utils
�[33mThe CJS build of Vite's Node API is deprecated. See https://vite.dev/guide/troubleshooting.html#vite-cjs-node-api-deprecated for more details.�[39m
ERROR: Coverage for lines (99.97%) does not meet global threshold (100%)
ERROR: Coverage for statements (99.97%) does not meet global threshold (100%)
ERROR: Coverage for branches (99.95%) does not meet global threshold (100%)
error: failed to push some refs to 'https://github.com/VatsalGoel3/powertools-lambda-typescript.git'

these issues of which none of the tests I wrote for the PR are failing, getting some AWS account authentication and build errors.

@dreamorosi
Copy link
Contributor Author

You can publish with --no-verify for now, it's a bit hard to advise without seeing the new code. This way it skips the checks, and we can fix them in the PR.

Copy link
Contributor

⚠️ COMMENT VISIBILITY WARNING ⚠️

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.

@github-actions github-actions bot added pending-release This item has been merged and will be released soon and removed help-wanted We would really appreciate some support from community for this one confirmed The scope is clear, ready for implementation labels Feb 27, 2025
@github-actions github-actions bot added completed This item is complete and has been merged/shipped and removed pending-release This item has been merged and will be released soon labels Mar 7, 2025
@aws-powertools aws-powertools deleted a comment from github-actions bot Mar 7, 2025
@dreamorosi dreamorosi removed the completed This item is complete and has been merged/shipped label Mar 7, 2025
@dreamorosi dreamorosi added the pending-release This item has been merged and will be released soon label Mar 7, 2025
Copy link
Contributor

This is now released under v2.17.0 version!

@github-actions github-actions bot added completed This item is complete and has been merged/shipped and removed pending-release This item has been merged and will be released soon labels Mar 25, 2025
@dreamorosi dreamorosi moved this from Coming soon to Shipped in Powertools for AWS Lambda (TypeScript) Mar 25, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
completed This item is complete and has been merged/shipped feature-request This item refers to a feature request for an existing or new utility validation This item relates to the Validation Utility
Projects
2 participants