OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Add advanced validation for AWS::Serverless::Api GET query params

  • Thread starter Thread starter BigButovskyi
  • Start date Start date
B

BigButovskyi

Guest
Im trying to add "advanced" validation to AWS::Serverless::Api GET request, query params.

I.e. minimal value of Int param: {page} should be 1. Or enum checking values

But Api gateway doesnt handle these kind of validations and only checks whether param is set.

How to do that?

I found on aws docs only information about body validation like creating models. However, it does not work with query-params

Cloudformation config

Code:
  SearchApi:
    Type: AWS::Serverless::Api
    Properties:
      Name: !Sub ${AWS::StackName}
      StageName: !Ref StageName
      DefinitionBody:
        openapi: "3.0.1"
        info: {}
        paths:
          /api/search:
            get:
              parameters:
                - name: text
                  in: "query"
                  required: true
                  schema:
                    type: "string"

                - name: page
                  in: "query"
                  required: true
                  schema:
                    type: integer
                    minimum: 1

                - name: hitsPerPage
                  in: query
                  required: true
                  schema:
                    type: integer
                    minimum: 1

                - name: subscription
                  in: query
                  required: true
                  schema:
                    type: string
                    enum: [PLUS, FREE, HD]
                    
                - name: distributionTenant
                  in: query
                  required: true
                  schema:
                    type: "string"
                    minLength: 4
  
              responses:
                "200":
                  description: "200 response"
                  headers:
                    Access-Control-Allow-Origin:
                      schema:
                        type: "string"
                    Access-Control-Allow-Methods:
                      schema:
                        type: "string"
                    Access-Control-Allow-Credentials:
                      schema:
                        type: "string"
                    Access-Control-Allow-Headers:
                      schema:
                        type: "string"
                  content: {}
              x-amazon-apigateway-request-validator: "params-only"
              x-amazon-apigateway-integration:
                credentials: !Ref APIGatewayRole
                httpMethod: "POST"
                uri:
                  Fn::Sub: "arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${SearchFunction.Arn}/invocations"
                passthroughBehavior: "when_no_match"
                type: "aws_proxy"

        x-amazon-apigateway-request-validators:
          params-only:
            validateRequestParameters: true
            validateRequestBody: false
<p>Im trying to add "advanced" validation to <code>AWS::Serverless::Api</code> <code>GET</code> request, query params.</p>
<blockquote>
<p>I.e. <code>minimal</code> value of <code>Int</code> param: <code>{page}</code> should be 1. Or enum checking values</p>
</blockquote>
<p>But Api gateway doesnt handle these kind of validations and only checks whether param is set.</p>
<p>How to do that?</p>
<p>I found on aws docs only information about <code>body</code> validation like creating models. However, it does not work with query-params</p>
<p>Cloudformation config</p>
<pre><code> SearchApi:
Type: AWS::Serverless::Api
Properties:
Name: !Sub ${AWS::StackName}
StageName: !Ref StageName
DefinitionBody:
openapi: "3.0.1"
info: {}
paths:
/api/search:
get:
parameters:
- name: text
in: "query"
required: true
schema:
type: "string"

- name: page
in: "query"
required: true
schema:
type: integer
minimum: 1

- name: hitsPerPage
in: query
required: true
schema:
type: integer
minimum: 1

- name: subscription
in: query
required: true
schema:
type: string
enum: [PLUS, FREE, HD]

- name: distributionTenant
in: query
required: true
schema:
type: "string"
minLength: 4

responses:
"200":
description: "200 response"
headers:
Access-Control-Allow-Origin:
schema:
type: "string"
Access-Control-Allow-Methods:
schema:
type: "string"
Access-Control-Allow-Credentials:
schema:
type: "string"
Access-Control-Allow-Headers:
schema:
type: "string"
content: {}
x-amazon-apigateway-request-validator: "params-only"
x-amazon-apigateway-integration:
credentials: !Ref APIGatewayRole
httpMethod: "POST"
uri:
Fn::Sub: "arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${SearchFunction.Arn}/invocations"
passthroughBehavior: "when_no_match"
type: "aws_proxy"

x-amazon-apigateway-request-validators:
params-only:
validateRequestParameters: true
validateRequestBody: false
</code></pre>
Continue reading...
 

Latest posts

Top