Skip to content

Matcher doesn't fail if not given JSON #19

@tralston

Description

@tralston

If the schema to be matched against doesn't contain type: 'object' at the root level, then if the received value is not JSON (e.g. a number), it won't fail.

Example:

it('should verify it has received an object'), () => {
  expect(1).toMatchSchema({
    properties: {
      a: { type: 'number' }
    }
  });
}

// should fail because received value is not JSON

For this to work, add type: 'object' at the root level of the schema

it('should verify it has received an object'), () => {
  expect(1).toMatchSchema({
    type: 'object',   // THIS WORKS
    properties: {
      a: { type: 'number' }
    }
  });
}

// properly fails because received value is not JSON

I know that the type property at the root level is part of the JSON Schema specification, and is optional, but seeing that a JSON object is always an object, and not a number or string (although it can contain a property that is a number or string), the type property at the root level should not be present in order to validate that a JSON object is being passed in.

Proposal: The first example above (without the type property at the root level) should cause the test to fail, saying it's received a value that's not JSON (or object).

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions