2017-07-12 3 views
0
var schema = { 
    "id": "test.json#", 
    "definitions": { 
    "body": { 
     "type": "object", 
     "properties": { 
     "name": {"type": "string"}, 
     "age": {"type": "integer"}, 
     } 
    }, 
    "request": { 
     "properties": { 
     "user": { 
      "$ref": "#/definitions/body" 
     } 
     } 
    } 
    }, 
    "typeA": { 
    "$ref": "#/definitions/request" 
    }, 
    "typeB": { 
    "allOf": [ 
     {"$ref": "#/definitions/request"}, 
     {"required": ["name"]} // Should require the name field in the user object. 
    ] 
    } 
} 

var Ajv = require('ajv') 
var ajv = Ajv({ 
    schemas: [schema] 
}) 

var data = { 
    user: { 
    "name": "tom", 
    "age": 1 
    } 
} 

var valid = ajv.validate({$ref: "test.json#/typeA"}, data) 
console.log('VALID', valid) // Returns true. 
var valid = ajv.validate({$ref: "test.json#/typeB"}, data) 
console.log('VALID', valid) // Returns false. 

私はJSONスキーマが初めてです。私は、typeAとtypeBの定義リクエストスキーマを再利用したいと思います。 typeBはユーザオブジェクトにnameフィールドを必要とするはずです。これをどのように達成するのですか?私は上記の私のソリューションが動作していない理由を知っています。必須は "user.name"のようなものでなければなりません。JSONスキーマ - ネストされたフィールドが必要です

ご協力いただきありがとうございます。ここ

答えて

0

は、ネストされたプロパティ

{ 
    "$id": "add", 
    "properties": { 
     "label": {"type": "string"}, 
     "inputs": {"type": "array", "items": {"type": "string"}}, 
     "outputs": {"type": "array", "items": {"type": "string"}}, 
     "filter": {"type": "array", "items": {"type": "string"}}, 
     "componentConfig": { 
     "type": "object", 
     "properties": { 
      "component": {"type": "string"}, 
      "resource": {"type": "array", "items": {"type": "string"}}, 
      "centerAddress": {"type": "string"} 
     }, 
     "required": ["component", "resource"] 
     } 
    }, 
    "required": ["label", "inputs", "outputs", "filter"] 
    } 
を必要とする方法の例です
関連する問題