@atomiq/json-schema-test-suite
Advanced tools
Comparing version 0.0.7 to 0.0.10
10
index.js
@@ -35,2 +35,5 @@ var _ = require('lodash'), | ||
/** | ||
* convenience filter for required tests | ||
*/ | ||
function requiredOnlyFilter(file, parent, optional) { | ||
@@ -40,2 +43,5 @@ return !optional; | ||
/** | ||
* convenience filter for optional tests | ||
*/ | ||
function optionalOnlyFilter(file, parent, optional) { | ||
@@ -52,3 +58,4 @@ return optional; | ||
* see exported requiredOnlyFilter | ||
* @param draft 'draft3' | 'draft4' (default) | ||
* @param options an object with the following properties | ||
* draft: 'draft3' | 'draft4' (default) | ||
* @returns [] | ||
@@ -149,4 +156,3 @@ */ | ||
return tests; | ||
} | ||
{ | ||
"name": "@atomiq/json-schema-test-suite", | ||
"version": "0.0.7", | ||
"description": "Exposes JSON Schema Test Suite as a Node.js package, so don't need to add as a git submodule.", | ||
"version": "0.0.10", | ||
"description": "Exposes JSON Schema Test Suite as a Node.js package", | ||
"main": "index.js", | ||
@@ -22,3 +22,3 @@ "directories": { | ||
], | ||
"author": "Tony Pujals <tony@subfuzion.com> (http://twitter.com/subfuzion)", | ||
"author": "Tony Pujals (http://twitter.com/subfuzion)", | ||
"license": "https://raw.githubusercontent.com/json-schema/json-schema-test-suite/develop/LICENSE", | ||
@@ -25,0 +25,0 @@ "bugs": { |
192
README.md
@@ -1,180 +0,10 @@ | ||
Node.js fork of JSON Schema Test Suite | ||
====================================== | ||
JSON Schema Test Suite [![Build Status](https://travis-ci.org/json-schema/JSON-Schema-Test-Suite.png?branch=develop)](https://travis-ci.org/json-schema/JSON-Schema-Test-Suite) | ||
====================== | ||
This is a fork of JSON Schema Test Suite for Node.js validator development. | ||
** This is the `node` branch of JSON Schema Test Suite.** | ||
The JSON Schema Test Suite is meant to be a language agnostic test suite for testing JSON Schema validation libraries. This fork makes the test suite available as an [npm package](https://www.npmjs.com/package/@atomiq/json-schema-test-suite) for use with Node.js. | ||
Node-specific support is maintained on GitHub on the [node branch](https://github.com/json-schema/JSON-Schema-Test-Suite/tree/node). | ||
See [NODE-README.md](https://github.com/json-schema/JSON-Schema-Test-Suite/blob/node/NODE-README.md) | ||
for more information on using this package. | ||
npm install @atomiq/json-schema-test-suite | ||
When pulling the source from GitHub, make sure to checkout the `node` branch for taking advantage of the node features in this fork. The master and develop branches track the [upstream repo](https://github.com/json-schema/JSON-Schema-Test-Suite/). It is current with the latest commit to `7511038dcb4f4b430fa2a929184da70b670bdd35` (June 17, 2015) on the upstream `develop` branch. | ||
### Usage: | ||
There are a number of ways of loading the tests: | ||
var testSuite = require('@atomiq/json-schema-test-suite'); | ||
// this will load all (required and optional) draft4 tests | ||
var 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'` or `'draft4'` | ||
var tests = testSuite.loadSync(filter, draft); | ||
// convenience functions: | ||
// The following take an optional `filter` as described previously (undefined will load all tests) | ||
var draft3 = testSuite.draft3(); | ||
var draft4 = testSuite.draft4(); | ||
// The following take an optional `draft` argument (defaults to 'draft4') | ||
var all = testSuite.loadAllSync(); | ||
var required = testSuite.loadRequiredSync(); | ||
var 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 | ||
path: '/full/path/to/JSON-Schema-Test-Suite/tests/draft4/additionalItems.json', | ||
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 | ||
``` | ||
var tv4 = require('tv4'); | ||
var tv4Factory = function (schema, options) { | ||
return { | ||
validate: function (json) { | ||
try { | ||
var valid = tv4.validate(json, schema); | ||
return valid ? { valid: true } : { valid: false, errors: [ tv4.error ] }; | ||
} catch (err) { | ||
return { valid: false, errors: [err.message] }; | ||
} | ||
} | ||
}; | ||
}; | ||
``` | ||
#### ZSchema | ||
``` | ||
var ZSchema = require('z-schema'); | ||
var zschemaFactory = function (schema, options) { | ||
var zschema = new ZSchema(options); | ||
return { | ||
validate: function (json) { | ||
try { | ||
var 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. | ||
``` | ||
var testSuite = require('@atomiq/json-schema-test-suite'); | ||
var factory = require('YOUR-FACTORY'); | ||
var options = { ... }; | ||
var tests = testSuite.testSync(factory, options); | ||
``` | ||
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 | ||
} | ||
} | ||
] | ||
} | ||
``` | ||
### Unit Tests | ||
You can run `npm test` from a clone of the repo or browse the unit test source [here](https://github.com/atomiqio/JSON-Schema-Test-Suite/blob/node/test/test.js) for examples using both [tv4](https://github.com/geraintluff/tv4) and [z-schema](https://github.com/zaggino/z-schema). | ||
--- | ||
JSON Schema Test Suite [![Build Status](https://travis-ci.org/json-schema/JSON-Schema-Test-Suite.png?branch=develop)](https://travis-ci.org/json-schema/JSON-Schema-Test-Suite) | ||
====================== | ||
This repository contains a set of JSON objects that implementors of JSON Schema | ||
@@ -265,3 +95,3 @@ validation libraries can use to test their validators. | ||
### Javascript ### | ||
### JavaScript ### | ||
@@ -283,2 +113,10 @@ * [json-schema-benchmark](https://github.com/Muscula/json-schema-benchmark) | ||
### Node.js ### | ||
The JSON Schema Test Suite is also available as an | ||
[npm](https://www.npmjs.com/package/json-schema-test-suite) package. | ||
Node-specific support is maintained on the [node branch](https://github.com/json-schema/JSON-Schema-Test-Suite/tree/node). | ||
See [NODE-README.md](https://github.com/json-schema/JSON-Schema-Test-Suite/blob/node/NODE-README.md) | ||
for more information. | ||
### .NET ### | ||
@@ -285,0 +123,0 @@ |
112
test/test.js
@@ -107,53 +107,28 @@ var assert = require('assert'), | ||
validatorResults.push(validatorResult); | ||
describe(draft, function () { | ||
var tests = testsuite.testSync(tv4Factory, {}, void 0, draft); | ||
tests.forEach(function (test) { | ||
describe(test.name, function () { | ||
test.schemas.forEach(function (schema) { | ||
describe(schema.description, function () { | ||
schema.tests.forEach(function (testCase) { | ||
it(testCase.description, function () { | ||
var result = testCase.result; | ||
if (result.valid === testCase.valid) { | ||
validatorResult.results.pass++; | ||
} else { | ||
validatorResult.results.fail++; | ||
} | ||
assert.equal(result.valid, testCase.valid); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}) | ||
}); | ||
}); | ||
runTests(tv4Factory, draft, validatorResult); | ||
}); | ||
}); | ||
describe('z-schema validator tests', function () { | ||
var ZSchema = require('z-schema'); | ||
var ZSchema = require('z-schema'); | ||
var zschemaFactory = function (schema, options) { | ||
var zschema = new ZSchema(options); | ||
var zschemaFactory = function (schema, options) { | ||
var zschema = new ZSchema(options); | ||
if (typeof schema == 'string') { | ||
schema = JSON.parse(text); | ||
} | ||
return { | ||
validate: function (json) { | ||
try { | ||
var valid = zschema.validate(json, schema); | ||
return valid ? { valid: true } : { valid: false, errors: zschema.getLastErrors() }; | ||
} catch (err) { | ||
return { valid: false, errors: [err.message] }; | ||
if (typeof schema == 'string') { | ||
schema = JSON.parse(text); | ||
} | ||
} | ||
}; | ||
}; | ||
return { | ||
validate: function (json) { | ||
try { | ||
var valid = zschema.validate(json, schema); | ||
return valid ? { valid: true } : { valid: false, errors: zschema.getLastErrors() }; | ||
} catch (err) { | ||
return { valid: false, errors: [err.message] }; | ||
} | ||
} | ||
}; | ||
}; | ||
// create a test suite for each draft | ||
@@ -163,31 +138,34 @@ drafts.forEach(function (draft) { | ||
validatorResults.push(validatorResult); | ||
runTests(zschemaFactory, draft, validatorResult); | ||
}); | ||
}); | ||
}); | ||
describe(draft, function () { | ||
function runTests(factory, draft, validatorResult) { | ||
describe(draft, function () { | ||
var tests = testsuite.testSync(zschemaFactory, {}, void 0, draft); | ||
tests.forEach(function (test) { | ||
describe(test.name, function () { | ||
test.schemas.forEach(function (schema) { | ||
describe(schema.description, function () { | ||
schema.tests.forEach(function (testCase) { | ||
it(testCase.description, function () { | ||
var result = testCase.result; | ||
if (result.valid === testCase.valid) { | ||
validatorResult.results.pass++; | ||
} else { | ||
validatorResult.results.fail++; | ||
} | ||
var tests = testsuite.testSync(factory, {}, void 0, draft); | ||
tests.forEach(function (test) { | ||
describe(test.name, function () { | ||
test.schemas.forEach(function (schema) { | ||
describe(schema.description, function () { | ||
schema.tests.forEach(function (testCase) { | ||
it(testCase.description, function () { | ||
var result = testCase.result; | ||
if (result.valid === testCase.valid) { | ||
validatorResult.results.pass++; | ||
} else { | ||
validatorResult.results.fail++; | ||
} | ||
assert.equal(result.valid, testCase.valid); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}) | ||
}); | ||
}); | ||
assert.equal(result.valid, testCase.valid); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}) | ||
}); | ||
}); | ||
}); | ||
} | ||
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
162278
70
4936
155
1