aws / aws/chalice

How to enable request validation?

Open
#1,724 1 comment 6 reactions 0 assignees View on GitHub
Dominant language
Python
Stars
11.1k
Forks
1k
Avg merge
1d 22h
Merged PRs (30d)
2

Description

Hi

I would like to enable request validation to my API, so I can stop invalid requests from invoking lambdas [1] and also make sure the data my users send is correct.

I've followed a couple of recommendations I found in this repo, and I used [leangle](https://github.com/aws/chalice/issues/1224#issuecomment-598215673) to add models. While the models are uploaded and are available in the API gateway console, and the Request Body is correctly assigned to my schema (called `TextSchema`), as seen in this screenshot the Request Validator setting is not set to _Validate body, query string parameters, and headers_ , and the request is not validated using the schema.

Screen Shot 2021-05-10 at 11 39 12

I've managed to make the Validator work by manually setting the correct value in the form, then deploying the API from the console, but as soon as I change something and deploy chalice from the cli (or the cdk) these values are reset and the API stops validating requests.

My API is experimental, so this is all the code

```
import leangle

from chalice import Blueprint
from marshmallow import Schema, fields

app = Blueprint("myApp")

@leangle.add_schema()
class TextSchema(Schema):
text = fields.Str()

@app.route('/test', methods=['POST'])
@leangle.describe.parameter(name='body', _in='body', description='Receive text', schema='TextSchema')
def process_text():
text = app.current_request.json_body['text']

return text
```

What is it that I am missing? Or is there a better way to do this?

Thank you

[1] https://dev.to/dvddpl/reduce-lambda-invocations-and-code-boilerplate-by-adding-a-json-schema-validator-to-your-api-gateway-15af

Contributor guide

Open the contributing guide

Research direction

Reproduce the deployment using the Blueprint route, leangle schema decorators, and the shown CLI or CDK workflow, then compare the generated API Gateway model and request-validator settings. The issue is resolved when deployment preserves body validation and invalid requests are rejected before invoking the Lambda.

Written by the indexing model from the issue text.

Assessment

Tech stack
aws, python
Domain
api, backend, cloud
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.