Module aws_lambda_powertools.utilities.validation

Simple validator to enforce incoming/outgoing event conforms with JSON Schema

Expand source code
"""
Simple validator to enforce incoming/outgoing event conforms with JSON Schema
"""

from .exceptions import InvalidEnvelopeExpressionError, InvalidSchemaFormatError, SchemaValidationError
from .validator import validate, validator

__all__ = [
    "validate",
    "validator",
    "InvalidSchemaFormatError",
    "SchemaValidationError",
    "InvalidEnvelopeExpressionError",
]

Sub-modules

aws_lambda_powertools.utilities.validation.base
aws_lambda_powertools.utilities.validation.envelopes

Built-in envelopes

aws_lambda_powertools.utilities.validation.exceptions

Functions

def validate(event: Dict, schema: Dict, formats: Union[Dict, NoneType] = None, envelope: str = None, jmespath_options: Dict = 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
Lambda event to be validated
schema : Dict
JSON Schema to validate incoming event
envelope : Dict
JMESPath expression to filter data against
jmespath_options : Dict
Alternative JMESPath options to be included when filtering expr
formats : Dict
Custom formats containing a key (e.g. int64) and a value expressed as regex or callback returning bool

Example

Validate event

from aws_lambda_powertools.utilities.validation import validate

def handler(event, context):
    validate(event=event, schema=json_schema_dict)
    return event

Unwrap event before validating against actual payload - using built-in envelopes

from aws_lambda_powertools.utilities.validation import validate, envelopes

def handler(event, context):
    validate(event=event, schema=json_schema_dict, envelope=envelopes.API_GATEWAY_REST)
    return event

Unwrap event before validating against actual payload - using custom JMESPath expression

from aws_lambda_powertools.utilities.validation import validate

def handler(event, context):
    validate(event=event, schema=json_schema_dict, envelope="payload[*].my_data")
    return event

Unwrap and deserialize JSON string event before validating against actual payload - using built-in functions

from aws_lambda_powertools.utilities.validation import validate

def handler(event, context):
    validate(event=event, schema=json_schema_dict, envelope="Records[*].powertools_json(body)")
    return event

Unwrap, decode base64 and deserialize JSON string event before validating against actual payload - using built-in functions

from aws_lambda_powertools.utilities.validation import validate

def handler(event, context):
    validate(event=event, schema=json_schema_dict, envelope="Records[*].kinesis.powertools_json(powertools_base64(data))")
    return event

Unwrap, decompress ZIP archive and deserialize JSON string event before validating against actual payload - using built-in functions # noqa: E501

from aws_lambda_powertools.utilities.validation import validate

def handler(event, context):
    validate(event=event, schema=json_schema_dict, envelope="awslogs.powertools_base64_gzip(data) | powertools_json(@).logEvents[*]")
    return event

Raises

SchemaValidationError
When schema validation fails against data set
InvalidSchemaFormatError
When JSON schema provided is invalid
InvalidEnvelopeExpressionError
When JMESPath expression to unwrap event is invalid
Expand source code
def validate(
    event: Dict,
    schema: Dict,
    formats: Optional[Dict] = None,
    envelope: str = None,
    jmespath_options: Dict = 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
        Lambda event to be validated
    schema : Dict
        JSON Schema to validate incoming event
    envelope : Dict
        JMESPath expression to filter data against
    jmespath_options : Dict
        Alternative JMESPath options to be included when filtering expr
    formats: Dict
        Custom formats containing a key (e.g. int64) and a value expressed as regex or callback returning bool

    Example
    -------

    **Validate event**

        from aws_lambda_powertools.utilities.validation import validate

        def handler(event, context):
            validate(event=event, schema=json_schema_dict)
            return event

    **Unwrap event before validating against actual payload - using built-in envelopes**

        from aws_lambda_powertools.utilities.validation import validate, envelopes

        def handler(event, context):
            validate(event=event, schema=json_schema_dict, envelope=envelopes.API_GATEWAY_REST)
            return event

    **Unwrap event before validating against actual payload - using custom JMESPath expression**

        from aws_lambda_powertools.utilities.validation import validate

        def handler(event, context):
            validate(event=event, schema=json_schema_dict, envelope="payload[*].my_data")
            return event

    **Unwrap and deserialize JSON string event before validating against actual payload - using built-in functions**

        from aws_lambda_powertools.utilities.validation import validate

        def handler(event, context):
            validate(event=event, schema=json_schema_dict, envelope="Records[*].powertools_json(body)")
            return event

    **Unwrap, decode base64 and deserialize JSON string event before validating against actual payload - using built-in functions**

        from aws_lambda_powertools.utilities.validation import validate

        def handler(event, context):
            validate(event=event, schema=json_schema_dict, envelope="Records[*].kinesis.powertools_json(powertools_base64(data))")
            return event

    **Unwrap, decompress ZIP archive and deserialize JSON string event before validating against actual payload - using built-in functions** # noqa: E501

        from aws_lambda_powertools.utilities.validation import validate

        def handler(event, context):
            validate(event=event, schema=json_schema_dict, envelope="awslogs.powertools_base64_gzip(data) | powertools_json(@).logEvents[*]")
            return event

    Raises
    ------
    SchemaValidationError
        When schema validation fails against data set
    InvalidSchemaFormatError
        When JSON schema provided is invalid
    InvalidEnvelopeExpressionError
        When JMESPath expression to unwrap event is invalid
    """
    if envelope:
        event = unwrap_event_from_envelope(data=event, envelope=envelope, jmespath_options=jmespath_options)

    validate_data_against_schema(data=event, schema=schema, formats=formats)
def validator(handler: Callable, event: Union[Dict, str], context: Any, inbound_schema: Dict = None, inbound_formats: Union[Dict, NoneType] = None, outbound_schema: Dict = None, outbound_formats: Union[Dict, NoneType] = None, envelope: str = None, jmespath_options: Dict = None) ‑> Any

Lambda handler decorator to validate incoming/outbound data using a JSON Schema

Parameters

handler : Callable
Method to annotate on
event : Dict
Lambda event to be validated
context : Any
Lambda context object
inbound_schema : Dict
JSON Schema to validate incoming event
outbound_schema : Dict
JSON Schema to validate outbound event
envelope : Dict
JMESPath expression to filter data against
jmespath_options : Dict
Alternative JMESPath options to be included when filtering expr
inbound_formats : Dict
Custom formats containing a key (e.g. int64) and a value expressed as regex or callback returning bool
outbound_formats : Dict
Custom formats containing a key (e.g. int64) and a value expressed as regex or callback returning bool

Example

Validate incoming event

from aws_lambda_powertools.utilities.validation import validator

@validator(inbound_schema=json_schema_dict)
def handler(event, context):
    return event

Validate incoming and outgoing event

from aws_lambda_powertools.utilities.validation import validator

@validator(inbound_schema=json_schema_dict, outbound_schema=response_json_schema_dict)
def handler(event, context):
    return event

Unwrap event before validating against actual payload - using built-in envelopes

from aws_lambda_powertools.utilities.validation import validator, envelopes

@validator(inbound_schema=json_schema_dict, envelope=envelopes.API_GATEWAY_REST)
def handler(event, context):
    return event

Unwrap event before validating against actual payload - using custom JMESPath expression

from aws_lambda_powertools.utilities.validation import validator

@validator(inbound_schema=json_schema_dict, envelope="payload[*].my_data")
def handler(event, context):
    return event

Unwrap and deserialize JSON string event before validating against actual payload - using built-in functions

from aws_lambda_powertools.utilities.validation import validator

@validator(inbound_schema=json_schema_dict, envelope="Records[*].powertools_json(body)")
def handler(event, context):
    return event

Unwrap, decode base64 and deserialize JSON string event before validating against actual payload - using built-in functions # noqa: E501

from aws_lambda_powertools.utilities.validation import validator

@validator(inbound_schema=json_schema_dict, envelope="Records[*].kinesis.powertools_json(powertools_base64(data))")
def handler(event, context):
    return event

Unwrap, decompress ZIP archive and deserialize JSON string event before validating against actual payload - using built-in functions # noqa: E501

from aws_lambda_powertools.utilities.validation import validator

@validator(inbound_schema=json_schema_dict, envelope="awslogs.powertools_base64_gzip(data) | powertools_json(@).logEvents[*]")
def handler(event, context):
    return event

Returns

Any
Lambda handler response

Raises

SchemaValidationError
When schema validation fails against data set
InvalidSchemaFormatError
When JSON schema provided is invalid
InvalidEnvelopeExpressionError
When JMESPath expression to unwrap event is invalid
Expand source code
@lambda_handler_decorator
def validator(
    handler: Callable,
    event: Union[Dict, str],
    context: Any,
    inbound_schema: Dict = None,
    inbound_formats: Optional[Dict] = None,
    outbound_schema: Dict = None,
    outbound_formats: Optional[Dict] = None,
    envelope: str = None,
    jmespath_options: Dict = None,
) -> Any:
    """Lambda handler decorator to validate incoming/outbound data using a JSON Schema

    Parameters
    ----------
    handler : Callable
        Method to annotate on
    event : Dict
        Lambda event to be validated
    context : Any
        Lambda context object
    inbound_schema : Dict
        JSON Schema to validate incoming event
    outbound_schema : Dict
        JSON Schema to validate outbound event
    envelope : Dict
        JMESPath expression to filter data against
    jmespath_options : Dict
        Alternative JMESPath options to be included when filtering expr
    inbound_formats: Dict
        Custom formats containing a key (e.g. int64) and a value expressed as regex or callback returning bool
    outbound_formats: Dict
        Custom formats containing a key (e.g. int64) and a value expressed as regex or callback returning bool

    Example
    -------

    **Validate incoming event**

        from aws_lambda_powertools.utilities.validation import validator

        @validator(inbound_schema=json_schema_dict)
        def handler(event, context):
            return event

    **Validate incoming and outgoing event**

        from aws_lambda_powertools.utilities.validation import validator

        @validator(inbound_schema=json_schema_dict, outbound_schema=response_json_schema_dict)
        def handler(event, context):
            return event

    **Unwrap event before validating against actual payload - using built-in envelopes**

        from aws_lambda_powertools.utilities.validation import validator, envelopes

        @validator(inbound_schema=json_schema_dict, envelope=envelopes.API_GATEWAY_REST)
        def handler(event, context):
            return event

    **Unwrap event before validating against actual payload - using custom JMESPath expression**

        from aws_lambda_powertools.utilities.validation import validator

        @validator(inbound_schema=json_schema_dict, envelope="payload[*].my_data")
        def handler(event, context):
            return event

    **Unwrap and deserialize JSON string event before validating against actual payload - using built-in functions**

        from aws_lambda_powertools.utilities.validation import validator

        @validator(inbound_schema=json_schema_dict, envelope="Records[*].powertools_json(body)")
        def handler(event, context):
            return event

    **Unwrap, decode base64 and deserialize JSON string event before validating against actual payload - using built-in functions** # noqa: E501

        from aws_lambda_powertools.utilities.validation import validator

        @validator(inbound_schema=json_schema_dict, envelope="Records[*].kinesis.powertools_json(powertools_base64(data))")
        def handler(event, context):
            return event

    **Unwrap, decompress ZIP archive and deserialize JSON string event before validating against actual payload - using built-in functions** # noqa: E501

        from aws_lambda_powertools.utilities.validation import validator

        @validator(inbound_schema=json_schema_dict, envelope="awslogs.powertools_base64_gzip(data) | powertools_json(@).logEvents[*]")
        def handler(event, context):
            return event

    Returns
    -------
    Any
        Lambda handler response

    Raises
    ------
    SchemaValidationError
        When schema validation fails against data set
    InvalidSchemaFormatError
        When JSON schema provided is invalid
    InvalidEnvelopeExpressionError
        When JMESPath expression to unwrap event is invalid
    """
    if envelope:
        event = unwrap_event_from_envelope(data=event, envelope=envelope, jmespath_options=jmespath_options)

    if inbound_schema:
        logger.debug("Validating inbound event")
        validate_data_against_schema(data=event, schema=inbound_schema, formats=inbound_formats)

    response = handler(event, context)

    if outbound_schema:
        logger.debug("Validating outbound event")
        validate_data_against_schema(data=response, schema=outbound_schema, formats=outbound_formats)

    return response

Classes

class InvalidEnvelopeExpressionError (*args, **kwargs)

When JMESPath fails to parse expression

Expand source code
class InvalidEnvelopeExpressionError(Exception):
    """When JMESPath fails to parse expression"""

Ancestors

  • builtins.Exception
  • builtins.BaseException
class InvalidSchemaFormatError (*args, **kwargs)

When JSON Schema is in invalid format

Expand source code
class InvalidSchemaFormatError(Exception):
    """When JSON Schema is in invalid format"""

Ancestors

  • builtins.Exception
  • builtins.BaseException
class SchemaValidationError (*args, **kwargs)

When serialization fail schema validation

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

Ancestors

  • builtins.Exception
  • builtins.BaseException