json-schema-org / website

JSON Schema website
https://json-schema.org
Other
53 stars 139 forks source link

Example of biredirectional relationship #202

Closed snimavat closed 6 months ago

snimavat commented 7 years ago

https://spacetelescope.github.io/understanding-json-schema/structuring.html Relations can be expressed using $ref and definitions. Can you put an example of how to handle bidirectional relationships such as below

class Person {
  Nose nose
}

Class Nose {
  Person person
}

When generating schema for person, we can have $ref and defintion for person.. but how should the person reference in nose should be handled.

Also, How many levels deep, Human > Head > Brain > Nerves etc

mdboom commented 6 years ago

Sorry for the delayed response. I'm taking this project out of the deep freeze :)

This seems like an infinitely looping relationship. Can you provide a JSON snippet of what you are trying to validate? It seems like at a minimum you'd need to make either Person.Nose or Nose.Person optional so the data structure would have an opportunity to "stop" recursing.

Relequestual commented 6 years ago

Also, to note, the spec says you MUST NOT run $ref in a recursive loop in your schemas. https://tools.ietf.org/html/draft-handrews-json-schema-01#section-8.3

benjagm commented 10 months ago

Also, to note, the spec says you MUST NOT run $ref in a recursive loop in your schemas.

We are closing this issue as peer this last comment by @Relequestual .

gregsdennis commented 10 months ago

Also, to note, the spec says you MUST NOT run $ref in a recursive loop in your schemas.

I don't think this comment applies here. This system of schemas is completely valid and models the above:

{
  "$id": "person",
  "type": "object",
  "properties": {
    "nose": { "$ref": "nose" }
  }
}

{
  "$id": "nose",
  "type": "object",
  "properties": {
    "person": { "$ref": "person" }
  }
}

The one thing you don't want to do is list person.nose and nose.person as required. This would still be a valid schema, but you'd never be able to create an instance that satisfies the schema because it would need to be infinitely deep.

(You might be able to create a YAML 1.2 instance using YAML references, but such data wouldn't be supported by the JSON data model, so you couldn't validate it with JSON Schema.)

gregsdennis commented 10 months ago

I don't think this comment applies here.

More on this.

What is illegal is something like

{ "$ref": "#" }

which recurses directly into itself, or

{ "$id": "a", "$ref": "b" }
{ "$id": "b", "$ref": "a" }

These are disallowed because attempting to resolve the reference results in another reference that must then be resolved, which eventually leads back to needing to resolve the first reference.

But the above scenario isn't one of these cases.

benjagm commented 6 months ago

@Relequestual @gregsdennis is it ok to close this issue?