我对节点很新,所以我可能不正确地使用JSON Schema,如果我错了请纠正我。
我一直在使用名为jsonschema的npm模块。
对于使用验证,我使用它像:
var Validator = require('jsonschema').Validator;
var v = new Validator();
var instance = {
"user_id" : "jesus",
"password" : "password"
};
var schema = {
"id" : "Login_login",
"type" : "object",
"additionalProperties" : false,
"properties" : {
"user_id": {
"type" : "string",
"required": true,
"minLenth" : 8,
"maxLength" : 10,
"description": "User id to login."
},
"password" : {
"type" : "string",
"required": true,
"minLength" : 8,
"maxLength" : 10,
"description" : "password to login."
}
}
};
var result = v.validate(instance, schema);
console.log('>>>>>> ' + result);
但关键是结果没有错误,尽管 user_id 的 minLength 保持为8但我已经传递了5个字符,所以如果我没有错结果应该抛出一个错误,为什么会这样 :(
答案 0 :(得分:1)
模式本身需要验证。其“ user_id”条件“ minLength”的拼写不含“ g”。