Skip to content

Commit 79fe069

Browse files
committed
docs: update README with request schema validators
1 parent a389613 commit 79fe069

File tree

1 file changed

+83
-0
lines changed

1 file changed

+83
-0
lines changed

README.md

+83
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ Serverless Framework v2.32.0 or later is required.
3838
- [Customizing response headers and templates](#customizing-response-headers-and-templates)
3939
- [Send request to an API](#send-request-to-an-api)
4040
- [Setting API keys for your Rest API](#setting-api-keys-for-your-rest-api)
41+
- [Request Schema Validators](#request-schema-validators)
4142
- [Schedule](#schedule)
4243
- [Enabling / Disabling](#enabling--disabling)
4344
- [Specify Name and Description](#specify-name-and-description)
@@ -933,6 +934,88 @@ Please note that those are the API keys names, not the actual values. Once you d
933934

934935
Clients connecting to this Rest API will then need to set any of these API keys values in the x-api-key header of their request. This is only necessary for functions where the private property is set to true.
935936

937+
#### Request Schema Validators
938+
939+
To use [request schema validation](https://serverless.com/framework/docs/providers/aws/events/apigateway/#request-schema-validators) with API gateway, add the [JSON Schema](https://json-schema.org/) for your content type. Since JSON Schema is represented in JSON, it's easier to include it from a file.
940+
941+
```yaml
942+
stepFunctions:
943+
stateMachines:
944+
create:
945+
events:
946+
- http:
947+
path: posts/create
948+
method: post
949+
request:
950+
schemas:
951+
application/json: ${file(create_request.json)}
952+
```
953+
954+
In addition, you can also customize created model with name and description properties.
955+
956+
```yaml
957+
stepFunctions:
958+
stateMachines:
959+
create:
960+
events:
961+
- http:
962+
path: posts/create
963+
method: post
964+
request:
965+
schemas:
966+
application/json:
967+
schema: ${file(create_request.json)}
968+
name: PostCreateModel
969+
description: 'Validation model for Creating Posts'
970+
```
971+
972+
To reuse the same model across different events, you can define global models on provider level. In order to define global model you need to add its configuration to `provider.apiGateway.request.schemas`. After defining a global model, you can use it in the event by referencing it by the key. Provider models are created for application/json content type.
973+
974+
```yaml
975+
provider:
976+
...
977+
apiGateway:
978+
request:
979+
schemas:
980+
post-create-model:
981+
name: PostCreateModel
982+
schema: ${file(api_schema/post_add_schema.json)}
983+
description: "A Model validation for adding posts"
984+
985+
stepFunctions:
986+
stateMachines:
987+
create:
988+
events:
989+
- http:
990+
path: posts/create
991+
method: post
992+
request:
993+
schemas:
994+
application/json: post-create-model
995+
```
996+
997+
A sample schema contained in `create_request.json` might look something like this:
998+
999+
```json
1000+
{
1001+
"definitions": {},
1002+
"$schema": "http://json-schema.org/draft-04/schema#",
1003+
"type": "object",
1004+
"title": "The Root Schema",
1005+
"required": ["username"],
1006+
"properties": {
1007+
"username": {
1008+
"type": "string",
1009+
"title": "The Foo Schema",
1010+
"default": "",
1011+
"pattern": "^[a-zA-Z0-9]+$"
1012+
}
1013+
}
1014+
}
1015+
```
1016+
1017+
**NOTE:** schema validators are only applied to content types you specify. Other content types are not blocked. Currently, API Gateway [supports](https://docs.aws.amazon.com/apigateway/latest/developerguide/models-mappings.html) JSON Schema draft-04.
1018+
9361019
### Schedule
9371020

9381021
The following config will attach a schedule event and causes the stateMachine `crawl` to be called every 2 hours. The configuration allows you to attach multiple schedules to the same stateMachine. You can either use the `rate` or `cron` syntax. Take a look at the [AWS schedule syntax documentation](http://docs.aws.amazon.com/AmazonCloudWatch/latest/events/ScheduledEvents.html) for more details.

0 commit comments

Comments
 (0)