several definitions of objects, like in xsd:all or xsd:choice

69 views
Skip to first unread message

Георгий

unread,
Sep 3, 2010, 3:52:35 PM9/3/10
to JSON Schema
http://msdn.microsoft.com/en-us/library/ms256182.aspx (xsd:all)
http://msdn.microsoft.com/en-us/library/ms256109.aspx (xsd:choice)

How can I do this?
I mean it would be great if I could write something like this:

{"type":"array",
"items": [
{
"type":"object",
"properties":{
"qwe":{ "type":"string" },
"asd":{ "type":"string", "optional":true }
}
},
{
"type":"object",
"properties":{
"zxc":{ "type":"string" }
}
}
]}


Then this array would be valid:

[{"qwe":"sssss","asd":"sss"}, {"zxc":"ssss"},
{"qwe":"sssss","asd":"sssss"}]

but these wouldn't:

[{"any":"sssss","other":"sss", "object":"sssss"}]

[{"zxc":"ssss", "qwe":"sssss","asd":"sss"}]


And any kind of 'choose-like' syntax for single objects (not arrays)
also would be very useful.

So, is there a possibility?

(Sorry if my English is a little bit terrifying.)

Георгий

unread,
Sep 3, 2010, 8:26:58 PM9/3/10
to JSON Schema
I found a theme: http://groups.google.com/group/json-schema/browse_thread/thread/b9dcd08b18cbad18
There is one piece of code.

{
"type": [
{
"properties": {
{ "name": "town" },
{ "name": "state" }
}
},
{
"properties": {
{ "name": "state", "optional": "true" }
}
}
]
}


So, I tried this:


s1 = {
"type": [
{
"properties": {
"qwe":{ "type":"string" },
"asd":{ "type":"string", "optional":true }
}
},
{
"properties": {
"zxc":{ "type":"string" }
}
}
]
}

e1 = {"qwe":"sssss","asd":"sss"}

e2 = {"zxc":"ssss", "qwe":"sssss","asd":"sss"}

e3 = {"any":"sssss","other":"sss", "object":"sssss"}

e4 = {"zxc":"ssss", "asd":"sss"}

//JSV.validate(e1, s1).errors.length // 0
//JSV.validate(e2, s1).errors.length // 0!
//JSV.validate(e3, s1).errors.length // 1
//JSV.validate(e4, s1).errors.length // 0!

//JSONSchema.validate(e1, s1).errors.length // 0
//JSONSchema.validate(e2, s1).errors.length // 0!
//JSONSchema.validate(e3, s1).errors.length // 1
//JSONSchema.validate(e4, s1).errors.length // 0!


Second and fourth example gave unexpected result. Was it conceived
from the outset?


Chrome has the function:

keys = function (o) { var a = []; for (var k in o) a.push(k); return
a; }

keys(e1) // ["qwe", "asd"]
keys(e2) // ["zxc", "qwe", "asd"]

It is not part of ecma spec, but "push" is, and "for-in" work well
with objects.
Arrays can be compared (by hand). Thus, exact comparsion is realizable.

Gary Court

unread,
Sep 3, 2010, 10:05:31 PM9/3/10
to JSON Schema
You need to use "additionalProperties:false".

On Sep 3, 6:26 pm, Георгий <httpcathar...@gmail.com> wrote:
> I found a theme:http://groups.google.com/group/json-schema/browse_thread/thread/b9dcd...

Георгий

unread,
Sep 4, 2010, 6:27:01 AM9/4/10
to JSON Schema
On 4 сен, 06:05, Gary Court <gary.co...@gmail.com> wrote:
> You need to use "additionalProperties:false".
>

Wow, it works!

s1 = {
"type": [
{
"properties": {
"qwe":{ "type":"string" },
"asd":{ "type":"string", "optional":true }
},
"additionalProperties": false
},
{
"properties": {
"zxc":{ "type":"string" }
},
"additionalProperties": false
}
]
}

I think we should write JSON Schema Primer, because such things is not
obvious from the spec.
Reply all
Reply to author
Forward
0 new messages