lundi 27 mars 2017

Accessing nested JSON elements in JSON Schema with IF/ELSE

I have this simple JSON, which is response from the server:

{
  "key" : "balance",
  "value" : 2500,
  "serverResponse": {
    "statusCode": 200, "message": "Success". 
  }
}

In my JSON schema, all I need to specify is that if statusCode == 200 (OK from server) then key and value fields should be present. If the statusCode is 404 or 500 then key and value must not be present. I tried this in my Schema, which does not quite work:

{
  "$schema": "http://ift.tt/1n3c9zE",
  "type": "object",
  "properties": {
    "key": { "type": "string" },
    "value": { "type": "integer" },
    "serverResponse": {  "type": "object",
      "properties": {
        "statusCode": { "enum": [200,400,404,500]
        },
        "message": {
          "type": "string"
        }
      },
      "required": [
        "statusCode"
      ] },
    "statusCode": {"type": "integer"}
  },

   "anyOf": [
    {
      "properties": {
        "statusCode": { "enum": [400,404,500] }
      },
      "not": { "required": [ "key", "value" ] }
    },
    {
      "properties": {
        "statusCode": { "enum": [200] }
      },
      "required": ["key", "value"]
    }
  ],
  "required": [ "serverResponse"]   

}

This, however, works only if I put statusCode outside server response. Any thoughts on how I can get it to work?

Aucun commentaire:

Enregistrer un commentaire