source: trip-planner-front/node_modules/ajv-keywords/keywords/select.js

Last change on this file was e29cc2e, checked in by Ema <ema_spirova@…>, 3 years ago

primeNG components

  • Property mode set to 100644
File size: 2.2 KB
RevLine 
[6a3a178]1'use strict';
2
3var util = require('./_util');
4
5module.exports = function defFunc(ajv) {
6 if (!ajv._opts.$data) {
7 console.warn('keyword select requires $data option');
8 return ajv;
9 }
10 var metaSchemaRef = util.metaSchemaRef(ajv);
11 var compiledCaseSchemas = [];
12
13 defFunc.definition = {
14 validate: function v(schema, data, parentSchema) {
15 if (parentSchema.selectCases === undefined)
16 throw new Error('keyword "selectCases" is absent');
17 var compiled = getCompiledSchemas(parentSchema, false);
18 var validate = compiled.cases[schema];
19 if (validate === undefined) validate = compiled.default;
20 if (typeof validate == 'boolean') return validate;
21 var valid = validate(data);
22 if (!valid) v.errors = validate.errors;
23 return valid;
24 },
25 $data: true,
26 metaSchema: { type: ['string', 'number', 'boolean', 'null'] }
27 };
28
29 ajv.addKeyword('select', defFunc.definition);
30 ajv.addKeyword('selectCases', {
31 compile: function (schemas, parentSchema) {
32 var compiled = getCompiledSchemas(parentSchema);
33 for (var value in schemas)
34 compiled.cases[value] = compileOrBoolean(schemas[value]);
35 return function() { return true; };
36 },
37 valid: true,
38 metaSchema: {
39 type: 'object',
40 additionalProperties: metaSchemaRef
41 }
42 });
43 ajv.addKeyword('selectDefault', {
44 compile: function (schema, parentSchema) {
45 var compiled = getCompiledSchemas(parentSchema);
46 compiled.default = compileOrBoolean(schema);
47 return function() { return true; };
48 },
49 valid: true,
50 metaSchema: metaSchemaRef
51 });
52 return ajv;
53
54
55 function getCompiledSchemas(parentSchema, create) {
56 var compiled;
57 compiledCaseSchemas.some(function (c) {
58 if (c.parentSchema === parentSchema) {
59 compiled = c;
60 return true;
61 }
62 });
63 if (!compiled && create !== false) {
64 compiled = {
65 parentSchema: parentSchema,
66 cases: {},
67 default: true
68 };
69 compiledCaseSchemas.push(compiled);
70 }
71 return compiled;
72 }
73
74 function compileOrBoolean(schema) {
75 return typeof schema == 'boolean'
76 ? schema
77 : ajv.compile(schema);
78 }
79};
Note: See TracBrowser for help on using the repository browser.