Giter VIP home page Giter VIP logo

jest-json-schema's People

Contributors

10xlacroixdrinker avatar adwilk avatar anescobar1991 avatar avantar avatar benmclees avatar dependabot[bot] avatar dogpatch626 avatar flupke avatar jamessingleton avatar lvl99 avatar nellyk avatar oneamexbot avatar realityking avatar tklever avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

jest-json-schema's Issues

jest-json-schema does not work when there is a missing field

Hi,

We found jest-json-schema does not work with missing field.
Endpoint : https://reqres.in/api/users/2
Method : GET
Actual json response :

{
id: 2,
first_name: 'Janet',
last_name: 'Weaver',
avatar:'https://s3.amazonaws.com/uifaces/faces/twitter/josephstein/128.jpg'
}

The code:

const { matchers } = require('jest-json-schema');
expect.extend(matchers);
const supertest = require('supertest');
const env = require('dotenv').config();
const api = supertest(process.env.API_BASE_URL);
const getSingleUser = id => api.get(`/users/${id}`)
  .set('Content-Type', 'application/json')
  .set('Accept', 'application/json');
let response;
describe('Test User', () => {
  test('Get single user dengan id 2', async () => {
    response = await getSingleUser(2);
    expect(response.status, 'Ini harus response 200').toBe(200);
    console.log(response.body.data);
    const schemaDetailUser = {
      type: 'object',
      required: ['first_name', 'last_name', 'avatar'],
      properties: {
        first_name: { type: 'string' },
        last_name: { type: 'string' },
        avatar: { type: 'string' },
      },
    };
    expect(response.body.data).toMatchSchema(schemaDetailUser);
  });
});

We expect that there will be an error occured because the 'id' field is missed.
But actually it does not display a error.
Or do I missed something?

Thank you

Matcher doesn't fail if not given JSON

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

Reporting a vulnerability

Hello!

I hope you are doing well!

We are a security research team. Our tool automatically detected a vulnerability in this repository. We want to disclose it responsibly. GitHub has a feature called Private vulnerability reporting, which enables security research to privately disclose a vulnerability. Unfortunately, it is not enabled for this repository.

Can you enable it, so that we can report it?

Thanks in advance!

PS: you can read about how to enable private vulnerability reporting here: https://docs.github.com/en/code-security/security-advisories/repository-security-advisories/configuring-private-vulnerability-reporting-for-a-repository

[Feature Request] Generate schema from object (when checking matcher for the first time)

There is couple of tools for JSON schema validation by @bahmutov:

That tools depends on nijikokun/generate-schema.

In the essence, bahmutov/schema-shot records snapshot in the first time and validates schema later on (by using bahmutov/validate-by-example).

It would be great to introduce schema generation functionality into the jest-json-schema.

What do you think?

Support matching against schemas in files

Folks could load their own schemas and match against the result, but having help methods to do that and an example in the README would be A++ if you get the time. :)

validates with ajv but not with jest-json-schema

Hello, Im able to validate a data file against a schema via ajv-cli but not with jest-json-schema@5

Any pointers?

The schema is https://json.schemastore.org/tsconfig

The test is:

import { matchersWithOptions } from 'jest-json-schema'
import schema from '../../../schemata/tsconfig.json'
import base from './base.json'

expect.extend(matchersWithOptions({
    schemas: [base]
  }));

it('should match schema', async () => {
  expect(base).toMatchSchema(schema)
})

Which yields:

 FAIL   @librelabs/tsconfig  src/base.spec.ts
  ✕ should match schema (8 ms)

  ● should match schema

    no schema with key or ref "http://json-schema.org/draft-04/schema#"

       8 | 
       9 | it('should match schema', async () => {
    > 10 |   expect(base).toMatchSchema(schema)
         |                ^
      11 | })

And using ajv directly without using strict mode:

$ ajv validate -s ../../schemata/tsconfig.json -d ./src/base.json 
strict mode: missing type "object" for keyword "properties" at "https://json.schemastore.org/tsconfig#/definitions/compilerOptionsDefinition" (strictTypes)

"$schema" in schema itself is not ignored

When the schema itself contains a key "$schema": "https://json-schema.org/draft/2020-12/schema", this is thrown as an error.

Reproduction:

it('should succeed', () => {
    const schema = {
        $id: 'https://my-schema.org/SomeObject.json',
        $schema: 'https://json-schema.org/draft/2020-12/schema',
        title: 'Some title',
        description: 'Some description',
        type: 'object',
        additionalProperties: true,
        properties: {
            id: {
                description: 'An id',
                type: 'string',
            },
        },
    }

    expect({
        $schema: 'https://my-schema.org/SomeObject.json',
        id: 'some id',
    }).toMatchSchema(schema)
})

Expected

The unit test should succeed

Actual

The unit test fails with this error:

    no schema with key or ref "https://json-schema.org/draft/2020-12/schema"

Not working with typescript test file

Hi,

When test case is in typescript, the toMatchSchema option does work.
I have imported and exteded the matchers as instructed.
Getting the following error -
Property 'toMatchSchema' does not exist on type 'JestMatchersShape<Matchers<void, any>, Matchers<Promise, any>>'.ts(2339)

Although, it works perfectly if I change my file back to plain javascript.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.