2016-10-04 8 views
1

私はJSON Schema Validatorの完全な初心者ですが、私は非常に強力だと思います。しかし、私は1つのJSONを検証できません。NodeJS JSONスキーマ検証が機能しない

これは

{ 
    title: "Example Schema", 
    type: "object", 
    properties: { 
    original_image:{ 
     type: "object", 
     properties: { 
     temp_id: {type: "string"}, 
     url: {type: "string"}, 
     scale:{ 
      type: "object", 
      properties:{ 
      new_width: {type: "number"}, 
      new_height: {type: "number"} 
      }, 
      required:["new_width","new_height"] 
     } 
     }, 
     required:["url","temp_id","scale"] 
    } 
    }, 
    required:["image"] 
} 

私のスキーマであり、これは実際のJSONです:あなたは「original_image」から「URL」プロパティを見ることができるように

{ 
    "original_image": { 
    "temp_id": "this is my id", 
    "scale": { 
     "new_width": null, 
     "new_height": 329 
    } 
    } 
} 

ありませんが、検証真を返します!そして、 "new_width"の場合、値をnullに設定し、再度検証を渡すので、何が間違っているのか分かりません。

答えて

1

正常に動作しているようです。コンソールはエラーを正しく記録します。これは私のindex.js

var Validator = require('jsonschema').Validator; 
var v = new Validator(); 
var instance = { 
    "original_image": { 
    "temp_id": "this is my id", 
    "scale": { 
     "new_width": null, 
     "new_height": 329 
    } 
    } 
}; 
var schema = { 
    title: "Example Schema", 
    type: "object", 
    properties: { 
    original_image:{ 
     type: "object", 
     properties: { 
     temp_id: {type: "string"}, 
     url: {type: "string"}, 
     scale:{ 
      type: "object", 
      properties:{ 
      new_width: {type: "number"}, 
      new_height: {type: "number"} 
      }, 
      required:["new_width","new_height"] 
     } 
     }, 
     required:["url","temp_id","scale"] 
    } 
    }, 
    required:["image"] 
}; 
console.log(v.validate(instance, schema)); 
+0

私はダッシュでjson-schemaを使用していましたが、jsonschemaはうまく機能していません。だから私は "jsonschema"パッケージに切り替えるでしょう、それはうまくいった! –

0

あなたがrequired:["url","temp_id","scale"]としてあなたの条件を入れた場合は、すべての3つのプロパティは、ペイロードに必要とされているが、urlがあなたのペイロードに欠けているように見えます。 urlをオプションにするには、必要な制約に入れないでください。 バリデーターはエラーメッセージも戻します。その場合、欠落しているパラメーター/プロパティーが戻されます。

+0

いいえ、それはまさに問題です。私はURLをオプションにしたくありません。問題は、要求されているにもかかわらず、それは返されず、エラーでもないということです。それは存在しないからですが、それは真実を返すので、検証は間違っています。 –