Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
guisehn opened this issue Mar 2, 2018 · 0 comments
Open

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

guisehn opened this issue Mar 2, 2018 · 0 comments

Comments

@guisehn
Copy link

guisehn commented Mar 2, 2018

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' } ] }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant