oferei / json-gate

A friendly, fast JSON schema validator
MIT License
100 stars 16 forks source link

Multiple types don't seem to work #23

Closed rhalff closed 11 years ago

rhalff commented 11 years ago

According to the readme I assumed I could use something like:

"type": ["string","number"]

but that doesn't seem to work, I get the following error:

Expected string,number got string
oferei commented 11 years ago

Yes, you can do this. Indeed it is documented (called "union type").

This code works with no error:

var createSchema = require('json-gate').createSchema;
schemaDef = { type: [ 'string', 'number' ] };
schema = createSchema(schemaDef);
schema.validate("hello");
schema.validate(42);

I don't know how to reproduce your error. Especially since I do not recognize the error you quoted. It looks nothing like the errors that json-gate produces. For example, if you pass an array to the above schema:

schema.validate([1, 2, 3]);

You'll get: "Error: JSON object is an array when it should be either a string or a number" As you can see the error is very different than the one you quoted.

So, how do you get that error?

rhalff commented 11 years ago

This is indeed my own code rejecting it, it was introduced after I read the wiki, so I jumped to conclusions too fast.

Thanks for your time and sorry to bother you on this one.

oferei commented 11 years ago

no problem.