JSON schema validation using javascript + tv4

I am trying to test a JSON scheme using TV4 .

My check uses hierarchical JSON and is based on this basic example :

var data = {
    "foo": "bar"
};

var schema = {
    "type": "object",
    "properties": {
        "foo": {
            "type": "string"
        }
    },
    "required": ["foo"]
};

var result = tv4.validateResult(data, schema);

In my test, I want to add another level of hierarchy:

 var data = {
        "foo": {
           "test": "bar"
        }
    };

    var schema = {
        "type": "object",
        "properties": {
            "foo": {
                    "test": {
                       "type": "string"
                     }
            }
        },
        "required": ["foo"]
    };

    var result = tv4.validateResult(data, schema);

This check does not work (if I put an integer instead of a string, it passes validation )

What am I doing wrong here?

+3
source share
2 answers

Disclaimer: I have never used TV4 before.

I assume that the scheme should indicate the property fooas objectwith the property string... Something like:

{
    "type": "object",
    "properties": {
        "foo": {
            "properties": {
                "test": {
                   "type": "string"
                 }
            },
            "type": "object"
        }
    },
    "required": ["foo"]
}
+4
source
0

All Articles