title: AWS SAM - Enforcing Request Validation in API Gateway Method by SAM Template
body: <p>I am working on a SAM application having a lambda function with API Gateway as source of event. API Endpoint is a POST Method requiring a set of parameters in request body. API Gateway provides us the capability of validating request body by specifying a request Model using AWS Console. </p>
  
  <p>Refer Screenshots below of AWS Console options:</p>
  
  <p><a href="https://i.stack.imgur.com/avBKD.png" rel="noreferrer"><img src="https://i.stack.imgur.com/avBKD.png" alt="enter image description here"></a></p>
  
  <p><a href="https://i.stack.imgur.com/MFzTw.png" rel="noreferrer"><img src="https://i.stack.imgur.com/MFzTw.png" alt="enter image description here"></a></p>
  
  <p>I need to set similar options via SAM template and <strong>able to link a Model</strong> with the request body but <strong>not able to set request validator option</strong> and is not able to find any documentation or example also.</p>
  
  <p>Below is my SAM Template</p>
  
  <pre><code>AWSTemplateFormatVersion: '2010-09-09'
  Transform: AWS::Serverless-2016-10-31
  
  Description: SAM Template
  
  Parameters: 
    Stage: 
      Type: String
      Default: dev
  
  Resources:
    MyApiGateway:
      Type: AWS::Serverless::Api
      Properties:
        Name: My AWS Serverless API
        StageName: !Ref Stage
        Models: 
          ExchangeRate: 
            $schema: "http://json-schema.org/draft-04/schema#"
            properties: 
              base: 
                type: string
              target: 
                type: string
            required: 
              - base
              - target
            title: User
            type: object
  
    ExchangeRateFunction:
      Type: AWS::Serverless::Function
      Properties:
        CodeUri: functions/exchange-rate/
        Handler: index.handler
        Runtime: nodejs12.x
        Description: Function to Get Currency Exchange Rate
        MemorySize: 128
        Timeout: 3
        Policies:
          - AWSLambdaBasicExecutionRole
        Events:
          HelloWorld:
            Type: Api
            Properties:
              RestApiId: !Ref MyApiGateway
              Path: /exchange
              Method: POST
              RequestModel:
                Model: ExchangeRate
                Required: true
  
  Outputs:
    ExchangeRateFunction:
      Description: "Exchange Rate Lambda Function ARN"
      Value: !GetAtt ExchangeRateFunction.Arn
    MyApiGateway:
      Description: "My Seed API EndPoint"
      Value: !Sub "https://${MyApiGateway}.execute-api.${AWS::Region}.amazonaws.com/${Stage}"
  
  </code></pre>
  
  <p>Documentation referred</p>
  
  <ul>
  <li><a href="https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-property-function-api.html" rel="noreferrer">https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-property-function-api.html</a></li>
  <li><a href="https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-resource-api.html" rel="noreferrer">https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-resource-api.html</a></li>
  </ul>
  
  <p>Please let me know how can I set 'Request Validator' to 'Validate body' option using SAM template. Will appreciate the help</p>