Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

chai-json-schema

Package Overview
Dependencies
Maintainers
3
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

chai-json-schema

Chai plugin for JSON Schema v4

  • 1.5.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
226K
increased by1.54%
Maintainers
3
Weekly downloads
 
Created

What is chai-json-schema?

The chai-json-schema npm package is a Chai plugin that provides assertions for validating JSON objects against JSON schemas. It allows you to write tests that ensure your JSON data conforms to a specified schema, making it easier to validate the structure and content of JSON responses in your tests.

What are chai-json-schema's main functionalities?

Basic Schema Validation

This feature allows you to validate a JSON object against a defined schema. In this example, the `validPerson` object is validated against the `schema` to ensure it has the required properties and types.

const chai = require('chai');
const chaiJsonSchema = require('chai-json-schema');
chai.use(chaiJsonSchema);
const expect = chai.expect;

const schema = {
  title: 'Person',
  type: 'object',
  required: ['name', 'age'],
  properties: {
    name: {
      type: 'string'
    },
    age: {
      type: 'integer'
    }
  }
};

const validPerson = {
  name: 'John Doe',
  age: 30
};

expect(validPerson).to.be.jsonSchema(schema);

Nested Schema Validation

This feature allows you to validate JSON objects with nested schemas. In this example, the `validPerson` object includes an `address` property that is validated against the `addressSchema`.

const chai = require('chai');
const chaiJsonSchema = require('chai-json-schema');
chai.use(chaiJsonSchema);
const expect = chai.expect;

const addressSchema = {
  title: 'Address',
  type: 'object',
  required: ['street', 'city'],
  properties: {
    street: {
      type: 'string'
    },
    city: {
      type: 'string'
    }
  }
};

const personSchema = {
  title: 'Person',
  type: 'object',
  required: ['name', 'age', 'address'],
  properties: {
    name: {
      type: 'string'
    },
    age: {
      type: 'integer'
    },
    address: {
      $ref: 'addressSchema'
    }
  }
};

const validPerson = {
  name: 'John Doe',
  age: 30,
  address: {
    street: '123 Main St',
    city: 'Anytown'
  }
};

chai.tv4.addSchema('addressSchema', addressSchema);
expect(validPerson).to.be.jsonSchema(personSchema);

Array Schema Validation

This feature allows you to validate arrays of JSON objects against a schema. In this example, the `validPeople` array is validated to ensure each object in the array conforms to the `personSchema`.

const chai = require('chai');
const chaiJsonSchema = require('chai-json-schema');
chai.use(chaiJsonSchema);
const expect = chai.expect;

const personSchema = {
  title: 'Person',
  type: 'object',
  required: ['name', 'age'],
  properties: {
    name: {
      type: 'string'
    },
    age: {
      type: 'integer'
    }
  }
};

const peopleSchema = {
  title: 'People',
  type: 'array',
  items: personSchema
};

const validPeople = [
  { name: 'John Doe', age: 30 },
  { name: 'Jane Doe', age: 25 }
];

expect(validPeople).to.be.jsonSchema(peopleSchema);

Other packages similar to chai-json-schema

Keywords

FAQs

Package last updated on 13 May 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc