requires by value and two types of one fields

107 views
Skip to first unread message

Vitaliy Petkanych

unread,
Jun 7, 2012, 1:05:46 AM6/7/12
to json-...@googlegroups.com
In documentation http://tools.ietf.org/html/draft-zyp-json-schema-02#section-5.6 I found example, but this is not what I need:
   {
     "state":
     {
       "optional":true
     },
     "town":
     {
       "requires":"state",
       "optional":true
     }
   }

I can not understand, is it possible to do the following:

1) field "town" required only if "state" is "California" else "town" is optional.

2) two types validation of one field. I mean boolean and int array. 
This field can be implemented with enum [true,false,1,2,3,4,...]. 
But this way doesn't validate type of field and value isn't array. Can I do it differently?

Thanks.

P.S. Sorry for my english (I'm from Ukraine)

Francis Galiegue

unread,
Jun 7, 2012, 2:14:01 AM6/7/12
to json-...@googlegroups.com
On Thu, Jun 7, 2012 at 7:05 AM, Vitaliy Petkanych <shx...@gmail.com> wrote:
> In documentation
> http://tools.ietf.org/html/draft-zyp-json-schema-02#section-5.6 I found
> example, but this is not what I need:

This is draft version 2. The current draft is version 3.

>    {
>      "state":
>      {
>        "optional":true
>      },
>      "town":
>      {
>        "requires":"state",
>        "optional":true
>      }
>    }
>
> I can not understand, is it possible to do the following:
>
> 1) field "town" required only if "state" is "California" else "town" is
> optional.
>

This is one way to do it:

{
"notCalifornia": {
"properties": {
"state": {
"disallow": [ { "enum":[ "California" ] } ],
"type": "string"
},
"town": { "type": "string" }
}
},
"California": {
"properties": {
"state": { "enum":[ "California" ] },
"town": { "type": "string" },
"dependencies": {
"state": "town"
}
}
},
"type": [
{ "$ref": "#/notCalifornia" },
{ "$ref": "#/California" }
]
}

> 2) two types validation of one field. I mean boolean and int array.
> This field can be implemented with enum [true,false,1,2,3,4,...].
> But this way doesn't validate type of field and value isn't array. Can I do
> it differently?
>

Your enum won't work, as enum is a list of values which the instance
must match exactly. In your example, your instance must have either
value true, or false, or 1, or... etc.

It isn't really clear what you want. If you mean that your instance is
either a boolean or an array of integers, then you can write:

{
"type": [ "boolean", "array" ],
"items": { "type": "integer" }
}

If you want your instance to be an array which elements are either
integers or booleans, then this would be:

{
"type": "array",
"items": { "type": [ "boolean", "integer" ] }
}

--
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)

Vitaliy Petkanych

unread,
Jun 7, 2012, 6:59:35 AM6/7/12
to json-...@googlegroups.com
Thanks a lot, your example will solve my problem.
Reply all
Reply to author
Forward
0 new messages