Zack
Zack

Reputation: 117

How to use ajv with dependent json-schemas?

I am tryng to make ajv work with two json-schemas, one dependent on the other. Here an example (reduced) of my schemas:

types.json

{
    "$schema":"http://json-schema.org/draft-04/schema#",
    "$id": "http://a.site.org/schemas/types.json",
    "type": "object",
    "definitions": {
        "gender":{"enum": ["male", "female"]}
    },
    "properties":{
        "gender":{"$ref": "#/definitions/gender"}
    }
}

definitions.json

{
"$schema":"http://json-schema.org/draft-04/schema#",
"$id": "http://a.site.org/schemas/definitions.json",
"definitions": {
    "person":{
        "type":"object",
        "properties": {
            "username":{"type":"string"},
            "password": {"type":"string"},
            "name":{"type":"string"},
            "surname":{"type":"string"},
            "sex":{"$ref": "types.json#/properties/gender"},
            "email":{"type":"string", "format":"email"}
        },
        "required":["name", "surname", "sex", "email"]
    }
},
"type":"object",
"properties": {
    "person": {"$ref": "#/definitions/person"}
    }
}

From node, I use ajv like this:

import Ajv, {JSONSchemaType, DefinedError} from "ajv"
import {Gender} from "./types"
import {Person} from "./definitions";
const ajv = new Ajv()

const types : JSONSchemaType<Gender>= require("../types.json");
const PersonSchema : JSONSchemaType<Person> = require('../definitions.json').definitions.person;

const isPerson = ajv.addSchema(types).compile(PersonSchema);
const riccardo:Person={name: "Riccardo", surname: "surname", sex: "male", email: "[email protected]"};
const personValid = isPerson(riccardo);
if (!personValid) console.log(isPerson.errors)

The error I get is:

Error: no schema with key or ref "http://json-schema.org/draft-04/schema#"

UPDATE: If I delete the "$schema ..." from types.json, the error I get is:

MissingRefError: can't resolve reference types.json#/definitions/gender from id #

I used this reference: https://github.com/ajv-validator/ajv/blob/master/docs/validation.md#modular-schemas

Does anyone know what I am doing wrong?

Upvotes: 0

Views: 3654

Answers (2)

Zack
Zack

Reputation: 117

Following the suggestion of an answer below, the problem was with the draft version. Here I post how I solved using a more recent draft of json-schema.

The files are now changed in this way:

types.json

{
    "$schema":"http://json-schema.org/draft-06/schema#",
    "$id": "http://asite.org/schemas/types.json",
    "type": "object",
    "definitions": {
        "gender":{"enum": ["male", "female"]}
    },
    "properties":{
        "gender":{"$ref": "#/definitions/gender"}
    }
}

definition.json:

{
    "$id": "http://asite.org/schemas/definitions.json",
    "$schema":"http://json-schema.org/draft-06/schema#",
    "definitions": {
        "person":{
            "$schema":"http://json-schema.org/draft-06/schema#",
            "$id": "http://asite.org/schemas/person.json",
            "type":"object",
            "properties": {
                "username":{"type":"string"},
                "password": {"type":"string"},
                "name":{"type":"string"},
                "surname":{"type":"string"},
                "sex":{"$ref": "types.json#/definitions/gender"},
                "email":{"type":"string", "format":"email"}
            },
            "required":["name", "surname", "sex", "email"]
        }
    },
    "type":"object",
    "properties": {
        "person": {"$ref": "#/definitions/person"}
    }
}

Used in this way:

import Ajv, {JSONSchemaType, DefinedError} from "ajv"
import {Gender} from "./types"
import {Person} from "./definitions";
const ajv = new Ajv();
ajv.addMetaSchema(require("../node_modules/ajv/lib/refs/json-schema-draft-06.json"))

const types = require("../types.json");
const PersonSchema : JSONSchemaType<Person> = require('../definitions.json').definitions.person;
const isPerson = ajv.addSchema(types).compile(PersonSchema);
const riccardo:Person={name: "Riccardo", surname: "surname", sex: "male", email: "[email protected]"};

const personValid = isPerson(riccardo);
if (!personValid) console.log(isPerson.errors)

Now different error arises, but deleting the "email" property it works, so it's a different matter:

Error: unknown format "email" ignored in schema at path "#/properties/email"

Upvotes: 1

Relequestual
Relequestual

Reputation: 12295

As of version 7, support for draft-04 has been removed. If you want to use draft-04, you need to use version 6 of ajv.

Upvotes: 0

Related Questions