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