https://github.com/gautierdag/serverless-plugin-ts-validation
Serverless plugin to generate validation JSON schemas from Typescript interfaces
https://github.com/gautierdag/serverless-plugin-ts-validation
aws lambda schema serverless serverless-plugin validation
Last synced: 12 months ago
JSON representation
Serverless plugin to generate validation JSON schemas from Typescript interfaces
- Host: GitHub
- URL: https://github.com/gautierdag/serverless-plugin-ts-validation
- Owner: gautierdag
- License: mit
- Created: 2020-03-15T12:14:57.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2023-01-17T11:01:45.000Z (about 3 years ago)
- Last Synced: 2025-03-09T04:19:02.933Z (about 1 year ago)
- Topics: aws, lambda, schema, serverless, serverless-plugin, validation
- Language: TypeScript
- Homepage:
- Size: 203 KB
- Stars: 1
- Watchers: 0
- Forks: 0
- Open Issues: 20
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# serverless-plugin-ts-validation
[](http://www.serverless.com)
[](https://badge.fury.io/js/serverless-plugin-ts-validation)
[](https://travis-ci.com/gautierdag/serverless-plugin-ts-validation)
This is a serverless plugin made to enable automatic validation of your AWS lambda functions from typescript interfaces.
This plugin should be compatible with other plugins such as the `serverless-plugin-typescript`.
Under the hood it uses the `ts-json-schema-generator` library to generate a json schema from an interface, and sends it to AWS proxy as an input validator.
The validation enables AWS proxy to validate the input, without needing the lambda functions to be called (saving you some money 💰) and ensuring the inputs to your functions are valid.
## How to use
Define a typescript interface which you would like to use as your validation:
`src/user-schema.interface.ts`
```ts
export interface UserSchema {
id: number;
name: string;
}
```
In your `serverless.yml`, you can add the validation as follows:
```yml
plugins:
- serverless-plugin-ts-validation
---
---
---
functions:
getUser:
handler: getUser.handler
events:
- http:
path: /user
method: post
request:
schema:
application/json:
tsPath: src/user-schema.interface.ts
tsInterface: UserSchema
```
You simply have to precise the `tsPath` and `tsInterface` to use to generate the json-schema. The parsing will be done automatically using the `ts-json-schema-generator` library.
When you then pass requests to the route `user` with a content type of `application/json` the passed body will be validated to check for the `id` and `name` attributes.
## Notes:
- This currently works for simple cases, but since the json schema we create is version 7.0, and AWS only supports version 4.0, there might be cases where AWS rejects the schema generated.
- The schema files are not currently stored locally, this could be a potential future feature.
- There are no provided default `tsconfig.json`, you will need to provide your own in the root directory.
This plugin would not have been possible without:
- `ts-json-schema-generator`
- `serverless-plugin-typescript`
## PRs welcomed! 🙂