aws-powertools / powertools-lambda-python

A developer toolkit to implement Serverless best practices and increase developer velocity.
https://docs.powertools.aws.dev/lambda/python/latest/
MIT No Attribution
2.72k stars 378 forks source link

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

Closed danieljnchen closed 2 weeks ago

danieljnchen commented 3 weeks ago

Issue number: https://github.com/aws-powertools/powertools-lambda-python/issues/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[bot] commented 3 weeks ago

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

sonarcloud[bot] commented 3 weeks ago

Quality Gate Failed Quality Gate failed

Failed conditions
2 Security Hotspots

See analysis details on SonarCloud

danieljnchen commented 3 weeks ago

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 commented 3 weeks ago

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!

codecov[bot] commented 2 weeks ago

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

Project coverage is 96.43%. Comparing base (e14e768) to head (caa806e). Report is 603 commits behind head on develop.

:exclamation: Current head caa806e differs from pull request most recent head 2191346

Please upload reports for the commit 2191346 to get more accurate results.

Additional details and impacted files ```diff @@ Coverage Diff @@ ## develop #4508 +/- ## =========================================== + Coverage 96.38% 96.43% +0.05% =========================================== Files 214 219 +5 Lines 10030 10628 +598 Branches 1846 1976 +130 =========================================== + Hits 9667 10249 +582 - Misses 259 267 +8 - Partials 104 112 +8 ```

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

danieljnchen commented 2 weeks ago

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!

boring-cyborg[bot] commented 2 weeks ago

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