node.js - aws lambda 上的 json 模式验证

标签 node.js amazon-web-services npm aws-lambda json-schema-validator

我需要验证我的 aws lambda 事件架构。我使用vandium进行验证。我有两个不同的案例。

  1. 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 是否存在。但我需要检查是否存在任何额外的 key 。

  • lambda 函数支持多种类型的事件。
  • 像这样

    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{"name":"hello","id":100 }

    test2{"schoolName":"threni","teacher":"abcd" }

    1. 对于类似问题,哪个是最好的 scema 验证 npm 包 这?
    2. vandium是否适合 json 验证?

    最佳答案

    你看过ajv吗? ?就像 Validating Data With JSON-Schema

    关于node.js - aws lambda 上的 json 模式验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39314046/

    相关文章:

    javascript - Firebase 实时数据库分页

    java - 如何获取 *-tests.jar 以使用 AWS 设备农场配置 Appium?

    amazon-web-services - AWS :Device Name Error when converting from Elastic Beanstalk instance from m3. xlarge 到 m4.xlarge

    ios - 递归上传目录 iOS AWS S3

    css - 错误 : Task not found: "prefix.css", compress.css“在 npm 构建之后:css 命令

    android - 如何将 phonegap 更新到 3.4.0

    node.js - nodemailer Node 电子邮件模板,没有此类文件或目录错误

    javascript - 使用 Browserify 捆绑 CasperJS+SlimerJS 代码?

    node.js - 如果我将模块发布到 npm,fs 无法获取文件

    node.js - 如何使用 node.js 获取 nightwatch.js 中元素的位置