Skip to content

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

@guisehn

Description

@guisehn

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' } ] }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions