array items - alternative to tuple typing

581 views
Skip to first unread message

heswell

unread,
Mar 26, 2009, 10:34:22 AM3/26/09
to JSON Schema
Hi,
I have a requirement for fairly complex json schema validation, and
have implemented my own variant of json-schema. It is so close to the
JSON-schema specification described here that it seems a shame not to
simply adopt the 'standard' version. There are a couple of features
that I use, one of which I think I'm right in saying isn't supported
bu JSON-schema (please correct me if I'm wrong about this), the other
I'm not sure about...
I need to be able to specify that an array type may contain items that
conform to any one of a list of declared types (similar to the way
that the 'type' attribute may reference an array of types). Currently,
declaring an array of types for the items attribute of an array type
(confusing, but bear with me) is interpreted as a tuple-typing
declaration. I find it hard to see the potential usage of 'tuple-
typing' though I suppose others must have a use for it.
Would it be possible to introduce an extra attribute on array type
declarations, that tells the validator to interpret an aray of types
in the 'items' attribute as alternative possible types, not tuple
types ?

Secondly, I want to be able to use the inheritance mechanism to
declare a property to be of type 'A', and have the validator allow any
valid subtype of 'A' - ie polymorphism. From my experiments, I believe
this doesn't work with JSON-schema, but I'm not 100% sure, has anyone
else tried this? If it isn't currently possible, would people consider
introducing such a possibility?

thanks

steve

Kris Zyp

unread,
Mar 26, 2009, 10:43:55 AM3/26/09
to json-...@googlegroups.com

heswell wrote:
> Hi,
> I have a requirement for fairly complex json schema validation, and
> have implemented my own variant of json-schema. It is so close to the
> JSON-schema specification described here that it seems a shame not to
> simply adopt the 'standard' version. There are a couple of features
> that I use, one of which I think I'm right in saying isn't supported
> bu JSON-schema (please correct me if I'm wrong about this), the other
> I'm not sure about...
> I need to be able to specify that an array type may contain items that
> conform to any one of a list of declared types (similar to the way
> that the 'type' attribute may reference an array of types). Currently,
> declaring an array of types for the items attribute of an array type
> (confusing, but bear with me) is interpreted as a tuple-typing
> declaration. I find it hard to see the potential usage of 'tuple-
> typing' though I suppose others must have a use for it.
> Would it be possible to introduce an extra attribute on array type
> declarations, that tells the validator to interpret an aray of types
> in the 'items' attribute as alternative possible types, not tuple
> types ?
>

I think you are describing union typing, and that is indeed supported.
For example, you could indicate an array of items where each item can be
a string or an object with a name property:
{
type:"array",
items:{
type:[{type:"string"}, {type:"object", properties:{name:{}}]
}
}

> Secondly, I want to be able to use the inheritance mechanism to
> declare a property to be of type 'A', and have the validator allow any
> valid subtype of 'A' - ie polymorphism. From my experiments, I believe
> this doesn't work with JSON-schema, but I'm not 100% sure, has anyone
> else tried this? If it isn't currently possible, would people consider
> introducing such a possibility?
>

That certainly should work. If it doesn't, it would be a bug in the
validator.
Kris

heswell

unread,
Mar 26, 2009, 10:54:20 AM3/26/09
to JSON Schema
thanks for the quick reply.
that's very encouraging. I've clearly misunderstood the spec on the
first point and probably got my structure wrong on the second. I'll
experiment further.

steve

steve heron

unread,
Mar 26, 2009, 11:45:26 AM3/26/09
to JSON Schema
Hi Kris,

I'm using the following simple example, pretty much as you posted earlier ...

var schema = {
type:"array",
items:{
type:[{type:"string"}, {type:"object", properties:{name:{type:"string"}}}]
}
};

but if I attempt to validate the following instance, which clearly
does not conform to that schema...

var instance = ["blah","vleh",{undeclared_property:23},34,[]]

The validator doesn't flag any errors.

I'm using the javascript validator for this

Is this a bug, or am I still misundestanding somethinhg?

steve

heswell

unread,
Apr 22, 2009, 4:44:47 AM4/22/09
to JSON Schema
reply to myself...yes, it was a bug and has been fixed by Kris in
jsonschema-b3
Reply all
Reply to author
Forward
0 new messages