Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use definitions from external files in JSON Schema?

Tags:

jsonschema

ajv

I'm trying to import the definitions from another json schema using $ref but getting the following error:

can't resolve reference ../base/definitions.schema.json#/definitions/datetime from id #

{
  "$schema": "http://json-schema.org/draft-06/schema#",

  "definitions": {
    "datetime": {
      "type": "string"
    },
    "name": {
      "type": "string"
    },
  }
}

{
  "$schema": "http://json-schema.org/draft-06/schema#",
  "properties": {
    "active": {"type": "boolean"},
    "created_at": { "$ref": "../base/definitions.schema.json#/definitions/datetime" },
    "name": { "$ref": "../base/base/definitions.schema.json#/definitions/name" },
    "updated_at": { "$ref": "../base/definitions.schema.json#/definitions/datetime" }
  },
  "required": ["name"],
  "type": "object"
}

Directory structure:

api
-- base
  -- definitions.schema.json
-- country
  -- country.schema.json

I have tried several combinations by using an absolute path, a file url and several other combinations of the path. Not sure what's going on.

Schema validator: [email protected]

like image 238
Sayem Avatar asked Mar 07 '23 23:03

Sayem


1 Answers

You need to add schemas using "addSchema" method. $ref is resolved relative to "id" attribute ("$id" in draft-06), ajv doesn't (and can't) use file paths.

EDIT: added $ref section to docs.

like image 150
esp Avatar answered Apr 08 '23 04:04

esp