gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 22:12:08 25 4
gpt4 key购买 nike

我需要验证我的 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/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com