【发布时间】:2016-08-14 11:26:12
【问题描述】:
我对节点很陌生,所以可能是我没有正确使用 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 个字符,所以如果我没记错的话结果应该抛出相同的错误,为什么会这样 :(
【问题讨论】:
-
请注意您将
minLength拼写为minLenth... -
#facepalm,我浪费了将近 1 个小时,然后你来救命了……哈哈,非常感谢……我删除了这个问题,不能面对那么多的屈辱…… .. :D
标签: json node.js jsonschema