2

I would like to validate that a property called "foo" contains an array of "bar" objects.

Using TV4 for validation all works as expected if I use an array as the value of foo however if I specify something other than an array such as a string or an integer validation passes when it should not.

var json = { "foo" : { "one" : "bar" }};

Passes as expected with a correctly formatted array.

var json = { "foo" : { "bar" : "error" }};

Fails as expected with in-correct formatted array.

var json = { "foo" : 2 };

Passes event though foo should be an array.

Below is the full code.

var should = require("should");
var validator = require("tv4");

var barSchema = {
    "id": "bar",
    "type" : "object",
    "properties": {
        "one": {
            "type": "string"
        }
    },
    "required": ["one"],
    "additionalProperties": false
}

var fooSchema = {
    "id" : "foo",
    "title": "foo",
    "type": "object",
    "properties": {
        "foo" : {
            "type:" : "array",
            "items": {"$ref":"bar"}
        }
    },
    "required": [
        "foo"
    ]
}

describe("foo with integer", function() {
    it("should result in an error message", function(){
        var json = {
            "foo" : 2
        }
        validator.addSchema(barSchema);
        var result = validator.validate(json, fooSchema);

        // above should fail but it passes
    });
}); 
Liam Molloy
  • 21
  • 1
  • 2
  • Are there any missing schemas? (`validator.missing`) If it's a referencing issue, then the schemas it can't find will turn up there. – cloudfeet Jan 12 '15 at 18:16

1 Answers1

0

I think you missed to add fooSchema to the validator:

describe("foo with integer", function() {
    it("should result in an error message", function(){
        var json = {
            "foo" : 2
        }
        validator.addSchema(barSchema);
        validator.addSchema(fooSchema);
        var result = validator.validate(json, fooSchema);

        // above should fail but it passes
    });
});
jruizaranguren
  • 12,679
  • 7
  • 55
  • 73