Skip to content Skip to sidebar Skip to footer

How Do You Make Json $ref To Local Files?

I am using the AJV package in my node.js project. I am trying to validate some data against a couple of schema files. Both of these schema files are in the same directory: /dir

Solution 1:

It's a common misconception that $ref somehow "loads" a file.

See what ajv.js.org says:

$ref is resolved as the uri-reference using schema $id as the base URI (see the example).

And:

You don’t have to host your schema files at the URIs that you use as schema $id. These URIs are only used to identify the schemas, and according to JSON Schema specification validators should not expect to be able to download the schemas from these URIs.

Ajv won't try loading this schema from stack://over.flow/string for example:

{
  "$id": "stack://over.flow/string",
  "type": "string"
}

If you want to reference that schema in another schema, they both need to have the same base URI stack://over.flow/ e.g.,

{
  "$id":  "stack://over.flow/object",
  "type": "object",
  "properties": {
    "a": { "$ref": "string#" }
  }
}

Here { "$ref": "string#" } says "import the schema at stack://over.flow/string" so you end up with:

{
  "$id":  "stack://over.flow/object",
  "type": "object",
  "properties": {
    "a": {
      "$id": "stack://over.flow/string",
      "type": "string"
    }
  }
}

This allows you to combine small schemas:

const ajv = new Ajv;

ajv.addSchema({
  "$id": "stack://over.flow/string",
  "type": "string"
});

ajv.addSchema({
  "$id": "stack://over.flow/number",
  "type": "number"
});

const is_string = ajv.getSchema("stack://over.flow/string");
const is_number = ajv.getSchema("stack://over.flow/number");

console.log(is_string('aaa'), is_string(42));
console.log(is_number('aaa'), is_number(42));

const is_ab = ajv.compile({
  "$id":  "stack://over.flow/object",
  "type": "object",
  "properties": {
    "a": { "$ref": "string#" },
    "b": { "$ref": "number#" }
  }
});

console.log(is_ab({a: "aaa", b: 42}));
console.log(is_ab({a: 42, b: "aaa"}));
<script src="https://cdnjs.cloudflare.com/ajax/libs/ajv/6.12.2/ajv.min.js"></script>

(Please note that in your example both schemas are incorrect. You're missing {"type": "object"} in both.)

To answer your question:

const ajv = new Ajv;

ajv.addSchema({
  "$id": "stack://over.flow/parent.schema",
  "type": "object",
  "properties": {
    "foo": { "type": "string" },
    "bar": { "$ref": "child.schema#" }
  }
});

ajv.addSchema({
  "$id": "stack://over.flow/child.schema",
  "type": "object",
  "properties": {
    "sub1": { "type": "string" },
  }
});

const is_parent = ajv.getSchema("stack://over.flow/parent.schema");
const is_child = ajv.getSchema("stack://over.flow/child.schema");

console.log(is_parent({
  "foo": "whatever",
  "bar": {
    "sub1": "sometext"
  }
}));
<script src="https://cdnjs.cloudflare.com/ajax/libs/ajv/6.12.2/ajv.min.js"></script>

Post a Comment for "How Do You Make Json $ref To Local Files?"