UNPKG

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