source: trip-planner-front/node_modules/json-schema/test/tests.js@ b738035

Last change on this file since b738035 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 3.1 KB
RevLine 
[6a3a178]1var assert = require('assert');
2var vows = require('vows');
3var path = require('path');
4var fs = require('fs');
5
6var validate = require('../lib/validate').validate;
7
8
9var revision = 'draft-03';
10var schemaRoot = path.join(__dirname, '..', revision);
11var schemaNames = ['schema', 'hyper-schema', 'links', 'json-ref' ];
12var schemas = {};
13
14schemaNames.forEach(function(name) {
15 var file = path.join(schemaRoot, name);
16 schemas[name] = loadSchema(file);
17});
18
19schemaNames.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
26function loadSchema(path) {
27 var data = fs.readFileSync(path, 'utf-8');
28 var schema = JSON.parse(data);
29 return schema;
30}
31
32function 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
44function 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
61function 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
74var 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);
Note: See TracBrowser for help on using the repository browser.