[英]json schema validation on aws lambda
i need to validate my aws lambda event schema.我需要验证我的 aws lambda 事件模式。 i used vandium for validation.
我使用钒进行验证。 i have two diffrent cases.
我有两种不同的情况。
like this像这样
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');
});
in this case, vandium only validate , if key is present or not.在这种情况下, vanium只验证 key 是否存在。 But i need to check if any extra key is present or not.
但我需要检查是否存在任何额外的密钥。
like this像这样
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');
}
});
in this case, events for test1 & test2 are diffrent.在这种情况下,test1 和 test2 的事件是不同的。 like this
像这样
test1{"name":"hello","id":100 }
test1{"name":"hello","id":100 }
test2{"schoolName":"threni","teacher":"abcd" }
test2{"schoolName":"threni","teacher":"abcd" }
have you taken a look at ajv ?你看过ajv吗? like in Validating Data With JSON-Schema
就像在使用 JSON-Schema 验证数据中一样
for those who need to validate events on aws lambda functions,@middy/validator would help.in this example case,you need to step 1:对于那些需要在 aws lambda 函数上验证事件的人,@middy/validator 会有所帮助。在此示例中,您需要执行步骤 1:
import validator from '@middy/validator';
step2:define a schema step2:定义模式
const schema = {
properties: {
body: {
type: 'object',
properties: {
name: {
type: 'string',
},
},
required: ['name'],
},
},
required: ['body'],
};
step3:use the validator middleware step3:使用验证器中间件
export const handler = Your_Lambda_Function
.use(validator({ inputSchema: schema }));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.