简体   繁体   中英

JSON-Schema: Using relative URLs in $ref

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?

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.

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