我需要验证我的aws lambda事件模式。我使用钒进行验证。我有两种不同的情况
- lambda函数只支持一种类型的事件。
var vandium = require('vandium');
vandium.validation({
name: vandium.types.string().required()
});
exports.handler = vandium(function (event, context, callback) {
console.log('hello: ' + event.name);
callback(null, 'Hello from Lambda');
});
在这种情况下,vandium仅在key存在或不存在的情况下有效。但是我需要检查是否有多余的密钥。
- lambda函数支持多种事件类型。 这样的
- 这是最好的场景验证npm包这个吗?
- is 钒适合json验证。
var vandium = require('vandium');
vandium.validation({
operation: vandium.types.string().required(),
name: vandium.types.string().required(), });
exports.handler = vandium(function (event, context, callback) {
const operation = event.operation;
switch (operation) {
case 'test1':
test1(event);
break;
case 'test2':
test2(event);
break;
default:
callback(new Error("Unrecognized operation=" + operation));
break;
}
function test1(event) {
//console.log('hello: ' + event.name);
callback(null, 'Hello from Lambda');
}
function test2(event) {
//console.log('hello: ' + event.name);
callback(null, 'Hello from Lambda');
}
});
在本例中为test1 &Test2是不同的。这样的
test1{"名称":"你好","id":100}
test2{"schoolName":"threni","老师":"abcd"}
你看过ajv了吗?就像在用JSON-Schema验证数据
对于那些需要在aws lambda函数上验证事件的人,@middy/validator将会有所帮助。在本例中,您需要步骤1:
import validator from '@middy/validator';
step2:定义schema
const schema = {
properties: {
body: {
type: 'object',
properties: {
name: {
type: 'string',
},
},
required: ['name'],
},
},
required: ['body'],
};
step3:使用验证器中间件
export const handler = Your_Lambda_Function
.use(validator({ inputSchema: schema }));