tests.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. var assert = require('assert');
  2. var vows = require('vows');
  3. var path = require('path');
  4. var fs = require('fs');
  5. var validate = require('../lib/validate').validate;
  6. var revision = 'draft-03';
  7. var schemaRoot = path.join(__dirname, '..', revision);
  8. var schemaNames = ['schema', 'hyper-schema', 'links', 'json-ref' ];
  9. var schemas = {};
  10. schemaNames.forEach(function(name) {
  11. var file = path.join(schemaRoot, name);
  12. schemas[name] = loadSchema(file);
  13. });
  14. schemaNames.forEach(function(name) {
  15. var s, n = name+'-nsd', f = path.join(schemaRoot, name);
  16. schemas[n] = loadSchema(f);
  17. s = schemas[n];
  18. delete s['$schema'];
  19. });
  20. function loadSchema(path) {
  21. var data = fs.readFileSync(path, 'utf-8');
  22. var schema = JSON.parse(data);
  23. return schema;
  24. }
  25. function resultIsValid() {
  26. return function(result) {
  27. assert.isObject(result);
  28. //assert.isBoolean(result.valid);
  29. assert.equal(typeof(result.valid), 'boolean');
  30. assert.isArray(result.errors);
  31. for (var i = 0; i < result.errors.length; i++) {
  32. assert.notEqual(result.errors[i], null, 'errors['+i+'] is null');
  33. }
  34. }
  35. }
  36. function assertValidates(doc, schema) {
  37. var context = {};
  38. context[': validate('+doc+', '+schema+')'] = {
  39. topic: validate(schemas[doc], schemas[schema]),
  40. 'returns valid result': resultIsValid(),
  41. 'with valid=true': function(result) { assert.equal(result.valid, true); },
  42. 'and no errors': function(result) {
  43. // XXX work-around for bug in vows: [null] chokes it
  44. if (result.errors[0] == null) assert.fail('(errors contains null)');
  45. assert.length(result.errors, 0);
  46. }
  47. };
  48. return context;
  49. }
  50. function assertSelfValidates(doc) {
  51. var context = {};
  52. context[': validate('+doc+')'] = {
  53. topic: validate(schemas[doc]),
  54. 'returns valid result': resultIsValid(),
  55. 'with valid=true': function(result) { assert.equal(result.valid, true); },
  56. 'and no errors': function(result) { assert.length(result.errors, 0); }
  57. };
  58. return context;
  59. }
  60. var suite = vows.describe('JSON Schema').addBatch({
  61. 'Core-NSD self-validates': assertSelfValidates('schema-nsd'),
  62. 'Core-NSD/Core-NSD': assertValidates('schema-nsd', 'schema-nsd'),
  63. 'Core-NSD/Core': assertValidates('schema-nsd', 'schema'),
  64. 'Core self-validates': assertSelfValidates('schema'),
  65. 'Core/Core': assertValidates('schema', 'schema'),
  66. 'Hyper-NSD self-validates': assertSelfValidates('hyper-schema-nsd'),
  67. 'Hyper self-validates': assertSelfValidates('hyper-schema'),
  68. 'Hyper/Hyper': assertValidates('hyper-schema', 'hyper-schema'),
  69. 'Hyper/Core': assertValidates('hyper-schema', 'schema'),
  70. 'Links-NSD self-validates': assertSelfValidates('links-nsd'),
  71. 'Links self-validates': assertSelfValidates('links'),
  72. 'Links/Hyper': assertValidates('links', 'hyper-schema'),
  73. 'Links/Core': assertValidates('links', 'schema'),
  74. 'Json-Ref self-validates': assertSelfValidates('json-ref'),
  75. 'Json-Ref/Hyper': assertValidates('json-ref', 'hyper-schema'),
  76. 'Json-Ref/Core': assertValidates('json-ref', 'schema')
  77. }).export(module);