Skip to content

feat(validation): support JSON Schema referencing in validation utils #4508

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

danieljnchen
Copy link

@danieljnchen danieljnchen commented Jun 13, 2024

Issue number: #4499

Summary

Changes

Please provide a summary of what's being changed

This change updates aws-lambda-powertools' validation functions to support passing the handlers param to fastjsonschema's validate call.

Customers now can use this new parameter

from aws_lambda_powertools.utilities.validation import validate, validator

PARENT_SCHEMA = {
  "$schema": "http://json-schema.org/draft-07/schema#",
  "$id": "https://example.com/schemas/parent.json",
  "type": "object",
  "properties": {
    "ParentSchema": {
      "$ref": "https://SCHEMA"
    }
  }
}

CHILD_SCHEMA = {
  "$schema": "http://json-schema.org/draft-07/schema#",
  "$id": "https://example.com/schemas/child.json",
  "type": "object",
  "properties": {
    "project": {
      "type": "string"
    }
  },
  "required": ["project"]
}

def get_child_schema(uri):
    return CHILD_SCHEMA

@validator(inbound_schema=PARENT_SCHEMA,inbound_handlers={"https": get_child_schema})
def lambda_handler(event, context):
    return "OK"

User experience

Please share what the user experience looks like before and after this change

Users previously were unable to use schemas with JSON Schema references

Checklist

If your change doesn't seem to apply, please leave them unchecked.

Is this a breaking change?

RFC issue number:

Checklist:

  • Migration process documented
  • Implement warnings (if it can live side by side)

Acknowledgment

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.

@boring-cyborg boring-cyborg bot added documentation Improvements or additions to documentation tests labels Jun 13, 2024
Copy link

boring-cyborg bot commented Jun 13, 2024

Thanks a lot for your first contribution! Please check out our contributing guidelines and don't hesitate to ask whatever you need.
In the meantime, check out the #python channel on our Powertools for AWS Lambda Discord: Invite link

@pull-request-size pull-request-size bot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Jun 13, 2024
@github-actions github-actions bot added feature New feature or functionality and removed documentation Improvements or additions to documentation labels Jun 14, 2024
@leandrodamascena leandrodamascena changed the title feat: support JSON Schema referencing in validation utils (by using fastjsonschema's handlers param) feat(validation): support JSON Schema referencing in validation utils Jun 14, 2024
@boring-cyborg boring-cyborg bot added the documentation Improvements or additions to documentation label Jun 14, 2024
@github-actions github-actions bot removed the documentation Improvements or additions to documentation label Jun 14, 2024
Copy link
Contributor

@leandrodamascena leandrodamascena left a comment

Choose a reason for hiding this comment

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

Hello @danieljnchen! Thans a lot for this super quick and very useful PR! We really need to offer this support to our customers.
I left a comment before I started doing a more detailed review.

Thank you and please ping me if you have any difficulties and I can help.

@boring-cyborg boring-cyborg bot added the documentation Improvements or additions to documentation label Jun 15, 2024
Copy link

Quality Gate Failed Quality Gate failed

Failed conditions
2 Security Hotspots

See analysis details on SonarCloud

@danieljnchen danieljnchen marked this pull request as ready for review June 17, 2024 20:32
@danieljnchen danieljnchen requested a review from a team June 17, 2024 20:32
@danieljnchen
Copy link
Author

Alright, my development environment is all set up, and I've run the make pr step. The only check that's failing is the complexity check that I left a note about. Let me know if I need to make any more changes to get this ready to go in!

@leandrodamascena
Copy link
Contributor

Alright, my development environment is all set up, and I've run the make pr step. The only check that's failing is the complexity check that I left a note about. Let me know if I need to make any more changes to get this ready to go in!

Hi @danieljnchen! This is amazing! I'm reviewing this PR and if I find something small and easy to fix, I'll push some commits to get it merged quickly!

Thank you very much for taking the time to make this improvement!

@github-actions github-actions bot removed the documentation Improvements or additions to documentation label Jun 18, 2024
@github-actions github-actions bot removed the documentation Improvements or additions to documentation label Jun 18, 2024
@boring-cyborg boring-cyborg bot added the documentation Improvements or additions to documentation label Jun 18, 2024
@github-actions github-actions bot removed the documentation Improvements or additions to documentation label Jun 18, 2024
@danieljnchen
Copy link
Author

Hey @danieljnchen! I've made some minor changes to this PR, let me explain them:

1 - To reduce the cyclomatic complexity, I created a new function, and now we are testing if variables are None or not through this function. This approach allows us to avoid direct if statements in the specific code, thereby reducing complexity and making Xenon happy.

2 - I refactored the example to make it more clear for customers that want just to copy and past and test this in their Lambda functions.

This PR is happily approved ❤️ .

@leandrodamascena (and @heitorlessa) thanks for your help getting this PR all cleaned up! I'm really excited to see this go in -- this is my first time contributing to an open-source project (I tried with python-dateutil a while back, but my MR never got reviewed). I appreciate the responsiveness and encouragement!

Copy link
Contributor

@heitorlessa heitorlessa left a comment

Choose a reason for hiding this comment

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

This is looking GREAT!!!

Made final comments, one potential error on exceptions, a small refactoring, line editing for docs along with explanation as to why each of them.

Looking forward to this - thank you both!!

@boring-cyborg boring-cyborg bot added documentation Improvements or additions to documentation internal Maintenance changes labels Jun 19, 2024
heitorlessa
heitorlessa previously approved these changes Jun 19, 2024
@github-actions github-actions bot removed documentation Improvements or additions to documentation internal Maintenance changes labels Jun 19, 2024
@boring-cyborg boring-cyborg bot added documentation Improvements or additions to documentation internal Maintenance changes labels Jun 19, 2024
@github-actions github-actions bot removed documentation Improvements or additions to documentation internal Maintenance changes labels Jun 19, 2024
@leandrodamascena leandrodamascena self-requested a review June 19, 2024 10:14
@leandrodamascena leandrodamascena dismissed stale reviews from heitorlessa and themself via 2191346 June 19, 2024 10:17
@boring-cyborg boring-cyborg bot added documentation Improvements or additions to documentation internal Maintenance changes labels Jun 19, 2024
@github-actions github-actions bot removed documentation Improvements or additions to documentation internal Maintenance changes labels Jun 19, 2024
@sthulb sthulb merged commit c509412 into aws-powertools:develop Jun 19, 2024
17 checks passed
Copy link

boring-cyborg bot commented Jun 19, 2024

Awesome work, congrats on your first merged pull request and thank you for helping improve everyone's experience!

@danieljnchen danieljnchen deleted the feat/validation-json-schema-refs branch June 20, 2024 15:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
commons feature New feature or functionality size/L Denotes a PR that changes 100-499 lines, ignoring generated files. tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support JSON Schema referencing in validation utils (by using fastjsonschema's handlers param)
4 participants