array of strings and a number

Visto 19 veces
Saltar al primer mensaje no leído

shp...@gmail.com

no leída,
10 ago 2015, 8:50:1610/8/15
a JSON Schema
Hi.
How can I specify array containing one or more strings and a number at it's end?

e.g.

['a', 'b', 'c', 0.5]

['ab' ,'cd', 'ef', 'gh', 'ij' , 'ab', 0.2]

but not 
['a', 0.5, 'b']

or

[0.3]


Jason Desrosiers

no leída,
11 ago 2015, 2:14:1211/8/15
a JSON Schema,shp...@gmail.com
I'm fairly certain this can not be expressed with JSON Schema.  The only way to set a schema for a specific item in an array is to use the array form of the `items` keyword.  However, there is no way to specify the schema for the item at the end of the array without specifying the schema for all items before it.  This means you would have to specify a schema for each number of items that would be supported.  It would not be possible to support an unlimited number of items.

This would be an example of a schema that supports up to 4 items.
{
 
"type": "array",
 
"additionalItems": { "type": "number" },
 
"anyOf": [
   
{
     
"items": [
       
{ "type": "string" }
     
],
     
"minItems": 2,
     
"maxItems": 2
   
},
    {
     
"items": [
       
{ "type": "string" },
       
{ "type": "string" }
     
],
     
"minItems": 3,
     
"maxItems": 3
   
},
   
{
     
"items": [
       
{ "type": "string" },
       
{ "type": "string" },
       
{ "type": "string" }
     
],
     
"minItems": 4,
     
"maxItems": 4
   
}
  ]
}

The more items you support, the more inefficient validation will become.

If you could make the number the first item instead of the last, it would be easy to express in JSON Schema.
{
 
"type": "array",
 
"items": [{ "type": "number" }],
 
"additionalItems": { "type": "string" },
 
"minItems": 2
}

Lavi Shpigelman

no leída,
11 ago 2015, 2:23:1311/8/15
a Jason Desrosiers,JSON Schema
Thanks for the detailed answer!
I settled for holding the strings and the number separately, though the number first + strings later at is also great.
Thanks, lavi
Responder a todos
Responder al autor
Reenviar
0 mensajes nuevos