2013-07-27 4 views
0

ниже моей JSON схемаJSON схема элемент дополнительно, в зависимости от другого значения элемента

У меня есть зависимость, где необязателен тег упоминается должен быть справедливы только если значение ActionType идентификатора элемента «SAVECONTACT»

я не знаю, как для реализации такой DEPENDENCY

пожалуйста, помогите мне с этим

{ 
    "type": "object", 
    "properties": { 
     "userId": { 
      "type": "string", 
      "optional": true 
     }, 
     "groupId": { 
      "type": "string", 
      "optional": true 
     }, 
     "socialMediaContacts": { 
      "type": "array", 
      "items": { 
       "type": "object", 
       "properties": { 
        "smContactId": { 
         "type":"string" 
        }, 
        "actionType": { 
         "type":"string", 
         "enum" : ["SAVECONTACT", "DELETECONTACT", "SAVEGROUP", "DELETEGROUP"] 
        }, 
        "contactLastName": { 
         "type":"string", 
         "optional": true 
        }, 
        "contactFirstName": { 
         "type":"string", 
         "optional": true 
        }, 
        "nickName": { 
         "type":"string", 
         "optional": true 
        }, 
        "contactType": { 
         "type":"string", 
         "enum" : ["SM", "REG"], 
         "optional": true 
        }, 
        "mediaSource": { 
         "type":"string", 
         "enum" : ["FB", "FS", "TW"], 
         "optional": true 
        }, 
        "socialMediaHandle": { 
         "type":"string", 
         "optional": true 
        }, 
        "email": { 
         "type":"string", 
         "optional": true 
        }, 
        "phone": { 
         "type":"string", 
         "optional": true 
        } 
       } 
      } 
     } 
    } 
} 

ответ

0

Я думаю, что решение вашей проблемы может использовать тот же ап proach that suggested in this question.

Во-первых, я бы использовал стандартный способ использования «обязательного» вместо вашего пользовательского тега «optional». Потом что-то подобное может работать, если у вас есть проект 4 валидатор:

{ 
"type": "object", 
"properties": { 
    "userId": { 
     "type": "string", 
     "optional": true 
    }, 
    "groupId": { 
     "type": "string", 
     "optional": true 
    }, 
    "socialMediaContacts": { 
     "type": "array", 
     "allOf": 
     { 
      "properties": { 
        "smContactId": {"type":"string"}, 
        "actionType": {"enum" : ["SAVECONTACT", "DELETECONTACT", "SAVEGROUP", "DELETEGROUP"]}, 
        "contactLastName": {"type":"string"}, 
        "contactFirstName": {"type":"string"}, 
        "nickName": {"type":"string"}, 
        "contactType": {"type":"string","enum" : ["SM", "REG"]}, 
        "mediaSource": {"type":"string","enum" : ["FB", "FS", "TW"]}, 
        "socialMediaHandle": {"type":"string"}, 
        "email": {"type":"string"}, 
        "phone": {"type":"string"} 
       }, 
       "required": ["actionType","smContactId"], 
       "additionalProperties": False 
      }, 
     "oneOf": [ 
      { "$ref": "#/definitions/savecontact" }, 
      { "$ref": "#/definitions/otheroperations" } 
     ] 
    } 
}, 
"definitions": { 
    "savecontact": { 
     "properties": { 
      "actionType": { "enum": [ "SAVECONTACT" ] }, 

     }, 
     "required": ["contactLastName","contactFirstName", etc. etc.], 
    }, 
    "otheroperations": { 
     properties": { 
      "actionType": {"enum" : ["DELETECONTACT", "SAVEGROUP", "DELETEGROUP"]} 
     } 
    } 
} 

}

Я не знаю полный контекст вашей проблемы, но, возможно, иметь одну схему каждой операции может иметь больше смысла ,

1

Чтобы справиться с такой ситуацией, вам, вероятно, понадобится "oneOf".

В принципе, вы создаете схему для каждой ситуации - "SAVECONTACT" и не- "SAVECONTACT":

{ 
    "type": "object", 
    ... /* other constraints */ 
    "oneOf": [ 
     { 
      "properties": { 
       "actionType": {"enum": ["SAVECONTACT"]} 
      }, 
      "required": [... all the properties ...] 
     } 
     { 
      "properties": { 
       "actionType": {"enum": ["DELETECONTACT", ...]} 
      }, 
      "required": [/* empty */] 
     } 
    ] 
}