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

Matcher doesn't fail if not given JSON #19

Open
tralston opened this issue Jun 8, 2020 · 2 comments
Open

Matcher doesn't fail if not given JSON #19

tralston opened this issue Jun 8, 2020 · 2 comments

Comments

@tralston
Copy link

tralston commented Jun 8, 2020

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

@github-actions
Copy link

github-actions bot commented Jul 9, 2020

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

@github-actions
Copy link

github-actions bot commented Aug 9, 2020

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants