# Set "Request Validator" of API Gateway by serverless

**URL:** https://forum.serverless.com/t/set-request-validator-of-api-gateway-by-serverless/7901
**Category:** Serverless Framework
**Tags:** aws, api-gateway
**Created:** [April 5, 2019, 12:01am UTC](https://forum.serverless.com/t/set-request-validator-of-api-gateway-by-serverless/7901 "2019-04-05T00:01:07Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![ikneg](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/ikneg/32/3148_2.png) [@ikneg](https://forum.serverless.com/u/ikneg)
#### Post date: [April 5, 2019, 12:01am UTC](https://forum.serverless.com/t/set-request-validator-of-api-gateway-by-serverless/7901/1 "2019-04-05T00:01:08Z")

</div>

I want to set Request Validator of API Gateway by serverless. I tried two different settings for the Request Validator. But, both methods have failed. I have summarized what I did, so please let me know if there is something wrong.

1. I write the API specification in swagger(OAS3.0). Therefore I tried to realize the setting of Request Validator using OAS extension. After I did `sls deploy` using below `swagger.yaml` and `serverless.yml`, None of the validate patterns described in `x-amazon-apigateway-request-validators` were added to the Request Validator options.  
[x-amazon-apigateway-request-validator property - Amazon API Gateway](https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-swagger-extensions-request-validator.html)

[![enter image description here](https://yyz2.discourse-cdn.com/flex036/uploads/serverless/original/2X/9/983cb0462782371a470afd361b35344889c0eb15.png)](https://forum.serverless.com/uploads/serverless/original/2X/9/983cb0462782371a470afd361b35344889c0eb15.png)

`swagger.yaml` is below:

```auto
openapi: 3.0.0

info:
  description: xxx
  version: '0.1'
  title: xxx API
x-amazon-apigateway-request-validators:
  body-only:
    validateRequestBody: true,
    validateRequestParameters: false
  except-body:
    validateRequestBody: false,
    validateRequestParameters: true
  all:
    validateRequestBody: true,
    validateRequestParameters: true
tags:
  - name: auth
    description: xxx
paths:
  /login:
    post:
      tags:
        - auth
      summary: xxx
      description: ''
      x-amazon-apigateway-request-validator: all
      responses:
        '200':
          description: success
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/AuthResponse'
        '400':
          description: fail
          content:
            application/json
        '401':
          description: fail
          content:
            application/json
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/AuthRequest'
        required: true
      x-amazon-apigateway-integration:
        responses:
          default:
            statusCode: "200"
        uri: "arn:aws:apigateway:ap-northeast-1:lambda:path/2015-03-31/functions/arn:aws:lambda:ap-northeast-1:xxxxxxxxxxxx:function:xxx-api-dev-login/invocations"
        passthroughBehavior: "when_no_match"
        httpMethod: "POST"
        contentHandling: "CONVERT_TO_TEXT"
        type: "aws_proxy"

```

My `serverless.yml` is below:

```auto
resources:
  Resources:
    RestApi :
      Type : AWS::ApiGateway::RestApi
      Properties :
        Body : ${file(./swagger.yaml)}
    LoginApiToInvokeLambda:
      Type: AWS::Lambda::Permission
      DependsOn: LoginLambdaFunction
      Properties:
        FunctionName: xxx-ext-api-dev-login
        Action: lambda:InvokeFunction
        Principal: apigateway.amazonaws.com

```

1. I tried to realize the setting of Request Validator using `AWS::ApiGateway::RequestValidator`. After I did `sls deploy` using above `swagger.yaml` and below `serverless.yml`, the `all` described in `RequestValidatorAll` in `severless.yml` were added to the Request Validator options. But the default value of Request Validator was still NONE.  
[![enter image description here](https://yyz2.discourse-cdn.com/flex036/uploads/serverless/original/2X/c/cacd34c6b9064d8692c6e29b92be1720be0270bd.png)](https://forum.serverless.com/uploads/serverless/original/2X/c/cacd34c6b9064d8692c6e29b92be1720be0270bd.png)

```auto
resources:
  Resources:
    RestApi :
      Type : AWS::ApiGateway::RestApi
      Properties :
        Body : ${file(./swagger.yaml)}
    LoginApiToInvokeLambda:
      Type: AWS::Lambda::Permission
      DependsOn: LoginLambdaFunction
      Properties:
        FunctionName: xxx-ext-api-dev-login
        Action: lambda:InvokeFunction
        Principal: apigateway.amazonaws.com
    RequestValidatorAll:
      Type: AWS::ApiGateway::RequestValidator
      Properties:
        Name: all
        RestApiId:
          Ref: RestApi
        ValidateRequestBody: true
        ValidateRequestParameters: true

```

---

<div class="post-metadata">

### Author: ![mnowik-ck](https://avatars.discourse-cdn.com/v4/letter/m/e36b37/32.png) [@mnowik-ck](https://forum.serverless.com/u/mnowik-ck)
#### Post date: [April 14, 2020, 9:07pm UTC](https://forum.serverless.com/t/set-request-validator-of-api-gateway-by-serverless/7901/2 "2020-04-14T21:07:57Z")

</div>

Below code works for me

```
  RequestValidator:
    Type: AWS::ApiGateway::RequestValidator
    Properties:
      RestApiId: !Sub "${ServerlessRestApi}"
      ValidateRequestParameters: true
```

---

<div class="post-metadata">

### Author: ![syedfaisalsaleeem](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/syedfaisalsaleeem/32/4789_2.png) [@syedfaisalsaleeem](https://forum.serverless.com/u/syedfaisalsaleeem)
#### Post date: [December 1, 2020, 1:48pm UTC](https://forum.serverless.com/t/set-request-validator-of-api-gateway-by-serverless/7901/3 "2020-12-01T13:48:32Z")

</div>

```auto
functions:

  hello:

    handler: handler.hello

    events:

      - http:

          path: /

          method: get

  likes:

    handler: handler.likes

    events:

      - http:

          path: /likes

          method: get

          integration: lambda

          reqValidatorName: xMyRequestValidator

          request:

            passThrough: NEVER

            parameters:

              querystrings:

                url: true

            template:

              application/json: '{ "userid":"$input.params(''userid'')","activityid":"$input.params(''activityid'')"}'

          response:

            headers:

              Content-Type: "'application/json'"

custom:

  wsgi:

    app: handler.app

    pythonBin: python # Some systems with Python3 may require this

    packRequirements: false

  pythonRequirements:

    dockerizePip: non-linux

resources:

  Resources:

    xMyRequestValidator:  

      Type: "AWS::ApiGateway::RequestValidator"

      Properties:

        Name: 'my-req-validator'

        RestApiId: 

          Ref: ApiGatewayRestApi

        ValidateRequestBody: true

        ValidateRequestParameters: true

```
