简体   繁体   English

邮递员的jsonschema

[英]jsonschema with Postman

I made a simple web api with node.js and express.js and I want to validate the schema of incomming json data to my application. 我用node.js和express.js制作了一个简单的Web api,我想验证将json数据传入我的应用程序的模式。

I have this very exact schema : 我有这个非常精确的模式:

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"]

};

I just want to validate (for now) the first and last name. 我只想验证(现在)名字和姓氏。 So I added "additionalProperties", so only json with a first name and a last name are valid. 因此,我添加了“ additionalProperties”,因此只有具有名字和姓氏的json才有效。

When I test my app with POSTMAN with the following data, I get a lot of errors. 当我使用POSTMAN使用以下数据测试我的应用程序时,出现很多错误。 (It should be valid) (应该是有效的)

{"first_name":"Test", "last_name":"Test"}

This should be invalid : 这应该是无效的:

{"first_name":"Test", "last_name":"Test", "jibber":"ish"}

The console show about 700 lines of jsonshema validation errors : 控制台显示约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' },

Are those some hidden properties that POSTMAN use ? 那些是POSTMAN使用的隐藏属性吗?

I did some testing and my body is fine. 我做了一些测试,身体还不错。 The additional properties were generated by mongoose wich casted my body to a mongoose object. 额外的属性是由猫鼬将我的身体铸成猫鼬物体而产生的。

The validation must validate the page body only or it will fail. 验证必须仅验证页面主体,否则它将失败。

古人的智慧

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM