Skip to content

Input Validation

This utility provides input validation and advanced parsing. It mitigates any hidden input assumption including value constraints.

Rule of thumb: Always, always, always (!), validate all input.

Input Validation

Key features

The Parser will validate the incoming event, extract the input business payload, decode it and validate it according to a predefined schema.

This schema will verify that all required parameters exist, their type is as expected and validate any value constraints.

And all this will be achieved with one line of code.

This "magic" line will allow you to focus on your business logic and not worry about metadata and encapsulating services.

Service Envelope

When an AWS Service sends an event that triggers your AWS Lambda function, metadata information is added to the event, and the business logic payload is encapsulated.

Let's call this metadata information 'envelope.'

The envelope contains valuable information, interesting headers, and the interesting part, the business logic input that you wish to process.

That's where it gets tricky.

Each AWS service has its envelope structure and may encapsulate the business logic payload differently.

Supported AWS Services

For a complete list click here.

Define Business Logic Schema

Use Pydantic schemas to define the expected input format. Extend 'BaseModel' class.

Define type and value constraints.

1
2
3
4
5
from pydantic import BaseModel, PositiveInt, constr

class Input(BaseModel):
    customer_name: constr(min_length=1, max_length=20)
    order_item_count: PositiveInt

The schema defines:

  1. 'customer_name' - customer name, a non-empty string with up to 20 characters.
  2. 'order_item_count' - a positive integer representing the number of ordered items that 'customer_name' placed.

Learn about models here and about advanced parsing here.

Usage in Handler

The parser is a called with the function 'parse'.

Use the envelope class that matches the AWS service that triggers your AWS Lambda function.

my_handler.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
from http import HTTPStatus
from typing import Any

from aws_lambda_powertools.utilities.parser import ValidationError, parse
from aws_lambda_powertools.utilities.parser.envelopes import ApiGatewayEnvelope
from aws_lambda_powertools.utilities.typing import LambdaContext

from .schema import Input


def my_handler(event: dict[str, Any], context: LambdaContext):
    try:
        input: Input = parse(event=event, model=Input, envelope=ApiGatewayEnvelope)  # noqa: F841
    except (ValidationError, TypeError):
        # log error, return BAD_REQUEST
        return {'statusCode': HTTPStatus.BAD_REQUEST}
    # process input

Accessing Envelope Metadata

You can access the metadata parameters by extending the model class and parsing the input without the envelope class.

Read more about it here.

Blog Reference

Read more about the importance of input validation and the potential pitfalls it prevents in my blog. Click HERE.

More Details

You can find more information at the official documentation.

Go to https://docs.powertools.aws.dev/lambda-python/latest/utilities/parser/