npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@json-schema-org/tests

v2.0.0

Published

A set of JSON objects that implementors of JSON Schema validation libraries can use to test their validators

Downloads

209

Readme

@json-schema-org/tests

NPM / node.js-specific support for the JSON Schema test suite

Build Status

The JSON Schema Test Suite is meant to be a language agnostic test suite for testing JSON Schema validation libraries. It is generally added to projects as a git submodule. However, to simplify things for Node.js developers, the test suite has also been made available as an npm package.

npm install @json-schema-org/tests

Usage:

There are a number of ways to load tests from the suite:

const testSuite = require('@json-schema-org/tests');

// this will load all (required and optional) draft6 tests
const tests = testSuite.loadSync();

// optional `filter` is a function that takes 3 arguments (filename, parent, optional)
// and returns true if the test should be included. The optional argument is true
// for all files under the `<draft>/optional` directory.
// optional `draft` should be either `'draft3'`, `'draft4'` or `'draft6'`

const tests = testSuite.loadSync(filter, draft);

// convenience functions:

// The following take an optional `filter` as described previously (undefined will load all tests)
const draft3 = testSuite.draft3();
const draft4 = testSuite.draft4();
const draft6 = testSuite.draft6();

// The following take an optional `draft` argument (defaults to 'draft6')
const all = testSuite.loadAllSync();
const required = testSuite.loadRequiredSync();
const optional = testSuite.loadOptionalSync();

The return value of these functions is an array of objects that correspond to each file under tests/<draft> that passed the filter (the default is all, so the array will also include all the optional files).

Each object has the following structure (using tests/draft4/additionalItems.json as an example):

{
  name:    'additionalItems',
  file:     'additionalItems.json',
  optional: false,  // true if a file under the optional directory
  schemas:  []
}

The schemas property contains the array of objects loaded from the test file. Each object consists of a schema and description, along with a number of tests used for validation. Using the first schema object in the array from tests/draft4/additionalItems.json as an example:

{
  description: 'additionalItems as schema',
  schema: {
    items: [{}],
    additionalItems: { type: "integer" }
  },
  tests: [
    {
      description: "additional items match schema",
      data: [ null, 2, 3, 4 ],
      valid: true
    },
    {
      description: "additional items do not match schema",
      data: [ null, 2, 3, "foo" ],
      valid: false
    }
  ]
}

Testing a JSON Validator

You can apply a validator against all the tests. You need to create a validator factory function that takes a JSON schema and an options argument, and returns an object with a validate method. The validate function should take a JSON object to be validated against the schema. It should return an object with a valid property set to true or false, and if not valid, an errors property that is an array of one or more validation errors.

The following are examples of Tiny Validator (tv4) and z-schema validator factories used by the unit test.

tv4

const tv4 = require('tv4');

const tv4Factory = function (schema, options) {
  return {
    validate: function (json) {
      try {
        const valid = tv4.validate(json, schema);
        return valid ? { valid: true } : { valid: false, errors: [ tv4.error ] };
      } catch (err) {
        return { valid: false, errors: [err.message] };
      }
    }
  };
};

ZSchema

const ZSchema = require('z-schema');

const zschemaFactory = function (schema, options) {
  const zschema = new ZSchema(options);

  return {
    validate: function (json) {
      try {
        const valid = zschema.validate(json, schema);
        return valid ? { valid: true } : { valid: false, errors: zschema.getLastErrors() };
      } catch (err) {
        return { valid: false, errors: [err.message] };
      }
    }
  };
};

Testing the Validator

Using a validator factory as described above, you can test it as follows.

const testSuite = require('json-schema-test-suite');

const tests = testSuite.testSync(factory);

The tests return value is as described previously in the Usage section, with an additional property for each test object that corresponds to the test result:

{
  description: 'additionalItems as schema',
  schema: {
    items: [{}],
    additionalItems: { type: "integer" }
  },
  tests: [
    {
      description: "additional items match schema",
      data: [ null, 2, 3, 4 ],
      valid: true,
      result: {
        valid: false,
        errors: [ ... ]
      }
    },
    {
      description: "additional items do not match schema",
      data: [ null, 2, 3, "foo" ],
      valid: false,
      result: {
        true
      }
    }
  ]
}

Tests

You can run the tests by doing:

npm test