async_schemas.spec.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. 'use strict';
  2. var jsonSchemaTest = require('json-schema-test')
  3. , Promise = require('./promise')
  4. , getAjvInstances = require('./ajv_async_instances')
  5. , Ajv = require('./ajv')
  6. , suite = require('./browser_test_suite')
  7. , after = require('./after_test');
  8. var instances = getAjvInstances({ $data: true });
  9. instances.forEach(addAsyncFormatsAndKeywords);
  10. jsonSchemaTest(instances, {
  11. description: 'asynchronous schemas tests of ' + instances.length + ' ajv instances with different options',
  12. suites: {
  13. 'async schemas':
  14. typeof window == 'object'
  15. ? suite(require('./async/{**/,}*.json', {mode: 'list'}))
  16. : './async/{**/,}*.json'
  17. },
  18. async: true,
  19. asyncValid: 'data',
  20. assert: require('./chai').assert,
  21. Promise: Promise,
  22. afterError: after.error,
  23. // afterEach: after.each,
  24. cwd: __dirname,
  25. hideFolder: 'async/',
  26. timeout: 90000
  27. });
  28. function addAsyncFormatsAndKeywords (ajv) {
  29. ajv.addFormat('english_word', {
  30. async: true,
  31. validate: checkWordOnServer
  32. });
  33. ajv.addKeyword('idExists', {
  34. async: true,
  35. type: 'number',
  36. validate: checkIdExists,
  37. errors: false
  38. });
  39. ajv.addKeyword('idExistsWithError', {
  40. async: true,
  41. type: 'number',
  42. validate: checkIdExistsWithError,
  43. errors: true
  44. });
  45. ajv.addKeyword('idExistsCompiled', {
  46. async: true,
  47. type: 'number',
  48. compile: compileCheckIdExists
  49. });
  50. }
  51. function checkWordOnServer(str) {
  52. return str == 'tomorrow' ? Promise.resolve(true)
  53. : str == 'manana' ? Promise.resolve(false)
  54. : Promise.reject(new Error('unknown word'));
  55. }
  56. function checkIdExists(schema, data) {
  57. switch (schema.table) {
  58. case 'users': return check([1, 5, 8]);
  59. case 'posts': return check([21, 25, 28]);
  60. default: throw new Error('no such table');
  61. }
  62. function check(IDs) {
  63. return Promise.resolve(IDs.indexOf(data) >= 0);
  64. }
  65. }
  66. function checkIdExistsWithError(schema, data) {
  67. var table = schema.table;
  68. switch (table) {
  69. case 'users': return check(table, [1, 5, 8]);
  70. case 'posts': return check(table, [21, 25, 28]);
  71. default: throw new Error('no such table');
  72. }
  73. function check(_table, IDs) {
  74. if (IDs.indexOf(data) >= 0)
  75. return Promise.resolve(true);
  76. var error = {
  77. keyword: 'idExistsWithError',
  78. message: 'id not found in table ' + _table
  79. };
  80. return Promise.reject(new Ajv.ValidationError([error]));
  81. }
  82. }
  83. function compileCheckIdExists(schema) {
  84. switch (schema.table) {
  85. case 'users': return compileCheck([1, 5, 8]);
  86. case 'posts': return compileCheck([21, 25, 28]);
  87. default: throw new Error('no such table');
  88. }
  89. function compileCheck(IDs) {
  90. return function (data) {
  91. return Promise.resolve(IDs.indexOf(data) >= 0);
  92. };
  93. }
  94. }