Schema restrictions

38 views
Skip to first unread message

Reut

unread,
Jun 18, 2017, 4:56:10 AM6/18/17
to JSON Schema
Hi ,
i am trying to define a schema validation for schema that need to contain :
{ "a" :"a value",
"b":"b value" ,
"c":"c value" ,
"properties":{
"property1Name":{
"type" :"type value",
"field1":"field1value",
"field2":"field2value"
},
"property2Name":{
"type" :"type value",
"field1":"field1value",
"field2":"field2value"
}
}
}

i define a schema to validate the restrications on each instance :
{
"a":{"type":"string"} ,
"b":{"type":"string"},
"c" :{"type":"string"} ,
"required": ["a","b","c","properties"]
}
how can i add the restriction for this properties part ? that each property will contain "field1" and "field2" ?

Thanks,
Reut





Christoph Kappestein

unread,
Jun 18, 2017, 5:31:31 AM6/18/17
to JSON Schema
Hi,

if you want that properties "property1Name" etc. are flexible you could use
"additionalProperties". Otherwise you could also hardcode the properties. Take a 
look at the following schema:

{
"type": "object",
"properties": {
"a": {
"type": "string"
},
"b": {
"type": "string"
},
"c": {
"type": "string"
},
"properties": {
"type": "object",
"additionalProperties": {
"type": "object",
"properties": {
"type": {
"type": "string"
},
"field1": {
"type": "string"
},
"field2": {
"type": "string"
}
}
}
}
},
"required": ["a","b","c","properties"]
}

Means basically inside the "properties" property you can have variable keys 
which must have as value an object with the "type", "field1" properties etc.

best regards
Christoph


Reut

unread,
Jun 19, 2017, 4:19:12 AM6/19/17
to JSON Schema
hi , 
thanks for your answer . i tried the solution you offer .
my schema now look like this  :
{
  "a": {
  "type": "string"
  },
  "b": {
  "type": "string"
  },
  "c": {
  "type": "string"
  },
  "d": {
  "type": "string"
  },
  "e": ["string"],
  "f": {
  "type": "string"
  },
  "properties": {
  "additionalProperties": {
  "type": "object",
  "properties": {
  "type": {
  "type": "string"
  },
  "field1": {
  "type": "string"
 
  },
              "required": ["type", "field1"]
  }
  },
  "required": ["a", "b", "c", "d", "properties"]
 }
when i try to validate with this schema the instance : i am not getting any error for that "field1" is missing . Do you have any idea about this ?
{
  "a": "aval",
  "b": "bval",
  "properties": {  
    "prop name": {
    "type": "number",
      "field222": "text box",
      "default": 60
    }
  }
}

Thanks,
Reut
Reply all
Reply to author
Forward
0 new messages