Question

I want to know how to get a json schema from an other file.

Suppose I have two files, placed in the same directory:

File 1: person.json

{
    "id":"#person",
    "name": {"type":"string"},
    "age": {"type":"number"},
    "address": {
        "type":"object",
        "properties": {
            "number": {"type":"number"},
            "street": {"type":"string"},
            "city": {"type":"string"}
        }
    }
}

File 2: company.json

{
    "id":"#company",
    "name": {"type":"string"},
    "employees": {
        "type":"array",
        "items" {"$ref":"person.json"}
    }
}

As you may observe, "employees" is supposed to be an array of "person". The problem is I don't know how to reference the "person" schema, because it is in a different file.

I am aware that this might be a simple question, and that there are maybe already answers about this, but I have already researched a lot and I don't understand how this is done.

EDIT 1

I am using Tiny Validator 4 (tv4) for schema validation. I am also using QUnit to test if the schemas are working like they should.

Below, I show you a test where the address number is boolean when it should be of type number. The schema validates, when it shouldn't.

asyncTest("invalid type for adress number", function() {
    expect(1);
    var jsonObject = {
        name: 'Computers Inc',
        employees: [
            {
                name: 'John',
                age: 29,
                address: {
                    number: 9,
                    street: 'Oak Street',
                    city: 'London'
                }
            },
            {
                name: 'Mike',
                age: 35,
                address: {
                    number: true,
                    street: 'Big Avenue',
                    city: 'London'
                }
            }
        ]
    };

    // Gets the JSON Schema
    $.getJSON('json_schemas/company.json', function(response) {
        var jsonSchema = response;
        console.log(jsonSchema);

        // Verifies the validity of the JSON object
        var valid = tv4.validate(jsonObject, jsonSchema);
        ok(!valid, "Not valid because Mike's number is a boolean.");
        start();
    });
});

After looking at it for a while, I think it is the "$.getJSON" who is messing it up. The "console.log(jsonSchema)" shows that the fetched schema doesn't include the person part. The fetched schema is exactly the one from the "company.json" file.

EDIT 2

I think it is working like this. :)

asyncTest("invalid type for address number", function() {
    expect(1);
    var jsonObject = {
        name: 'Computers Inc',
        employees: [
            {
                name: 'John',
                age: 29,
                address: {
                    number: 9,
                    street: 'Oak Street',
                    city: 'London'
                }
            },
            {
                name: 'Mike',
                age: 35,
                address: {
                    number: false,
                    street: 'Big Avenue',
                    city: 'London'
                }
            }
        ]
    };

    tv4.asyncValidate(jsonObject, 'json_schemas/company.json', function(valid) {
        ok(!valid, printMessage(valid));
        start();
    });
});
Was it helpful?

Solution

Your schemas look like they should work - so my guess is that you aren't loading the schemas into your validator properly.

With tv4 (which you are apparently using), you should be able to see if you are missing any schemas using the "missing" property of your results.

var result = tv4.validateMultiple(data, schema);
console.log(result.missing); // ['/json_schemas/missing-schema']

To fix this, you need to load any schemas you need into tv4 before validation:

tv4.addSchema('/json_schemas/missing-schema', ...)

There's also an example script that adds a tv4.asyncValidate(data, schema, callback) method, which uses jQuery to fetch missing schemas and try again, which might be useful to you.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top