You can create references using {"$ref" : "uri"} . All schemas have a
URI, and you can use relative URIs in your references. So, in your
example, your schema would look like:
{
type: 'object',
properties: {
name: {type: 'string'},
children: {type: 'array', optional: true, items: {'$ref': '#'}}
}
}
"#" is the fragment tag in a URI, which (on it's own) indicates the
root element of the URI.
You can also reference sub elements. For example, to reference
"children", you would use a relative URI of "#/properties/children".
You can also use named references by adding "id" attributes to your
schemas. So, for example, with the following schema:
{
id : '#root'
type: 'object',
properties: {
name: {id: '#name', type: 'string'},
children: {id: '#children', type: 'array', optional: true, items:
{'$ref' : '#root'}}
}
}
You can now reference children with the URI "#children", or name with "#name".
-Gary
On Thu, Nov 4, 2010 at 4:42 AM, Sebastian Kurfuerst
<sebastian.kurfue
...@gmail.com> wrote:
> Hey everybody,
> I am looking for how a recursive schema, like a schema for a file
> tree, can be defined. Imagine we have the following tree:
> {
> name: '/'
> children: [
> {
> name: 'test',
> children: [....]
> }
> ]
> }
> So, basically I want to evaluate the following:
> - make sure "name" is present everywhere
> - if there are children, that should be an array.
> So something like this:
> {
> type: 'object',
> properties: {
> name: {type: 'string'},
> children: {type: 'array', optional: true, items: /*REFERENCE TO
> "OBJECT" ABOVE */}
> }
> }
> It'd be really great if somebody could give me a hint how to reference
> the main object in this case.
> Thanks a lot,
> Sebastian
> PS: I know this question has been asked, but I did not find any
> answers yet which explained it nicely. Sorry!
> --
> You received this message because you are subscribed to the Google Groups "JSON Schema" group.
> To post to this group, send email to json-schema@googlegroups.com.
> To unsubscribe from this group, send email to json-schema+unsubscribe@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/json-schema?hl=en.