jsonschema - JSON Schema regarding use of $ref -


i understand $ref takes uri json schema use $ref : "#" point to? mean use current schema block level? or mean use root level schema defined in root level id?

edit: if have:

"items": {         "anyof": [             { "$ref": "#" },             { "$ref": "#/definitions/schemaarray" }         ],         "default": {}     } 

because lacks id field attempt validate instance items root schema first , if fails try validate schemaarray schema defined in definitions schema, right?

so if change to:

 "items": {             "id" : "#/items",             "anyof": [                 { "$ref": "#" },                 { "$ref": "#/definitions/schemaarray" }             ],             "default": {}         } 

then first subschema in anyof array point items schema itself?

edit #2: okay if had:

 "items": {         "id" : "itemschema",         "anyof": [             { "$ref": "#" },             { "$ref": "#/definitions/schemaarray" }         ],         "default": {}     } 

and

"stringarray": {         "type": "array",         "items": { "$ref" : "itemschema" },         "minitems": 1,         "uniqueitems": true     } 

"stringarray"'s "items" field validated against above "itemsschema"?

also second $ref in 'anyof' work going root , traversing down path till hits schema? thanks!

ok: each $ref resolved full uri. once done, questions answered asking question: what schema end with, if fetched uri? $ref is, how loaded, of irrelevant - it's entirely dependent on resolved uri.

the library might take shortcuts (like caching documents fetched once, or trusting 1 schema "speak for" another), implementation details.

response original question:

# not special: values of $ref resolved uris relative current document (or closest value of "id", if there one).

therefore, if haven't used "id", # point root of schema document. if fetched schema http://example.com/schema, {"$ref": "#"} anywhere inside resolve http://example.com/schema#, document itself.

it different when use "id", because changes "base" schema against $ref resolved:

{     "type": "array",     "items": {         "id": "http://example.com/item-schema",         "type": "object",         "additionalproperties": {"$ref": "#"}     } } 

in example, $ref resolves http://example.com/item-schema#. now, if json schema setup trusts schema has, can re-use value "items".

however, point there nothing special # - resolves uri other.

response edit 1:

your first example correct.

however, second unfortunately not. because of way fragments resolution works uris: 1 fragment completely replaces another. when resolve # against "id" value of #/items, don't end #/items again - end #. in second example, first entry in "anyof" still resolve root of document, in first example.

response edit 2:

assuming document loaded http://example.com/my-schema, full uris of 2 $refs are:

  • http://example.com/itemschema#
  • http://example.com/itemschema#/definitions/schemaarray

for first one, library may use schema has, might not - after all, looking @ uris, http://example.com/my-schema might not trusted accurately represent http://example.com/itemschema.

for second 1 - that's not going work, because "itemschema" doesn't have "definitions" section, $ref won't resolve @ all.


Comments