Skip to content

Instantly share code, notes, and snippets.

@evdb
Last active December 15, 2015 03:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save evdb/5196782 to your computer and use it in GitHub Desktop.
Save evdb/5196782 to your computer and use it in GitHub Desktop.
correct case
"use strict";
var assert = require('assert'),
JSV = require("JSV").JSV;
function formatError (error) {
if (error) {
return error.message + ': ' + error.schemaUri;
} else {
return 'expected errors but got none';
}
};
var outerSchema = {
"$schema": "http://json-schema.org/draft-03/schema#",
"type": "object",
"id": "http://example.com/outer#",
"properties": {
"name": {
"description": "An alternate or former name",
"type": "string",
"required": true
},
"inners": {
"description": "an array of specific inner entries",
"type": "array",
"items": {
"$ref": "http://example.com/foo#"
}
},
}
};
var innerSchema = {
"$schema": "http://json-schema.org/draft-03/schema#",
"type": "object",
"id": "http://example.com/inner#",
"properties": {
"name": {
"description": "An inner's name",
"type": "string",
"required": true
},
}
};
var outerSchemaWithInnerEmbedded = {
"$schema": "http://json-schema.org/draft-03/schema#",
"type": "object",
"id": "http://example.com/outer#",
"properties": {
"name": {
"description": "An alternate or former name",
"type": "string",
"required": true
},
"inners": {
"description": "an array of specific inner entries",
"type": "array",
"items": innerSchema
},
}
};
var env = JSV.createEnvironment();
env.createSchema(innerSchema, null, innerSchema.id);
env.createSchema(outerSchema, null, outerSchema.id);
// env.createSchema(outerSchemaWithInnerEmbedded, null, outerSchemaWithInnerEmbedded.id);
describe( '$ref validation', function () {
describe("inner", function () {
it("has draft-03 schema", function () {
var schema3 = env.findSchema("http://json-schema.org/draft-03/schema#");
assert(schema3, "found draft 3 schema");
assert.equal(schema3.validate(schema3).errors.length, 0, "schema3.validate(schema3)");
});
it("correctly formed", function () {
var report = env.validate(
{ name: 'joey' },
innerSchema
);
assert.equal(report.errors.length, 0, formatError(report.errors[0]));
});
it("badly formed", function () {
var report = env.validate(
{ name: 123 },
innerSchema
);
assert.equal(report.errors.length, 1, formatError(report.errors[0]));
});
});
describe("outer", function () {
it("correctly formed", function () {
var report = env.validate(
{ name: 'joey',
inners: [ { name: 'bob' } ]
},
outerSchema
);
assert.equal(report.errors.length, 0, formatError(report.errors[0]));
});
it("badly formed", function () {
var report = env.validate(
{ name: 'joey', inners: [ { name: 123 } ] },
outerSchema
);
assert.equal(report.errors.length, 1, formatError(report.errors[0]));
});
});
describe("outer with inner embedded", function () {
it("correctly formed", function () {
var report = env.validate(
{ name: 'joey', inners: [ { name: 'bob' } ] },
outerSchemaWithInnerEmbedded
);
assert.equal(report.errors.length, 0, formatError(report.errors[0]));
});
it("badly formed", function () {
var report = env.validate(
{ name: 'joey', inners: [ { name: 123 } ] },
outerSchemaWithInnerEmbedded
);
assert.equal(report.errors.length, 1, formatError(report.errors[0]));
});
});
});
# mocha -R spec simple_test.js
$ref validation
inner
✓ has draft-03 schema (96ms)
✓ correctly formed
✓ badly formed
outer
1) correctly formed
✓ badly formed
outer with inner embedded
✓ correctly formed
✓ badly formed
✖ 1 of 7 tests failed:
1) $ref validation outer correctly formed:
AssertionError: Unknown schema reference: http://json-schema.org/draft-03/schema#
at Context.env.validate.name (/Users/evdb/mysociety/popit-api/src/schemas/simple_test.js:113:14)
at Test.Runnable.run (/usr/local/lib/node_modules/mocha/lib/runnable.js:184:32)
at Runner.runTest (/usr/local/lib/node_modules/mocha/lib/runner.js:300:10)
at Runner.runTests.next (/usr/local/lib/node_modules/mocha/lib/runner.js:346:12)
at next (/usr/local/lib/node_modules/mocha/lib/runner.js:228:14)
at Runner.hooks (/usr/local/lib/node_modules/mocha/lib/runner.js:237:7)
at next (/usr/local/lib/node_modules/mocha/lib/runner.js:185:23)
at Runner.hook (/usr/local/lib/node_modules/mocha/lib/runner.js:205:5)
at process.startup.processNextTick.process._tickCallback (node.js:244:9)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment