我用 node.js 和 express.js 制作了一个简单的 web api,我想验证将 json 数据传入我的应用程序的模式。
我有这个非常精确的架构:
var schema = {
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "Student",
"description": "Schema for student",
"type": "object",
"properties": {
"first_name": {
"description": "Firts name of the student",
"type": "string"
},
"last_name": {
"description": "Last name of the student",
"type": "string"
}
},
"additionalProperties": false,
"required": ["first_name", "last_name"]
};
我只想验证(现在)名字和姓氏。所以我添加了“additionalProperties”,所以只有名字和姓氏的json是有效的。
当我使用 POSTMAN 使用以下数据测试我的应用程序时,我得到了很多错误。(应该是有效的)
{"first_name":"Test", "last_name":"Test"}
这应该是无效的:
{"first_name":"Test", "last_name":"Test", "jibber":"ish"}
控制台显示大约 700 行 jsonshema 验证错误:
{ instance:
{ first_name: 'Test',
last_name: 'Test',
_id: 5451419404e5006c094057c1,
},
schema:
{ '$schema': 'http://json-schema.org/draft-04/schema#',
title: 'Student',
description: 'Schema for student',
type: 'object',
properties: { first_name: [Object], last_name: [Object] },
additionalProperties: false,
required: [ 'first_name', 'first_name' ] },
propertyPath: 'instance',
errors:
[ { property: 'instance',
message: 'Property $__ does not exist in the schema',
schema: [Object],
instance:
{ first_name: 'Test',
last_name: 'Test',
_id: 5451419404e5006c094057c1,
},
stack: 'instance Property $__ does not exist in the schema' },
{ property: 'instance',
message: 'Property isNew does not exist in the schema',
schema: [Object],
instance:
{ first_name: 'Test',
last_name: 'Test',
_id: 5451419404e5006c094057c1,
},
stack: 'instance Property isNew does not exist in the schema' },
这些是邮递员使用的一些隐藏属性吗?