Javascript 带回调的JSON模式解析器/验证器?
什么是JavaScript中可扩展的可靠JSON模式验证器?在某种意义上,您可以在解析元素时在其中添加功能?允许您添加 也许是这样的:Javascript 带回调的JSON模式解析器/验证器?,javascript,json,validation,parsing,jsonschema,Javascript,Json,Validation,Parsing,Jsonschema,什么是JavaScript中可扩展的可靠JSON模式验证器?在某种意义上,您可以在解析元素时在其中添加功能?允许您添加 也许是这样的: 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 does
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
}
});
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
}
});
json模式片段
firstname: {
type: 'string',
cache: true //could be anything if you didn't want the if(p)
}
看看WADL规范()和JSON模式规范() 希望能有帮助
Simone如果您不介意给我装箱的名为JULES的JSON验证器一次机会,告诉我您的想法。有一种方法可以扩展JSON模式:
jules.validator.new_keyword = function(value, keyword, schema) {
//define your validator here
return true;
};
通过结构化错误报告支持同步和异步验证(browser/Node.js等)。它提供了许多常见的验证功能(如电子邮件、日期、URL等),并允许添加自定义功能
异步验证的基本示例如下所示:
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
}
});
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
}
});
阅读我们的文章,了解更多高级示例
免责声明:我为项目做出了贡献。您想要什么样的额外功能?例如,
tv4
(免责声明:我的项目)允许您定义自定义关键字(以及自定义格式等)。我需要在解析JSON文档时将其部分存储在缓存中。我在寻找类似onEachField()的回调,就像来自Ivartech的解析器JULES一样。但我也在试验tv4,因为它被广泛使用。好吧,你们想动态加载一些输入数据吗?所以当需要一个给定的属性时,你会得到某种回调?我很高兴在有需求的地方为tv4添加功能。是的,它似乎非常有用。我认为在XML世界中有很多对这类事情的支持,就像在SAX解析器中一样,但在JSON世界中还没有那么多?