How to get json schema from another file?

I want to know how to get json schema from another 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 can see, “employees” should be an array of “people.” The problem is that I don’t know how to refer to the "person" schema because it is in a different file.

I know that this may be a simple question and that there may already be 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) to test the circuit. I also use QUnit to check if circuits work as they should.

, , . , .

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 number is a boolean.");
        start();
    });
});

, , "$.getJSON", . "Console.log(jsonSchema)" , . , "company.json".

2

, .:)

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();
    });
});
+4
1

, , , .

tv4 ( , -, ) , - , "missing" .

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

, , , tv4 :

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

script, tv4.asyncValidate(data, schema, callback), jQuery , .

+2

Source: https://habr.com/ru/post/1539741/


All Articles