Skip to content

refactor(validation): add from __future__ import annotations #4984

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
merged 2 commits into from
Aug 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions aws_lambda_powertools/utilities/validation/base.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
from __future__ import annotations

import logging
from typing import Dict, Optional, Union

import fastjsonschema # type: ignore

from .exceptions import InvalidSchemaFormatError, SchemaValidationError
from aws_lambda_powertools.utilities.validation.exceptions import InvalidSchemaFormatError, SchemaValidationError

logger = logging.getLogger(__name__)


def validate_data_against_schema(data: Union[Dict, str], schema: Dict, formats: Optional[Dict] = None):
def validate_data_against_schema(data: dict | str, schema: dict, formats: dict | None = None):
"""Validate dict data against given JSON Schema

Parameters
----------
data : Dict
data : dict
Data set to be validated
schema : Dict
schema : dict
JSON Schema to validate against
formats: Dict
formats: dict
Custom formats containing a key (e.g. int64) and a value expressed as regex or callback returning bool

Raises
Expand Down
24 changes: 13 additions & 11 deletions aws_lambda_powertools/utilities/validation/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
from typing import Any, List, Optional
from __future__ import annotations

from ...exceptions import InvalidEnvelopeExpressionError
from typing import Any

from aws_lambda_powertools.exceptions import InvalidEnvelopeExpressionError


class SchemaValidationError(Exception):
"""When serialization fail schema validation"""

def __init__(
self,
message: Optional[str] = None,
validation_message: Optional[str] = None,
name: Optional[str] = None,
path: Optional[List] = None,
value: Optional[Any] = None,
definition: Optional[Any] = None,
rule: Optional[str] = None,
rule_definition: Optional[Any] = None,
message: str | None = None,
validation_message: str | None = None,
name: str | None = None,
path: list | None = None,
value: Any | None = None,
definition: Any | None = None,
rule: str | None = None,
rule_definition: Any | None = None,
):
"""

Expand All @@ -29,7 +31,7 @@ def __init__(
name : str, optional
name of a path in the data structure
(e.g. `data.property[index]`)
path: List, optional
path: list, optional
`path` as an array in the data structure
(e.g. `['data', 'property', 'index']`),
value : Any, optional
Expand Down
53 changes: 27 additions & 26 deletions aws_lambda_powertools/utilities/validation/validator.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
from __future__ import annotations

import logging
from typing import Any, Callable, Dict, Optional, Union
from typing import Any, Callable

from aws_lambda_powertools.middleware_factory import lambda_handler_decorator
from aws_lambda_powertools.utilities import jmespath_utils

from ...middleware_factory import lambda_handler_decorator
from .base import validate_data_against_schema
from aws_lambda_powertools.utilities.validation.base import validate_data_against_schema

logger = logging.getLogger(__name__)


@lambda_handler_decorator
def validator(
handler: Callable,
event: Union[Dict, str],
event: dict | str,
context: Any,
inbound_schema: Optional[Dict] = None,
inbound_formats: Optional[Dict] = None,
outbound_schema: Optional[Dict] = None,
outbound_formats: Optional[Dict] = None,
inbound_schema: dict | None = None,
inbound_formats: dict | None = None,
outbound_schema: dict | None = None,
outbound_formats: dict | None = None,
envelope: str = "",
jmespath_options: Optional[Dict] = None,
jmespath_options: dict | None = None,
**kwargs: Any,
) -> Any:
"""Lambda handler decorator to validate incoming/outbound data using a JSON Schema
Expand All @@ -28,21 +29,21 @@ def validator(
----------
handler : Callable
Method to annotate on
event : Dict
event : dict
Lambda event to be validated
context : Any
Lambda context object
inbound_schema : Dict
inbound_schema : dict
JSON Schema to validate incoming event
outbound_schema : Dict
outbound_schema : dict
JSON Schema to validate outbound event
envelope : Dict
envelope : dict
JMESPath expression to filter data against
jmespath_options : Dict
jmespath_options : dict
Alternative JMESPath options to be included when filtering expr
inbound_formats: Dict
inbound_formats: dict
Custom formats containing a key (e.g. int64) and a value expressed as regex or callback returning bool
outbound_formats: Dict
outbound_formats: dict
Custom formats containing a key (e.g. int64) and a value expressed as regex or callback returning bool

Example
Expand Down Expand Up @@ -140,26 +141,26 @@ def handler(event, context):

def validate(
event: Any,
schema: Dict,
formats: Optional[Dict] = None,
envelope: Optional[str] = None,
jmespath_options: Optional[Dict] = None,
schema: dict,
formats: dict | None = None,
envelope: str | None = None,
jmespath_options: dict | None = None,
):
"""Standalone function to validate event data using a JSON Schema

Typically used when you need more control over the validation process.

Parameters
----------
event : Dict
event : dict
Lambda event to be validated
schema : Dict
schema : dict
JSON Schema to validate incoming event
envelope : Dict
envelope : dict
JMESPath expression to filter data against
jmespath_options : Dict
jmespath_options : dict
Alternative JMESPath options to be included when filtering expr
formats: Dict
formats: dict
Custom formats containing a key (e.g. int64) and a value expressed as regex or callback returning bool

Example
Expand Down
Loading