1

It might be that the answer is so obvious, it's not even worth mentioning anywhere, but I couldn't find any explicit information about this. Can a definition of a schema (defined inside "definitions" in the JSON schema) refer to another definition?

I did not run my code yet because I am still writing the schema and I don't want to mess it up. Here is an example of what I'm trying to do:

"definitions": {
    "apple": {
        "description": "a type of fruit",
        "edible": "boolean"
    },
    "fruit": {
        "description": "a type of food",
        "edible": "boolean",
        "items": {
           "$ref": "#definitions/apple"
        }
    }
}
Dorielle
  • 85
  • 5

1 Answers1

1

This is interesting!

References from within definitions to another definition is valid.

If your reference was under items as opposed to "examples", then this would be valid, because the value of "items" must be a schema, where as the value of "examples" is not defined as a schema. ("examples" must be an array FYI.)

Relequestual
  • 11,631
  • 6
  • 47
  • 83
  • 1
    right, of course. I forgot about this with trying to demonstrate what I mean. But yes, what you said is completely what I'm trying to do. – Dorielle Apr 29 '19 at 11:18
  • 1
    Super! As a tip, always consider a subschema to have basically the same abilities as a full schema document. – Relequestual Apr 29 '19 at 11:19
  • 1
    thank you! I fixed the sample code in my question to make it comprehensible. – Dorielle Apr 29 '19 at 11:22
  • Props for that. Feel free to drop by the JSON Schema slack should you have any questions outside of the scope allowed by SO, or you want any general help. We also monitor the jsonschema tag here. – Relequestual Apr 29 '19 at 11:23
  • update: I've actually tried running my code now, and it failed! the message was "$jsonSchema keyword \'$ref\' is not currently supported". I'm using mongoDB for my database. I know that mongo does not support refs but I 'm using json schema ref parser so It's not because of that, and it has worked before adding the definitions that refer to other definitions. Is it because the path for the definitions from inside the definitions object is wrong? – Dorielle May 01 '19 at 14:58
  • A regular ref would look like this: "$ref": "#/definitions/some_defined_object", however I don't see why it should be different from within the schema, since it basically means "go back to root then look inside definitions", no? – Dorielle May 01 '19 at 14:58
  • As in https://github.com/APIDevTools/json-schema-ref-parser ? It's a bit of a misnomer, as it is non JSON Schema aware. I can't quite work out, but It sounds like there's a problem with what the reference parser you're using can support, as I would say you're correct, and that should work as I understand it. I suggest you log an issue on the github repo and wait for a response. Of course if mongoDB doesn't support $ref, you need to check that what you're putting into mongoDB does not contain $ref, without making the assumption that your reference parser is working as you expect. – Relequestual May 01 '19 at 15:03