Yes. You can do, for instance:
----
{
"rootSchema": {
"patternProperties": {
"Pane": { "$ref": "#/paneSchema" }
}
},
"paneSchema": {
"properties": {
//define a pane object here
}
}
}
----
and then validate using this schema starting at #/rootSchema.
#/rootSchema and #/paneSchema are JSON paths.
I'm not sure about the "name" attribute either.
--
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)
This property in this example should probably be "title" and not
"name". But in any case, it is an attribute that has no definition as
per the JSON Schema schema (which is allowed).
This is not the only example in draft 3 that has errors. These will be
fixed come draft 4.
--Gary
Any idea on the date?
You have written "Pane" in uppercase in the schema, this is why!
Anyway, with my implementation
(https://github.com/fge/json-schema-validator), after fixing that, it
gives what is expected:
----
public final class Test
{
public static void main(final String... args)
throws IOException
{
final JsonNode schema = JsonLoader.fromResource("/t/schema.json");
final JsonNode data = JsonLoader.fromResource("/t/data.json");
final JsonValidator validator = new JsonValidator(schema);
final ValidationReport report = validator.validate("#/rootSchema",
data);
System.out.println("Success: " + report.isSuccess());
for (final String msg: report.getMessages())
System.out.println(msg);
System.exit(0);
}
}
----
The output:
----
Success: false
#/pane/height: instance is of type string, which is none of the
allowed primitive types ([integer, number])
----
Yes, indeed, sorry for that :/
> I have created a Gist with the exact code that I am running, and a
> bl.ocks that runs it:
>
> http://gist.github.com/1364126
> http://bl.ocks.org/1364126
>
> If you run that second link, you should see that the validator claims
> that the json object is valid.
>
Yes indeed, and it is indeed wrong about this. But as I don't do
javascript, I won't be able to help...
If you try to inline the schema, ie:
----
{
"properties": {
"pane": {
"properties": {
"height": {
"type": "number"
}
}
}
}
}
----
and change your call to "dojox.json.schema.validate(doc, schema)", does it work?
> The way you are running it is different from the way I am running it.
The paths I use are written following the JSON Pointer draft, that is why.
> Maybe the pure-javascript Dojox version doesn't support local
> references?
>
That would be strange, since it is a base feature of json schema...
But maybe it does not support JSON Pointer? Can you also try and write
"#.paneSchema" instead of "#/paneSchema"? If yes then there is a
problem in this implementation.
1. You can not do referencing that way. References must point to a
subschema, or an already registered schema. This is what you should be
doing:
schema = {
"patternProperties": {
"pane": {
"properties": {
"height": {
"type": "number"
}
}
}
}
}
2. I don't know how up-to-date Dojo's JSON Schema implementation is. I
would recommend taking a look at JSV
(https://github.com/garycourt/JSV).
--Gary
Hmm, can you please define "registered schema"?
Personally, and my implementation does, I consider the schema valid
because it is what the validator is told to eat -- with a "$schema"
identifier (or is that "id"?) of "#". How is this interpretation
flawed? (and I am the culprit, I am the one who suggested the
solution)
Beware with patternProperties, regexes are not anchored by default:
/topPane/ will match property "NOTtopPaneITellYou".
Maybe you could then write:
----
{
"patternProperties": {
"Pane$": {
// etc
----
I'm at a loss here. What happens if a schema does NOT have a "$schema"
or "id" field in it? Is it considered invalid by the current draft?
Can't we use "anonymous" schemas? Plenty of people do that... And
examples already exist which address subschemas using JSON Pointers
from a same root document. My example looked perfectly valid to me.
So, what is what?
Here's what your example should look like with references:
schema = {
"patternProperties": {
"topPane": {
"properties": {
"height": {
"type": "number"
}
}
},
"middlePane": {"$ref" : "#/patternProperties/topPane"},
"bottomPane": {"$ref" : "#/patternProperties/topPane"}
}
}
The problem with your previous gist was that you were calling the dojo
validator with "schema.rootSchema". In JavaScript, that cuts off any
reference to "schema.paneSchema". The above example alleviates that
problem.
--Gary
A registered schema is a schema that is already known by the
validator. For example, "http://json-schema.org/schema" is a
registered schema in almost all validators.
--Gary
Yes, you can do that. JSV will accept this schema if you move the
"paneSchema" property to the top of the object (JSV 3.x only supports
backward references, will be fixed in the next release).
--Gary
Oopps... Sorry, you can't actually do that because JSV doesn't know
that "paneSchema" is a schema and not just an object. My bad. You will
have to go back to using my previous example instead.
--Gary
This normally shouldn't: $ref just doesn't point anywhere, and to my
eyes, it works only by chance (because JSV is written in Javascript
maybe?). "paneSchema" is not a valid JSON Pointer.
Don't forget that $ref is a URI, and such a thing as "paneSchema" is a
URI, yes, but relative to... What? This isn't defined in the draft at
the moment, but consider this, which is legal in $ref:
some/thing/here#/some/path
This has to be clarified. For me, "paneSchema" as a value in $ref is
just illegal, unless you consider a schema itself to be a base for
relative URIs without a JSON Pointer. But then what of JSON Pointer in
such a case?