我对 Node 还很陌生,所以可能是我没有正确使用 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 个字符,所以如果我没有错,结果应该会抛出一个错误,为什么会这样:(
架构本身需要验证。其“user_id”条件“minLength”拼写不带“g”。
我是一名优秀的程序员,十分优秀!