2013-12-23 3 views
9

나는는 JSON 스키마 - 재귀 스키마 정의

{ 
    'description': 'TPNode', 
    'type': 'object', 
    'id': 'tp_node', 
    'properties': { 
     'selector': { 
      'type': 'string', 
      'required': true 
     }, 
     'attributes': { 
      'type': 'array', 
      'items': { 
       'name': 'string', 
       'value': 'string' 
      } 
     }, 
     'children': { 
      'type': 'array', 
      'items': { 
       'type': 'object', 
       '$ref': '#' 
      } 
     }, 
     'events': { 
      'type': 'array', 
      'items': { 
       'type': 'object', 
       'properties': { 
        'type': { 
         'type': 'string' 
        }, 
        'handler': { 
         'type': 'object' 
        }, 
        'dependencies': { 
         'type': 'array', 
         'items': { 
          'type': 'string' 
         } 
        } 
       } 
      } 
     } 
    } 
} 

내가 아이들 속성에 표현하기 위해 노력하고있어이 같은 정확한 스키마 객체의 배열 있다고하는 JSON 스키마가 있습니다. 이것을 설명하는 올바른 방법일까요?

+0

? ''required ''는 v4의 배열입니다. – cloudfeet

+0

정확합니다. 그러나, 나는 JSON.NET을 통해 스키마를 검증하고 있는데, 이는 내가 알아 냈던 것처럼 v4 구문을 지원하지 않는다. – William

답변

11

사용하면

'$ref': 'tp_node' 

여기를 참조하십시오 참조 할 필요가 스키마의 id : http://json-schema.org/latest/json-schema-core.html#anchor30

+3

("$ ref": "#"'를 사용하여) 문제에서 제안 된 해결책은 실제로 스키마가 옮겨 지거나 이름이 변경 되더라도 작동하기 때문에 실제로 더 좋습니다. 솔루션을 사용하여 스키마의 이름을 바꾸면 내부 참조가 될 수있는 참조 묶음을 변경해야합니다. – cloudfeet

+0

이 해결책이 바람직하며 받아 들여지는 것이 좋습니다. 스키마의 변경이있을 경우, "$ ref"를 사용하는보다 일반적 인 것과는 달리 명확하고 명확하게 깨집니다. "#"변경을 통해 오류를 진단하기가 더 어려워 질 수 있습니다. –

14

예, 스키마가 작동합니다. "$ref": "#"은 스키마. 서의 루트를 다시 가리 킵니다.

그러나, "type": "object" 쓸모가 : 존재

{ 
    'type': 'object', 
    '$ref': '#' 
} 

$ref 경우, 다른 모든 키워드는 무시됩니다. #/properties/children/items 스키마에서 type을 제거하는 것이 좋습니다.

3

사용 정의 및 $ ref.

다음 스키마를 복사하여 online json/schema editor에 붙여넣고 결과를 확인할 수 있습니다.

편집기 스크린 샷 :

editor screenshot

스키마 코드 : 당신은 왜 V3 구문을 사용하는

{ 
    "definitions": { 
     "TPNode": { 
      "title": "TPNode", 
      "description": "TPNode", 
      "type": "object", 
      "properties": { 
       "selector": { 
        "type": "string", 
        "required": true 
       }, 
       "attributes": { 
        "type": "array", 
        "items": { 
         "title": "Attribute", 
         "type": "object", 
         "properties": { 
          "name": { 
           "type": "string" 
          }, 
          "value": { 
           "type": "string" 
          } 
         } 
        } 
       }, 
       "children": { 
        "type": "array", 
        "items": { 
         "$ref": "#/definitions/TPNode" 
        } 
       }, 
       "events": { 
        "type": "array", 
        "items": { 
         "title": "Event", 
         "type": "object", 
         "properties": { 
          "type": { 
           "type": "string" 
          }, 
          "handler": { 
           "type": "object" 
          }, 
          "dependencies": { 
           "type": "array", 
           "items": { 
            "type": "string" 
           } 
          } 
         } 
        } 
       } 
      } 
     } 
    }, 
    "$ref": "#/definitions/TPNode" 
}