# Add Request Validation to a Serverless API

An agent validates a Serverless API's create and update bodies with Ajv and returns 400 with field errors; npm test passes 5 of 5.

[Run this on Rush](https://cloud.getrush.ai/run/add-input-validation/serverless/). Opens the console with this prompt ready to send, in a Project that has your repository.

## What the agent does

The agent reads `aws-node-rest-api-with-dynamodb/serverless.yml`, `package.json` and the two handlers first. `todos/create.js` and `todos/update.js` each ran `JSON.parse(event.body ?? '{}')` and then a hand-written `typeof` check that answered with one error string, so a malformed body threw instead of getting a 400. The example had no `test` script.

It adds `todos/validate.js`: one `validateBody(schema, body)` on Ajv with `allErrors`, a schema for create (`text` required) and one for update (`text` and `checked`), and a 400 of `{message, errors: [{field, message}]}`. Both handlers call it before any DynamoDB call. Five `node:test` cases in `todos/validation.test.js` call the handlers directly with bad bodies.

## Start it with one API call

`POST https://api.phoenix.so/o/{org}/sessions`

```json
{
  "agent": "claude",
  "input": "A Linear issue labeled agent says: \"Add Request Validation to a Serverless API. In aws-node-rest-api-with-dynamodb, validate create and update bodies with a JSON schema and return 400 with the field errors instead of a 500.\" Do the work in this repository, add tests for it, run npm test and serverless package, and open a pull request that links back to the issue.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
```

1. **Get an API key.** In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
2. **Send the request.** POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
3. **Read what came back.** Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

## What comes back

Tried on serverless/examples at 424fe6f: 7 files changed, 629 insertions(+), 9 deletions(-), of which 531 lines are a new `package-lock.json` (the example had none). `npm test` failed on the untouched clone with `Missing script: "test"` and passed after the change with `# pass 5` and `# fail 0`. `serverless package` failed both before and after: Serverless Framework v4 will not run without `SERVERLESS_ACCESS_KEY` or `SERVERLESS_LICENSE_KEY`, and the sandbox had neither, so packaging is unverified.

For the reviewer: the schemas set `additionalProperties: false` and `minLength: 1`, so extra fields and empty text, which the old checks let through, are now refused. The error body changes from `{error}` to `{message, errors}`, which breaks a client that reads `error`. The success paths need DynamoDB and were not tested.

## Run it on every Linear issue labeled agent

An automation with a `linear` trigger on `linear.issue.labeled` and the label `agent` runs the `linear-label-to-pr` recipe's prompt when someone in your Rush org labels an issue about bad input reaching a handler. An issue that names the error body clients already read lets the agent keep `{error}` instead of switching to `{message, errors}`.

## Why run it on Rush?

The request asked for 400s instead of 500s, but the old handlers already answered 400 for a wrong type; the 500 came from malformed JSON reaching `JSON.parse` uncaught. One test sends `{nope` and expects a 400. The run also reported the failing package step instead of counting it as passed.

## Frequently asked questions

### Why did serverless package fail?

Serverless Framework v4 asks for a sign-in or a license key before any command. `npx --yes serverless package` ended with `You must sign in or use a License Key with Serverless Framework V.4 and later versions`, on the untouched code and on the change alike, so the failure is not from the change.

### Does a valid todo still reach DynamoDB?

Each handler only gained the `validateBody` call in front; the DynamoDB calls after it are unchanged. The five tests stop at the 400 branch, so the success path was not run in the trial.

## More Serverless Framework jobs

- [Add Lambda Powertools to a Serverless Framework Node.js API](https://getrush.ai/run/add-lambda-powertools/serverless/)
- [Set CloudWatch Log Retention in a Serverless Framework Service](https://getrush.ai/run/add-log-retention/serverless/)
- [Add Rate Limiting to a Serverless Framework API from a Linear Issue](https://getrush.ai/run/add-rate-limiting/serverless/)

## The same job on other stacks

- [Add Input Validation to a Slim 4 API](https://getrush.ai/run/add-input-validation/slim/)
- [Add Input Validation to a Socket.IO Chat](https://getrush.ai/run/add-input-validation/socket-io/)
- [Add Request Validation to a Vapor API](https://getrush.ai/run/add-input-validation/vapor/)

## Keep reading

- [How a Rush run works on your repository, from request to pull request](https://getrush.ai/cloud/)
- [Compare cloud agents, agent sandboxes and AI code review](https://getrush.ai/compare/)
- [Rush pricing: Free, Pro and Max](https://getrush.ai/pricing/)
