Coder Social home page Coder Social logo

jest-json-schema's Introduction

jest-json-schema

npm version Build Status Mentioned in Awesome Jest

JSON schema matcher for jest

Want to get paid for your contributions to jest-json-schema? Send your resume to [email protected]

Matchers included

toMatchSchema(schema)

Validates that an object matches the given JSON schema

it('validates my json', () => {
  const schema = {
    properties: {
      hello: { type: 'string' },
    },
    required: ['hello'],
  };
  expect({ hello: 'world' }).toMatchSchema(schema);
});

Installation

$ npm install --save-dev jest-json-schema

Usage

In any test file:

import { matchers } from 'jest-json-schema';
expect.extend(matchers);

Or if you want it available for all test files then set it up the same way in a test framework script file

You can pass Ajv options using matchersWithOptions and passing it your options object. The only option passed by default is allErrors: true.

import { matchersWithOptions } from 'jest-json-schema';

const formats = {
  bcp47: /^[a-z]{2}-[A-Z]{2}$/,
}

expect.extend(matchersWithOptions({ formats }));

Additionally you can also use a callback to further configure and extend the Ajv instance used by the matchers:

import ajvKeywords from 'ajv-keywords';
import { matchersWithOptions } from 'jest-json-schema';

const formats = {
  bcp47: /^[a-z]{2}-[A-Z]{2}$/,
}

expect.extend(matchersWithOptions({ formats }, (ajv) => {
  // This uses the `ajv-keywords` library to add pre-made
  // custom keywords to the Ajv instance.
  ajvKeywords(ajv, ['typeof', 'instanceof']);
}));

You can also customize the Ajv class with the AjvClass option:

import Ajv2020 from 'ajv/dist/2020'
import { matchersWithOptions } from 'jest-json-schema';

expect.extend(matchersWithOptions({ AjvClass: Ajv2020 }));

Verbose errors

Ajv supports a verbose option flag which enables more information about individual errors. This extra information can mean that we can output to Jest more meaningful errors that can help the development process:

const { matchersWithOptions } = require('jest-json-schema');

expect.extend(matchersWithOptions({
  verbose: true
}));

test('check that property errors are outputted', () => {
  const schema = {
    $id: 'testSchema',
    type: 'object',
    properties: {
      name: {
        type: 'string',
      },
      dob: {
        type: 'string',
        format: 'date',
      },
    },
  };

  const invalidData = {
    name: null,
    dob: '02-29-2000',
  };

  expect(() => {
    expect(invalidData).toMatchSchema(schema)
  }).toThrowErrorMatchingInlineSnapshot(`
"expect(received).toMatchSchema(schema)

Received:
  .name should be string
    Received: <null>
    Path: testSchema#/properties/name/type
  .dob should match format \\"date\\"
    Received: <string> 02-29-2000
    Path: testSchema#/properties/dob/format
"
`);
});

Example using multiple schema files

If you organise your schemas into separate files and use refs which point to the various different schemas, it will be important to include those dependent schema files when extending Jest's expect handler, using the matchersWithOptions interface:

schemaA.json

{
  "$id": "schemaA",
  "$schema": "http://json-schema.org/draft-07/schema#",
  "description": "Example of a definition schema.",
  "definitions": {
    "testA": {
      "type": "number",
      "const": 1
    },
    "testB": {
      "type": ["null", "string"]
    }
  }
}

schemaB.json

{
  "$id": "schemaB",
  "$schema": "http://json-schema.org/draft-07/schema#",
  "description": "Example of a schema that references another schema.",
  "$ref": "schemaA#/definitions/testB"
}

schemaA.test.js

const { matchersWithOptions } = require('jest-json-schema');

// Local schema files are imported like normal. If you use TypeScript you
// will need to ensure `--resolveJsonModule` is enabled.
const schemaA = require('./schemaA.json');
const schemaB = require('./schemaB.json');

expect.extend(matchersWithOptions({
  // Loading in a schema which is comprised only of definitions,
  // which means specific test schemas need to be created.
  // This is good for testing specific conditions for definition schemas.
  schemas: [schemaA]
});

test('schemaA is valid', () => {
  expect(schemaA).toBeValidSchema();
});

test('using schemaA to build a test schema to test a specific definition', () => {
  // This is a test schema which references a definition in one of the
  // pre-loaded schemas. This can allow us to write tests for specific
  // definitions.
  const testSchema = {
    $ref: 'schemaA#/definitions/testA'
  };

  expect(testSchema).toBeValidSchema();

  // Valid
  expect(1).toMatchSchema(testSchema);

  // This example runs through a number of values that we know don't match
  // the schema, ensuring that any future changes to the schema will require
  // the test to be updated.
  ['1', true, false, null, [], {}].forEach(value => {
     expect(value).not.toMatchSchema(testSchema);
  });
});

test('using schemaB which already references a definition in schemaA', () => {
  expect(schemaB).toBeValidSchema();

  // Valid
  ['', '1', null].forEach(value => {
    expect(value).toMatchSchema(schemaB);
  });

  // Invalid
  ['1', true, false, [], {}].forEach(value => {
     expect(value).not.toMatchSchema(schemaB);
  });
});

TypeScript support

If you would like to use jest-json-schema library in your TypeScript project, remember to install type definitions from @types/jest-json-schema package.

npm install --save-dev @types/jest-json-schema

Or if yarn is your package manager of choice:

yarn add @types/jest-json-schema --dev

Contributing

We welcome Your interest in the American Express Open Source Community on Github. Any Contributor to any Open Source Project managed by the American Express Open Source Community must accept and sign an Agreement indicating agreement to the terms below. Except for the rights granted in this Agreement to American Express and to recipients of software distributed by American Express, You reserve all right, title, and interest, if any, in and to Your Contributions. Please fill out the Agreement.

Please feel free to open pull requests and see CONTRIBUTING.md to learn how to get started contributing.

License

Any contributions made under this project will be governed by the Apache License 2.0.

Code of Conduct

This project adheres to the American Express Community Guidelines. By participating, you are expected to honor these guidelines.

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

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

"$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"

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

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)

[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?

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

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.