2016-12-11 1 views
0

에 따라 값이 나는 장난감 3 종류를 정의하는 다음 JSON 스키마이 json GUI builder (github)와 함께 사용할 수있다 :JSON 스키마 : oneOf

{ 
    "id": "http://some.site.somewhere/entry-schema#", 
    "$schema": "http://json-schema.org/draft-04/schema#", 
    "description": "schema for toys in game", 
    "type": "object", 
    "required": [ "type" ], 
    "properties": { 
     "sawObj": { 
      "type": "object", 
      "oneOf": [ 
       { "$ref": "#/definitions/rect" }, 
       { "$ref": "#/definitions/circle" }, 
       { "$ref": "#/definitions/img" } 
      ] 
     } 
    }, 
    "definitions": { 
     "rect": { 
      "properties": { 
       "width": { "type": "integer" }, 
       "height": { "type": "integer" }, 
       "weight": { "type": "integer" } 
      }, 
      "required": [ "width", "height", "weight" ], 
      "additionalProperties": false 
     }, 
     "circle": { 
      "properties": { 
       "radius": { "type": "integer" }, 
       "weight": { "type": "integer" } 
      }, 
      "required": [ "radius", "weight" ], 
      "additionalProperties": false 
     }, 
     "img": { 
      "properties": { 
       "path": { "type": "string" }, 
       "width": { "type": "integer" }, 
       "height": { "type": "integer" }, 
       "weight": { "type": "integer" } 
      }, 
      "required": [ "path", "width", "height", "weight" ], 
      "additionalProperties": false 
     } 
    } 
} 

내가 예를 들어 원 객체를 선택하는 경우 나는 출력을 얻을 :

{ 
    "sawObj": { 
    "radius": 0, 
    "weight": 0 
    } 
} 

내가 항상 사용자에게 선택 유형을 반영하기 위해 제한 될 값 "유형"를 추가하고 싶다. 대신 다음과 같은 내용이 있습니다.

{ 
    "sawObj": { 
    "type": "circle", 
    "radius": 0, 
    "weight": 0 
    } 
} 

여기서 유형은 oneOf 속성 섹션의 사용자 선택에 따라 자동으로 결정됩니다.

어떻게 json-schema로이 작업을 수행 할 수 있습니까?

답변

0

나는 enum 값을 할 수 있었고 형식을 나타내는 단일 값만 허용 할 수있었습니다. 또한 필요에 따라 유형 값을 설정하므로 항상 자동으로 '원'으로 설정됩니다.

"circle": { 
    "properties": { 
     "radius": { 
      "type": "integer" 
     }, 
     "weight": { 
      "type": "integer" 
     }, 
     "type": { 
      "type": "string", 
      "enum": ["circle"] 
     } 
    }, 
    "required": ["radius", "weight", "type"], 
    "additionalProperties": false 
} 

참고 :이 솔루션은 이상적이지 않습니다. 나는 이것을하는 더 좋은 방법을 찾고 싶다.