How would I implement RequestValidator?

hi @jayair I think this would be useful as an example as I wasn’t aware of it and encourages good practice in my eyes.

I am using your api and resource pack to use and i’ve done the following:

list:
# Defines an HTTP API endpoint that calls the main function in list.js
# - path: url path is /deals
# - method: GET request
handler: list.main
events:
  - http:
      path: deals
      method: get
      cors: true
      authorizer: aws_iam
      request:
        parameters:
          querystrings:
            country: true
            type: true
            categoryId: true
            yearMonth: true
            batchLimit: true

However, I found out validation was not turned on. From googling it seems you can do this:

Resources:
  ParameterRequestValidator:
    Type: AWS::ApiGateway::RequestValidator
    Properties:
      Name: ParameterRequestValidator
      RestApiId:
        Ref: ApiGatewayRestApi
      ValidateRequestBody: false
      ValidateRequestParameters: true

  ApiGatewayMethodNameOfYourApiLookItUpInYourTemplate:
    Properties:
      RequestValidatorId:
        Ref: ParameterRequestValidator

How can I implement this into your code base?

Thanks, S.

Hmm I’m actually not sure about this. I haven’t tried it before. You might have to look around. If you get it working, please share!

For those of you failing to see this, like I also did.

This is what you need to do in plain english.

Turn

ApiGatewayMethodNameOfYourApiLookItUpInYourTemplate

to

APIGatewayMethod<1><2>

API Gateway

In my case, it was APIGatewayDealsGet

The thing I was looking at was my handler name in serverless

   list:
    # Defines an HTTP API endpoint that calls the main function in list.js
    # - path: url path is /deals
    # - method: GET request
    handler: list.main
    events:
      - http:
          path: deals
          method: get
          cors: true
          authorizer: aws_iam
          request:
            parameters:
              querystrings:
                country: true
                type: true

Alternatively, if this does not work, check the s3 bucket, mine was called xxxxxxx-ap-serverlessdeploymentbuck-1epdp60eqveqr and go to serverless > yyyyyyyyyyy > aaaa >
timestamp > compiled-cloudformation-template.json

And look for the name of your method in there, example mine was:

	"ApiGatewayMethodDealsGet": {
		"Type": "AWS::ApiGateway::Method",
		"Properties": {
			"HttpMethod": "GET",
			"RequestParameters": {
1 Like

Awesome. Thank you for sharing!

1 Like