Re: [json-schema] validating dynamic JSON with json-schema

588 views
Skip to first unread message

Francis Galiegue

unread,
Sep 6, 2012, 6:15:44 AM9/6/12
to json-...@googlegroups.com
On Thu, Sep 6, 2012 at 11:46 AM, Anton Koval <psih...@gmail.com> wrote:
> Hello, guys!
>
> Please correct me, if I understand purposes/goals of json-schema mechanism
> in a wrong way.
> Lets suggest, we have two types of object within our JSON, like:
> {foo: { is: bar } } and {bar: { contains: foo } }. Call them f1 and f2
> respectively
> Based on this two objects, some other JSON (using and/or joins ) could be
> generated:
>
> {
> or: [ f1, {and: [ f1, f2 ]}, f2 ]
> }
>
> or
>
> {
> and: [ f1, f2 ]
> }
>
> or
>
> {
> and: [f2, {or: [f2, {and: [f1, f2] } ] } ]
> }
>
> tons of variants here.
>
> Is it possible to write single json-schema for validation different
> combinations of or/and with my objects f1/f2?
> Or should I validate only f1/f2 with json-schema? And all the joins
> validate with some other mechanism (like some recursive bypass with
> validation of join conditions)?
>

It is possible. The schema is unfortunately quite complicated, since
draft v3 has no "maxProperties" (it is scheduled for addition in draft
v4):

{
"type": [ { "$ref": "#/or" }, { "$ref": "#/and" } ],
"f1": {
//schema for f1 here
},
"f2": {
// schema for f2 here
},
"array-element": {
"type": [ { "$ref": "#/f1" }, { "$ref": "#/f2" }, { "$ref":
"#/or" }, { "$ref": "#/and" } ]
},
"or": {
"type": "object",
"properties": {
"or": {
"type": "array",
"items": { "$ref": "#/array-element" }
}
},
"additionalProperties": "false"
},
"and": {
"type": "object",
"properties": {
"and": {
"type": "array",
"items": { "$ref": "#/array-element" }
}
},
"additionalProperties": "false"
}
}

If you have questions about the logic used, feel free to ask!

--
Francis Galiegue, fgal...@gmail.com
JSON Schema: https://github.com/json-schema
"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)

Anton Koval

unread,
Sep 6, 2012, 9:43:33 AM9/6/12
to json-...@googlegroups.com
Francis, thank you a lot.
You've opened my eyes :)

By the way, do you know some libraries for validation by json-schema which support $ref attribute? Unfortunately, the one I was using (written in python) does not support $ref attribute.

четверг, 6 сентября 2012 г., 13:15:45 UTC+3 пользователь fge написал:

Francis Galiegue

unread,
Sep 6, 2012, 9:46:12 AM9/6/12
to json-...@googlegroups.com
On Thu, Sep 6, 2012 at 3:43 PM, Anton Koval <psih...@gmail.com> wrote:
> Francis, thank you a lot.
> You've opened my eyes :)
>

You're welcome ;)

> By the way, do you know some libraries for validation by json-schema which
> support $ref attribute? Unfortunately, the one I was using (written in
> python) does not support $ref attribute.
>

Well, mine does, but it's written in Java, not Python:
https://github.com/fge/json-schema-validator

By the way, is the Python implementation you use listed in the page
below? If not, can you provide a link?

http://json-schema.org/implementations.html

Thanks,

Anton Koval

unread,
Sep 6, 2012, 10:09:30 AM9/6/12
to json-...@googlegroups.com
Thanks for link to Java library, I'll take a look.

And I'm using python library which is mentioned on http://json-schema.org/implementations.html

четверг, 6 сентября 2012 г., 16:46:14 UTC+3 пользователь fge написал:
Reply all
Reply to author
Forward
0 new messages