【问题标题】:jsonschema with Postman带有邮递员的 jsonschema
【发布时间】:2014-10-29 19:51:13
【问题描述】:

我使用 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' },

那些是 POSTMAN 使用的隐藏属性吗?

【问题讨论】:

  • 作为一种解决方法,如果您想允许以$ 开头的所有内容,您可以使用"patternProperties":{"^\$":{}} - 每个以$ 开头的属性都将匹配(但没有限制)。跨度>
  • 有很多不以$开头的属性,比如我的错误输出的最后一行:instance Property isNew。

标签: node.js express jsonschema postman


【解决方案1】:

我做了一些测试,我的身体很好。附加属性是由 mongoose 生成的,它将我的身体投射到一个 mongoose 对象上。

验证必须仅验证页面正文,否则将失败。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-10-22
    • 2019-04-16
    • 2021-11-24
    • 2017-05-17
    • 2021-05-29
    • 2018-06-25
    • 2022-08-02
    • 1970-01-01
    相关资源
    最近更新 更多