1

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 が使用するいくつかの隠しプロパティですか?

4

1 に答える 1

3

いろいろ検査しましたが、体は大丈夫です。追加のプロパティは、私の体をマングース オブジェクトにキャストしたマングースによって生成されました。

検証では、ページの本文のみを検証する必要があります。そうでない場合、検証は失敗します。

先人の知恵

于 2014-11-04T16:11:06.410 に答える