jest-json-schema icon indicating copy to clipboard operation
jest-json-schema copied to clipboard

Matcher doesn't fail if not given JSON

Open tralston opened this issue 4 years ago • 2 comments

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).

tralston avatar Jun 08 '20 16:06 tralston

This issue is stale because it has been open 30 days with no activity.

github-actions[bot] avatar Jul 09 '20 00:07 github-actions[bot]

This issue is stale because it has been open 30 days with no activity.

github-actions[bot] avatar Aug 09 '20 00:08 github-actions[bot]