if.js 618 B

123456789101112131415161718192021
  1. 'use strict';
  2. module.exports = function defFunc(ajv) {
  3. if (!ajv._opts.v5) console.warn('keywords if/then/else require v5 option');
  4. defFunc.definition = {
  5. macro: function (schema, parentSchema) {
  6. if (parentSchema.then === undefined)
  7. throw new Error('keyword "then" is absent');
  8. var cases = [ { 'if': schema, 'then': parentSchema.then } ];
  9. if (parentSchema.else !== undefined)
  10. cases[1] = { 'then': parentSchema.else };
  11. return { switch: cases };
  12. }
  13. };
  14. ajv.addKeyword('if', defFunc.definition);
  15. ajv.addKeyword('then');
  16. ajv.addKeyword('else');
  17. return ajv;
  18. };