Blame view

node_modules/ajv-keywords/keywords/if.js 600 Bytes
ce4c83ff   wxy   初始提交
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
  'use strict';
  
  module.exports = function defFunc(ajv) {
    if (!ajv.RULES.keywords.switch) require('./switch')(ajv);
  
    defFunc.definition = {
      macro: function (schema, parentSchema) {
        if (parentSchema.then === undefined)
          throw new Error('keyword "then" is absent');
        var cases = [ { 'if': schema, 'then': parentSchema.then } ];
        if (parentSchema.else !== undefined)
          cases[1] = { 'then': parentSchema.else };
        return { switch: cases };
      }
    };
  
    ajv.addKeyword('if', defFunc.definition);
    ajv.addKeyword('then');
    ajv.addKeyword('else');
    return ajv;
  };