无服务器无法获取所有记录事件对象验证失败?
Serverless can't fetch all records Event object failed validation?
我正在尝试使用查询和 JSON 架构获取所有记录,但我一直收到 Event object failed validation
除非我通过查询它没有给我任何结果。
我正在尝试获取所有具有 status=OPEN
的记录
我设置了 status=OPEN
的默认值,但看起来默认值有效。
除非我将 status=OPEN
作为查询传递
请帮帮我!!!
并在这种情况下使用了 @middy/validator
已经 2 天了,我仍然无法解决问题
JSON 架构文件
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,
},
})
),
};
这是我在 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'
}
]
}
验证器需要 queryStringParameters
属性 object
类型。根据 JSON Schema Specification for Objects,如果 属性 被声明为具有特定类型,则如果 属性 具有不同的类型,则验证失败。
如果您不将任何查询参数传递给 Api 网关(在 Lambda 代理集成中),queryStringParameters 将为 null
,但您已指定它必须是一个对象并且 null is not an object.
可以在架构中指定几种允许的类型:type: ['object', 'null']
。您可以阅读有关使用多种类型的更多信息 here.
编辑:
即使在查询中 queryStringParameters
为 null
时也能将 status
设置为 'OPEN',您可以给 queryStringParameters
一个默认值(一个对象),使用status
设置为 'OPEN'):
const getAuctionsSchema = {
type: 'object',
required: ['queryStringParameters'],
properties: {
queryStringParameters: {
type: 'object',
required: ['status'],
default: {status: 'OPEN'},
properties: {
status: {
default: 'OPEN',
enum: ['OPEN', 'CLOSED'],
},
},
},
},
};
我正在尝试使用查询和 JSON 架构获取所有记录,但我一直收到 Event object failed validation
除非我通过查询它没有给我任何结果。
我正在尝试获取所有具有 status=OPEN
的记录
我设置了 status=OPEN
的默认值,但看起来默认值有效。
除非我将 status=OPEN
作为查询传递
请帮帮我!!!
并在这种情况下使用了 @middy/validator
已经 2 天了,我仍然无法解决问题
JSON 架构文件
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,
},
})
),
};
这是我在 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'
}
]
}
验证器需要 queryStringParameters
属性 object
类型。根据 JSON Schema Specification for Objects,如果 属性 被声明为具有特定类型,则如果 属性 具有不同的类型,则验证失败。
如果您不将任何查询参数传递给 Api 网关(在 Lambda 代理集成中),queryStringParameters 将为 null
,但您已指定它必须是一个对象并且 null is not an object.
可以在架构中指定几种允许的类型:type: ['object', 'null']
。您可以阅读有关使用多种类型的更多信息 here.
编辑:
即使在查询中 queryStringParameters
为 null
时也能将 status
设置为 'OPEN',您可以给 queryStringParameters
一个默认值(一个对象),使用status
设置为 'OPEN'):
const getAuctionsSchema = {
type: 'object',
required: ['queryStringParameters'],
properties: {
queryStringParameters: {
type: 'object',
required: ['status'],
default: {status: 'OPEN'},
properties: {
status: {
default: 'OPEN',
enum: ['OPEN', 'CLOSED'],
},
},
},
},
};