1 var assert = require('assert');
2 var vows = require('vows');
3 var path = require('path');
4 var fs = require('fs');
6 var validate = require('../lib/validate').validate;
9 var revision = 'draft-03';
10 var schemaRoot = path.join(__dirname, '..', revision);
11 var schemaNames = ['schema', 'hyper-schema', 'links', 'json-ref' ];
14 schemaNames.forEach(function(name) {
15 var file = path.join(schemaRoot, name);
16 schemas[name] = loadSchema(file);
19 schemaNames.forEach(function(name) {
20 var s, n = name+'-nsd', f = path.join(schemaRoot, name);
21 schemas[n] = loadSchema(f);
26 function loadSchema(path) {
27 var data = fs.readFileSync(path, 'utf-8');
28 var schema = JSON.parse(data);
32 function resultIsValid() {
33 return function(result) {
34 assert.isObject(result);
35 //assert.isBoolean(result.valid);
36 assert.equal(typeof(result.valid), 'boolean');
37 assert.isArray(result.errors);
38 for (var i = 0; i < result.errors.length; i++) {
39 assert.notEqual(result.errors[i], null, 'errors['+i+'] is null');
44 function assertValidates(doc, schema) {
47 context[': validate('+doc+', '+schema+')'] = {
48 topic: validate(schemas[doc], schemas[schema]),
49 'returns valid result': resultIsValid(),
50 'with valid=true': function(result) { assert.equal(result.valid, true); },
51 'and no errors': function(result) {
52 // XXX work-around for bug in vows: [null] chokes it
53 if (result.errors[0] == null) assert.fail('(errors contains null)');
54 assert.length(result.errors, 0);
61 function assertSelfValidates(doc) {
64 context[': validate('+doc+')'] = {
65 topic: validate(schemas[doc]),
66 'returns valid result': resultIsValid(),
67 'with valid=true': function(result) { assert.equal(result.valid, true); },
68 'and no errors': function(result) { assert.length(result.errors, 0); }
74 var suite = vows.describe('JSON Schema').addBatch({
75 'Core-NSD self-validates': assertSelfValidates('schema-nsd'),
76 'Core-NSD/Core-NSD': assertValidates('schema-nsd', 'schema-nsd'),
77 'Core-NSD/Core': assertValidates('schema-nsd', 'schema'),
79 'Core self-validates': assertSelfValidates('schema'),
80 'Core/Core': assertValidates('schema', 'schema'),
82 'Hyper-NSD self-validates': assertSelfValidates('hyper-schema-nsd'),
83 'Hyper self-validates': assertSelfValidates('hyper-schema'),
84 'Hyper/Hyper': assertValidates('hyper-schema', 'hyper-schema'),
85 'Hyper/Core': assertValidates('hyper-schema', 'schema'),
87 'Links-NSD self-validates': assertSelfValidates('links-nsd'),
88 'Links self-validates': assertSelfValidates('links'),
89 'Links/Hyper': assertValidates('links', 'hyper-schema'),
90 'Links/Core': assertValidates('links', 'schema'),
92 'Json-Ref self-validates': assertSelfValidates('json-ref'),
93 'Json-Ref/Hyper': assertValidates('json-ref', 'hyper-schema'),
94 'Json-Ref/Core': assertValidates('json-ref', 'schema')