带有回调的JSON模式解析器/验证器

JSON Schema parser/validator with callbacks?

本文关键字:验证 模式 回调 JSON      更新时间:2023-09-26

什么是JavaScript中可靠的可扩展的JSON模式验证器?从某种意义上说,在解析元素时,可以在钩子中添加功能?

JJV允许您添加自定义检查

也许是这样的:

env.addCheck('cache', function (v, p) {
    if(p) { //you don't need the 'if' if you didn't want to.
        //your caching code goes here
        return true; //to make sure it doesn't invalidate the check
    }
});

json模式片段

firstname: {
            type: 'string',
            cache: true //could be anything if you didn't want the if(p) 
        }

查看WADL规范(http://wadl.java.net)和JSON模式规范(http://json-schema.org/examples.html)。

希望能有所帮助。

Simone

FieldVal支持同步和异步验证(browser/Node.js等)以及结构化错误报告。它提供了许多常见的验证功能(即电子邮件、日期、URL等),并允许添加自定义功能。

异步验证的基本示例如下:

var params = {email: "email@example.com"}
var validator = new FieldVal(params);
//Email validation
validator.get_async("email", [BasicVal.email(), function(value, emit, callback) {  
    //Imitating asynchronous database call
    setTimeout(function() {
        callback({
            error: 1001,
            error_message: "Email already exists"
        })
    }, 5);
}]);
validator.end(function(error) {  
    if (error) {
        //Do something about the error
    } else {
        //Everything is valid - proceed
    }
});

阅读我们的博客文章,了解更多高级示例。

免责声明:我正在为这个项目做贡献。

如果您不介意给我放入的名为JULES的JSON验证器一个机会https://github.com/stamat/jules告诉我你的想法。有一种方法可以扩展JSON模式:

jules.validator.new_keyword = function(value, keyword, schema) {
    //define your validator here
    return true;
};