package
Testing JSON schemas against sample data
npm install json-schema-testGitHub-flavored package documentation.
Across all versions.
Download trend unavailable.
Runtime metadata.
Tagged topics.
Testing JSON schemas against sample data
npm install json-schema-testThe module is designed to be used inside mocha test (global describe etc. should be avaliable).
Example usage:
var jsonSchemaTest = require('json-schema-test');
jsonSchemaTest([ ajv, tv4 ], {
description: 'My schema tests',
suites: {
'JSON-Schema tests draft4': './JSON-Schema-Test-Suite/tests/draft4/{**/,}*.json',
'Advanced schema tests': './tests/{**/,}*.json'
},
// async: true,
// asyncValid: true, // or 'data', deafult is true
afterEach: afterEachFunc,
// afterError: afterErrorFunc,
log: false,
only: ONLY_FILES,
skip: SKIP_FILES,
cwd: __dirname,
hideFolder: 'draft4/',
timeout: 10000,
assert: chai.assert
});
function afterEachFunc(result) {
The library runs tests defined in JSON consructed using the same format as the tests in JSON-Schema-Tests-Suite.
Each test file should have this format:
[
{ // schema to be tested, there can be multiple schemas in each file
// only: true, // only this schema will be tested
// skip: true, // skip this schema
description: 'The description is shown in the test report',
// schema object or reference if the validator supports it
schema: { ... } // or 'http://schema.example.com/schema.json#',
// schemas: [] // array of schema objects or refs (schema property won't be used)
tests: [ // test cases for the schema
{
// only: true, // only this data sample will be tested
// skip: true, // skip this data sample
description: 'valid data', // shown in report
data: { ... }, // data object (can be any type)
// dataFile: './valid_data.json', // or the relative path to the file
valid: true // whether the data is valid for schema
},
{
description: 'invalid data'
The schema for the test file is in test_file_schema.json.
jsonSchemaTest(valdator, options)Validator instance to be used or array of validator instances (in which case each test will run with each instance).
Validator should have validate(schema, data) method and after validation should have errors property (array in case of failure, empty array or null in case of success).
If validator instance has different API you can use json-schema-consolidate as adapter (or just use some simple adaptor function).
name and path properties) of of actual tests (objects with name and test properties) can be passed.true if validate function is asynchronous and returns the Promise. The promise should resolve with true or reject with the exception that has .errors property (array of errors). The promise may also reject with any error (and if it is the expected result, the test case in json file should specify error property with the message instead of valid property). That is the asynchronous api of Ajv - use an adaptor in case you are using some validator with a different api. It's safe to use async option if some results are synchronous, the results will simply be wrapped in the promise.true will be expected.mailonline
table1.mailonline@gmail.com
false to prevent default error logging.true to test only these suites.json extension).true to skip all suites__dirname to pass paths relative to the module in suites option.assert from nodejs will be used that can be undesired when used in the browser.async option is true.Estimated unpacked size across 8 packages.
6.9 MB
Includes dependencies.