flatiron / revalidator

A cross-browser / node.js validator powered by JSON Schema
http://github.com/flatiron/revalidator
Apache License 2.0
589 stars 82 forks source link

It doesn't really check if the value passed is an object #122

Open guisehn opened 6 years ago

guisehn commented 6 years ago

Revalidator type check on the root object only works if the type to be validated is an array, but it doesn't work if it's an object.


If schema.type is array and the value passed is an object:

let value = {};
let schema = { type: 'array' };
let result = revalidator.validate(value, schema);
console.log(result);

Revalidator returns an error:

{ valid: false,
  errors:
   [ { attribute: 'type',
       property: '',
       expected: 'array',
       actual: 'object',
       message: 'must be of array type' } ] }

I expected the same behavior to happen when schema.type is object, but it doesn't work. See the examples:


Example 1:

let value = [];
let schema = { type: 'object' };
let result = revalidator.validate(value, schema);
console.log(result);

returns

{ valid: true, errors: [] }

Example 2:

let value = 5;
let schema = { type: 'object' };
let result = revalidator.validate(value, schema);
console.log(result);

returns

{ valid: true, errors: [] }

Example 3:

let value = 'hi';
let schema = { type: 'object' };
let result = revalidator.validate(value, schema);
console.log(result);

returns

{ valid: false,
  errors:
   [ { attribute: 'additionalProperties',
       property: '0',
       expected: undefined,
       actual: 'h',
       message: 'must not exist' },
     { attribute: 'additionalProperties',
       property: '1',
       expected: undefined,
       actual: 'i',
       message: 'must not exist' } ] }

If we expect it to have the same behavior of the array validation, it should be returning something like:

{ valid: false,
  errors:
   [ { attribute: 'type',
       property: '',
       expected: 'object',
       actual: 'array', // or "number", or "string"
       message: 'must be of object type' } ] }