Payload Validation in AWS REST API using PYDANTIC
Published: 2022-07-27 18:18:34

This post will show an example of how to validate the payload received in a REST API developed in Python using SAM (Serverless Application Model). In addition i will show how to deploy it locally.

**PYDANTIC** provides data validation and settings management using python type annotations. Furthermore **PYDANTIC** enforces type hints at runtime, and provides user friendly errors when data is invalid.


![SAM](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/aibmn9ivxpf0w8us7p4b.png)
 

Pre-Requistes for this example:

- Setup a [Local Serverless Environment](https://dev.to/criscarba/aws-local-serverless-environment-setup-using-aws-sam-6h8)(Click to Open).
- Create a GitHub Account and Fork the following Repo: https://github.com/criscarba/aws_sam_app_public/tree/master/payload-validator . Finally clone/download it locally.

Please make sure you have successfully completed the Pre-Requisites listed above before continue. Reach out me if you have any trouble.


**STEP #1 **— Open VSCode in the directory where the repository was downloaded or cloned:


![1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jylch8ukbzw7h139fyk9.png)
 

![2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rifnj18flovyi2ttkwg5.png)
 

**STEP #2**— Build the REST API with AWS SAM

Open a new Terminal in VSCode and make sure that you are sat into the REST API path where SAM can find the “template.yaml” file and run the command:

`SAM BUILD`

![3](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i5hkab5og079xam8y0xp.png)
 

**STEP #3** — Start the API Locally

Within the terminar in VSCode run the command

`SAM LOCAL START-API`

![4](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/70yqxm30akq2b0vgwacd.png)

![5](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vfi1nqlqf33u9c4wjti4.png)
 
You will see that the application is running at http://127.0.0.1:3000 (localhost)


**STEP #4:** — Execute API using POSTMAN

Download POSTMAN from their official site https://www.postman.com/

Create a new workspace and test the API:


![Postman](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i0aufxo8jcwppbqacbpl.png)
 

**STEP #5**: Understand PYDANTIC

As explained above **PYDANTIC** provides data validation and settings management using python type annotations and enforces type hints at runtime, and provides user friendly errors when data is invalid.


![py](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7ud9vv1k1wzyirulfjnx.png)
 
Within the “services” folder there is a python code called “event_check.py” that code is a PYDANTIC Class which contains the model of the expected event payload.


![py2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s5bsjna14o3cpz43qiia.png)
 


**STEP #6**: Test PYDANTIC Behavior

Now that you understand how to create the model of your event that will allow PYDANTIC to enforce the validation, is time to call the API with with failure payloads and see the outcomes

- Send an unexpected KEY (table_2):

![py5](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4sx06j9pc6ke152drtyt.png)
 

- Send less keys than expected (Only table):

![py6](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7jdzlnl7k3ojxhj7xl23.png)
 
**
I hope the content is useful for everyone. Thanks a lot!
Cheers!**

Cristian Carballo
cristian.carballo3@gmail.com