Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

395
Views
Serverless can't fetch all records Event object failed validation?

I am trying to fetch all records using query and JSON schema but I am keep getting Event object failed validation unless I pass a query it didn't give me any result. I am trying to fetch all the records that have status=OPEN I set the default value of status=OPEN but it looks like default value is working. Unless I pass the status=OPEN as a query Please help me!!!

And used @middy/validator for this case anyone it's been 2 days I still can't figured out the problem

JSON Schema file

const getAuctionsSchema = {
    type: 'object',
    required: ['queryStringParameters'],
    properties: {
        queryStringParameters: {
            type: 'object',
            required: ['status'],
            properties: {
                status: {
                    default: 'OPEN',
                    enum: ['OPEN', 'CLOSED'],
                },
            },
        },
    },
};

module.exports = getAuctionsSchema;

Here all records fetch file

const AWS = require('aws-sdk');
const createError = require('http-errors');
const validator = require('@middy/validator');
const commonMiddleware = require('../lib/commonMiddleware');
const getAuctionsSchema = require('../lib/schemas/getAuctionsSchema');

const dynamoDB = new AWS.DynamoDB.DocumentClient();

const get_auctions = async (event) => {
    const { status } = event.queryStringParameters;
    let auctions;

    const params = {
        TableName: process.env.AUCTIONS_TABLE_NAME,
        IndexName: 'statusAndEndDate',
        KeyConditionExpression: '#status = :status',
        ExpressionAttributeValues: {
            ':status': status,
        },
        ExpressionAttributeNames: {
            '#status': 'status',
        },
    };

    try {
        const result = await dynamoDB.query(params).promise();

        auctions = result.Items;
    } catch (err) {
        console.log(err);
        throw new createError.InternalServerError(err);
    }

    return {
        statusCode: 200,
        body: JSON.stringify(auctions),
    };
};

module.exports = {
    handler: commonMiddleware(get_auctions).use(
        validator({
            inputSchema: getAuctionsSchema,
            ajvOptions: {
                useDefaults: true,
                strict: false,
            },
        })
    ),
};

Here is the error I can see in Cloud Watch

ERROR   BadRequestError: Event object failed validation
at createError (/var/task/node_modules/@middy/util/index.js:259:10)
    at validatorMiddlewareBefore (/var/task/node_modules/@middy/validator/index.js:55:21)
    at runMiddlewares (/var/task/node_modules/@middy/core/index.js:120:88)
    at async runRequest (/var/task/node_modules/@middy/core/index.js:80:5) {
  details: [
    {
      instancePath: '',
      schemaPath: '#/required',
      keyword: 'required',
      params: [Object],
      message: 'must have required property queryStringParameters'
    }
  ]
}
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

The validator is expecting a queryStringParameters property of type object. According to the JSON Schema Specification for Objects, if a property is declared as having a certain type, that property fails validation if it is has a different type.

If you don't pass any query parameters to Api Gateway (in a Lambda Proxy integration), queryStringParameters will be null, but you have specified that it must be an object and null is not an object.

It is possible to specify several allowed types in the Schema: type: ['object', 'null']. You can read more about using several types here.


EDIT: To be able to set status to 'OPEN' even when queryStringParameters is null in the query, you can give queryStringParameters a default value (an object), with status set to 'OPEN'):

const getAuctionsSchema = {
    type: 'object',
    required: ['queryStringParameters'],
    properties: {
        queryStringParameters: {
            type: 'object',
            required: ['status'],
            default: {status: 'OPEN'},
            properties: {
                status: {
                    default: 'OPEN',
                    enum: ['OPEN', 'CLOSED'],
                },
            },
        },
    },
};
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!