Re: [json-schema] enum field on array type

2,144 views
Skip to first unread message

Francis Galiegue

unread,
Jun 24, 2012, 6:15:26 AM6/24/12
to json-...@googlegroups.com
On Mon, Jun 18, 2012 at 1:09 PM, <luismari...@gmail.com> wrote:
> hi, reading the latest draft from the RFC I'm implementing the enum check
> for the array type, I want to know if the enum will be used to check
> if the whole array matches one item from enum or if the enum field will be
> used to check individual items of the array
>
> to be clear, enum should be an array of arrays that defines all the arrays
> that can be defined as valid values for that field (with comparison as of
> section 5.17) or should it be an array of items that defines which array
> items are valid?
>

It will check equality of the array to the value in the enum. A JSON
array is a JSON value like any other, after all.

Two array JSON instances are equal if and only if:

* they both have the same number of items, and
* an item at index n in the first instance equals the item at index n
in the second.

And this definition is, of course, recursive.

Similarly, equality for object instances is defined as follows:

* both object instances have the same set of keys, and
* for key k, the value of this key in the first instance equals the
value of this same key in the second instance.

Finally, it should be noted for completeness that the _order_ of keys
in a JSON object instance does _not_ matter:

{
"a": "b",
"c": "d"
}

is equals to

{
"c": "d",
"a": "b"
}

--
Francis Galiegue, fgal...@gmail.com
"It seems obvious [...] that at least some 'business intelligence'
tools invest so much intelligence on the business side that they have
nothing left for generating SQL queries" (Stéphane Faroult, in "The
Art of SQL", ISBN 0-596-00894-5)

Francis Galiegue

unread,
Jun 25, 2012, 3:20:00 AM6/25/12
to json-...@googlegroups.com
On Sun, Jun 24, 2012 at 12:15 PM, Francis Galiegue <fgal...@gmail.com> wrote:
[...]
>
> It will check equality of the array to the value in the enum. A JSON
> array is a JSON value like any other, after all.
>

Let me be more clear about this, since the wording is a little poor.

When a schema has an "enum" keyword, the value of the instance _must_
be one of the values in the "enum" array.

Therefore, if you have:

{
"enum": [ [ "a", "b" ], null, [ 1, 2, 3 ] ]
}

then it means your instance must be either of [ "a", "b" ], or the
null value, or [ 1, 2, 3 ].

If you wanted to restrict the set of possible values of items in an
array, then you could write:

{
"type": "array",
"items": {
"enum": [ "a", "b", 1, 2 ]
}
}

Hope this helps,
Reply all
Reply to author
Forward
0 new messages