Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Validating JSON scheme using javascript + tv4

I am trying to validate JSON scheme using TV4.

My validation is using 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 one more hierarchy level:

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

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

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

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

What am I doing wrong here?

like image 489
Yaniv Efraim Avatar asked Apr 12 '26 07:04

Yaniv Efraim


1 Answers

Disclaimer: I have never used TV4 before.

I'd guess that the schema should specify the foo property as an object with a string property... Something like:

{
    "type": "object",
    "properties": {
        "foo": {
            "properties": {
                "test": {
                   "type": "string"
                 }
            },
            "type": "object"
        }
    },
    "required": ["foo"]
}
like image 167
RobH Avatar answered Apr 13 '26 22:04

RobH