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
const getAuctionsSchema = {
type: 'object',
required: ['queryStringParameters'],
properties: {
queryStringParameters: {
type: 'object',
required: ['status'],
properties: {
status: {
default: 'OPEN',
enum: ['OPEN', 'CLOSED'],
},
},
},
},
};
module.exports = getAuctionsSchema;
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,
},
})
),
};
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'
}
]
}
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'],
},
},
},
},
};