1 | var assert = require('assert');
|
---|
2 | var vows = require('vows');
|
---|
3 | var path = require('path');
|
---|
4 | var fs = require('fs');
|
---|
5 |
|
---|
6 | var validate = require('../lib/validate').validate;
|
---|
7 |
|
---|
8 |
|
---|
9 | var revision = 'draft-03';
|
---|
10 | var schemaRoot = path.join(__dirname, '..', revision);
|
---|
11 | var schemaNames = ['schema', 'hyper-schema', 'links', 'json-ref' ];
|
---|
12 | var schemas = {};
|
---|
13 |
|
---|
14 | schemaNames.forEach(function(name) {
|
---|
15 | var file = path.join(schemaRoot, name);
|
---|
16 | schemas[name] = loadSchema(file);
|
---|
17 | });
|
---|
18 |
|
---|
19 | schemaNames.forEach(function(name) {
|
---|
20 | var s, n = name+'-nsd', f = path.join(schemaRoot, name);
|
---|
21 | schemas[n] = loadSchema(f);
|
---|
22 | s = schemas[n];
|
---|
23 | delete s['$schema'];
|
---|
24 | });
|
---|
25 |
|
---|
26 | function loadSchema(path) {
|
---|
27 | var data = fs.readFileSync(path, 'utf-8');
|
---|
28 | var schema = JSON.parse(data);
|
---|
29 | return schema;
|
---|
30 | }
|
---|
31 |
|
---|
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');
|
---|
40 | }
|
---|
41 | }
|
---|
42 | }
|
---|
43 |
|
---|
44 | function assertValidates(doc, schema) {
|
---|
45 | var context = {};
|
---|
46 |
|
---|
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);
|
---|
55 | }
|
---|
56 | };
|
---|
57 |
|
---|
58 | return context;
|
---|
59 | }
|
---|
60 |
|
---|
61 | function assertSelfValidates(doc) {
|
---|
62 | var context = {};
|
---|
63 |
|
---|
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); }
|
---|
69 | };
|
---|
70 |
|
---|
71 | return context;
|
---|
72 | }
|
---|
73 |
|
---|
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'),
|
---|
78 |
|
---|
79 | 'Core self-validates': assertSelfValidates('schema'),
|
---|
80 | 'Core/Core': assertValidates('schema', 'schema'),
|
---|
81 |
|
---|
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'),
|
---|
86 |
|
---|
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'),
|
---|
91 |
|
---|
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')
|
---|
95 | }).export(module);
|
---|