简体   繁体   中英

The id property in JSON schema v4

I'm trying to properly resolve more complex JSON schemas, which have references. The id property however gives me a lot of trouble, and is not clear for me how it works from the docs. Here are the things I still don't get:

  • Is the id property restricted to URIs ( "id": "http://some.site/my/schema.json" ) or can a piece of schema have any arbitrary id ( "id": "foo" )?
  • Is the id property hierarchical? If a parent schema has id foo (I assume ids are not limited to URis) and a child has bar , then the proper way of referencing the child is foo/bar ? Or are ids completely independent?
  • Is there a way to clearly tell if a $ref is referencing a piece of schema by id ?

The "id" is an URI. It can be a relative or absolute URI. If it is relative, then it is resolved against the parent resolution scope. If it is absolute then it isn't affected by the parent scope.

So with these in mind, the in mind

a) it can be an "arbitary id" but it will be interpreted as a relative URI

b) yes - the resolution of a relative URI happens against the parent scope

c) an empty "$ref" will do that

One thing that the spec doesn't tell you is what to do if the top-level URI is a relative URI - what will it be resolved against. It can be implementation-dependent.

As a best practice I'd advise you to use only a single "id" at the top level, and make all $refs be relative to this (unless you have a good reason to go in an other way).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM