coherent value dependencies

49 views
Skip to first unread message

Alexandre Conrad

unread,
Mar 14, 2012, 7:08:11 PM3/14/12
to json-...@googlegroups.com
Hi,

I am was wondering if there was a way to enforce a range to be in the right order, such as the following would be valid:

{"start": 0,
 "end": 10}

But this would be invalid:

{"start": 10,
 "end": 5}

because the "end" value can't be smaller than the "start" one.

Same would hold true with dates, say for a hotel booking period, such as:

{"checkin": "2012-01-15",
 "checkout": "2012-01-18"}

Thanks,
Alex

Francis Galiegue

unread,
Mar 15, 2012, 4:13:11 AM3/15/12
to json-...@googlegroups.com

Currently, this is not possible. This kind of checks needs to be done
post JSON Schema validation.

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

Xample

unread,
Mar 15, 2012, 4:31:40 AM3/15/12
to json-...@googlegroups.com
Not possible but you may make your own service:
{
}

which will return the schema
{"deny":"any"}
if your condition is wrong (an empty schema is ok)
This is a bit tricky but it works. I would have loved to have a property "error" and "warning" which would allow to do return schema like:
{"error":"invalid range"} (which "error" property would be caught by the validator and added to the report)
But it seems I'm the only one to see the benefits of such feature….

Francis Galiegue

unread,
Mar 15, 2012, 4:56:54 AM3/15/12
to json-...@googlegroups.com
On Thu, Mar 15, 2012 at 09:31, Xample <flavien...@gmail.com> wrote:
> Not possible but you may make your own service:
> {
>
>  "extends":{"$ref":"http://link/to/externalValidator/checkRange/{#/start}/{#/end}"}
> }
>
> which will return the schema
> {"deny":"any"}
> if your condition is wrong (an empty schema is ok)

Nice trick... But not sure about your remark about an empty schema: an
empty schema validates everything...

Alexandre Conrad

unread,
Mar 15, 2012, 1:57:38 PM3/15/12
to json-...@googlegroups.com
Le 15 mars 2012 01:13, Francis Galiegue <fgal...@gmail.com> a écrit :
> On Thu, Mar 15, 2012 at 00:08, Alexandre Conrad
>> I am was wondering if there was a way to enforce a range to be in the right
>> order, such as the following would be valid:
>>
>> {"start": 0,
>>  "end": 10}
>>
>> But this would be invalid:
>>
>> {"start": 10,
>>  "end": 5}
>
> Currently, this is not possible. This kind of checks needs to be done
> post JSON Schema validation.

Are there any plans to support this eventually? I think it could
become very useful in many situations.

Thanks,
--
Alex | twitter.com/alexconrad

Alexandre Conrad

unread,
Mar 15, 2012, 2:01:18 PM3/15/12
to json-...@googlegroups.com
Le 15 mars 2012 01:31, Xample <flavien...@gmail.com> a écrit :
> Not possible but you may make your own service:
> {
>
>  "extends":{"$ref":"http://link/to/externalValidator/checkRange/{#/start}/{#/end}"}
> }
>
> which will return the schema
> {"deny":"any"}

So is this how JSON validators should behave? Connect to an external
HTTP service and validate values? Can't it be "self-contained" ? My
validator may not have access to an external HTTP resource, and it
would also make validation very slow.

Cheers,
--
Alex | twitter.com/alexconrad

Francis Galiegue

unread,
Mar 15, 2012, 2:25:31 PM3/15/12
to json-...@googlegroups.com
On Thu, Mar 15, 2012 at 19:01, Alexandre Conrad
<alexandr...@gmail.com> wrote:
> Le 15 mars 2012 01:31, Xample <flavien...@gmail.com> a écrit :
>> Not possible but you may make your own service:
>> {
>>
>>  "extends":{"$ref":"http://link/to/externalValidator/checkRange/{#/start}/{#/end}"}
>> }
>>
>> which will return the schema
>> {"deny":"any"}
>
> So is this how JSON validators should behave? Connect to an external
> HTTP service and validate values?

Not "should". As I said, currently, nothing is defined for such checks.

But it should be easy enough to plug in your own checks, no? FWIW, I
have the structure in place in my current implementation to add
arbitrary keywords which can check whatever you like, but it's an
extension out of thin air and non standard (oh, and it's Java).

Alexandre Conrad

unread,
Mar 15, 2012, 3:35:35 PM3/15/12
to json-...@googlegroups.com
Francis,


Le 15 mars 2012 11:25, Francis Galiegue <fgal...@gmail.com> a écrit :
> On Thu, Mar 15, 2012 at 19:01, Alexandre Conrad
> <alexandr...@gmail.com> wrote:
>> Le 15 mars 2012 01:31, Xample <flavien...@gmail.com> a écrit :
>>> Not possible but you may make your own service:
>>> {
>>>
>>>  "extends":{"$ref":"http://link/to/externalValidator/checkRange/{#/start}/{#/end}"}
>>> }
>>>
>>> which will return the schema
>>> {"deny":"any"}
>>
>> So is this how JSON validators should behave? Connect to an external
>> HTTP service and validate values?
>
> Not "should". As I said, currently, nothing is defined for such checks.
>
> But it should be easy enough to plug in your own checks, no? FWIW, I
> have the structure in place in my current implementation to add
> arbitrary keywords which can check whatever you like, but it's an
> extension out of thin air and non standard (oh, and it's Java).

Thanks for the input. I will probably have a special key in my schema that will be able to take a custom callable, somehow, like you did.

From the top of my head, let me do a quick suggestion on how I could imagine this being implemented by introducing a "constraints" key, allowing to define constraints for a schema:

{"type": "object",
 "properties": {"checkin": {"type": "string", "format": "date"},
                "checkout": {"type": "string", "format": "date"}},
 "constraints": {"sequence": ["checkin", "checkout"]}}

Because the type is an object, the "sequence" constraint would take a list of keys, and the validator would have to make sure the value of these keys are ordered.

We could further extend this "constraints" for other things, such as equality verification, say, to make sure to keys match, such as passwords:

{"type": "object",
 "properties": {"oldPassword": {"type": "string"},
                "newPassword": {"type": "string"},
                "confirmPassword": {"type": "string"}},
 "constraints": {"equals": ["newPassword", "confirmPassword"]}}

Just food for thought.

Cheers,
--
Alex | twitter.com/alexconrad

Reply all
Reply to author
Forward
0 new messages