Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

392
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda