-
Notifications
You must be signed in to change notification settings - Fork 154
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
Comments
@dreamorosi Would like to work on this Issue, please assign it to me |
Hey @VatsalGoel3, great to hear thank you for picking this up! |
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. |
Hey @dreamorosi, still working on the PR, getting some issue with a test failing, so could not push the commit |
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. |
@dreamorosi Hey, so i was trying to publish the branch in my forked repo and I get
these issues of which none of the tests I wrote for the PR are failing, getting some AWS account authentication and build errors. |
You can publish with |
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 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 aroundajv
, 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 singleparams
object and acts as an assertion function which - as the name suggests - asserts that the event is of a certain type, or throws aSchemaValidationError
error otherwise.The
params
object should accept the following items:payload
- this is the unknown object we want to validateschema
- this is the JSON Schema we'll use to validateenvelope
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/draftThe
validate
function should be generic and accept a type which is the type being asserted, if no type is supplied we'll default tounknown
.In terms of implementation, the
validate
function will rely onajv
and instantiate its own instance (new Ajv()
) whenever one is not provided in theparams
object. When creating a newAjv
instance, we'll pass down any of theformats
andexternalRefs
present in theparams
to theAjv
constructor.If instead an
ajv
instance is already supplied, we'll add theformats
andexternalRefs
using the methodsaddFormat()
andaddSchema()
.Next, the
validate
function should call thecompile()
method on theajv
instance using theschema
provided.In case an
envelope
is passed, the function will use thesearch
function from the@aws-lambda-powertools/jmespath
module (already installed as dependency) to extract a subset of thepayload
using theenvelope
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
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: