0

I'm having trouble using a parent schema that references a child schema via a relative URL.

Here's my parent schema:

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "description": "The parent schema.",
  "type": "object",
  "properties": {
    "parentProp1": { "type": "string"},
    "parentProp2": { "$ref": "child.json#"}
  }
}

Here's my child schema:

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "description": "The parent schema.",
  "type": "object",
  "properties": {
    "childProp1": { "type": "number" }
  }
}

Here's my data:

{
  "parentProp1": "one",
  "parentProp2": {
    "childProp1": 1
  }
}

Here's the error I get with ajv-cli:

$ ajv -s parent.json -d parent-test.json

schema parent.json is invalid

error: can't resolve reference child.json# from id #

I get a similar error (that child.json can't be resolved) with 3 other validators, which suggests my schema is incorrect.

What am I doing wrong?

Community
  • 1
  • 1
Horace
  • 346
  • 2
  • 14

1 Answers1

1

Are you passing the child schema as a dependency? Most validators won't automatically load schemas and need to be told about each one explicitly. There is a way to do this with ajv-cli although the syntax it escapes me at the moment. Pretty sure if you get it to print the help for the command it will tell you, though.

Henry Andrews
  • 663
  • 3
  • 6
  • I don't understand. Doesn't `$ref` indicate a dependency? – Horace Jan 11 '18 at 18:01
  • Implementations are not required to automatically load another schema. Some implementations will fetch schemas over HTTP. Some understand file URIs. Others will only load schemas you explicitly pass them. Do take a look at the ajv-cli help and see if you can pass referenced schemas on the command line. And make sure your schema has an "$id" identifying it as child.json. – Henry Andrews Jan 12 '18 at 18:22