source: trip-planner-front/node_modules/ajv-keywords/keywords/dynamicDefaults.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.0 KB
RevLine 
[6a3a178]1'use strict';
2
3var sequences = {};
4
5var DEFAULTS = {
6 timestamp: function() { return Date.now(); },
7 datetime: function() { return (new Date).toISOString(); },
8 date: function() { return (new Date).toISOString().slice(0, 10); },
9 time: function() { return (new Date).toISOString().slice(11); },
10 random: function() { return Math.random(); },
11 randomint: function (args) {
12 var limit = args && args.max || 2;
13 return function() { return Math.floor(Math.random() * limit); };
14 },
15 seq: function (args) {
16 var name = args && args.name || '';
17 sequences[name] = sequences[name] || 0;
18 return function() { return sequences[name]++; };
19 }
20};
21
22module.exports = function defFunc(ajv) {
23 defFunc.definition = {
24 compile: function (schema, parentSchema, it) {
25 var funcs = {};
26
27 for (var key in schema) {
28 var d = schema[key];
29 var func = getDefault(typeof d == 'string' ? d : d.func);
30 funcs[key] = func.length ? func(d.args) : func;
31 }
32
33 return it.opts.useDefaults && !it.compositeRule
34 ? assignDefaults
35 : noop;
36
37 function assignDefaults(data) {
38 for (var prop in schema){
39 if (data[prop] === undefined
40 || (it.opts.useDefaults == 'empty'
41 && (data[prop] === null || data[prop] === '')))
42 data[prop] = funcs[prop]();
43 }
44 return true;
45 }
46
47 function noop() { return true; }
48 },
49 DEFAULTS: DEFAULTS,
50 metaSchema: {
51 type: 'object',
52 additionalProperties: {
53 type: ['string', 'object'],
54 additionalProperties: false,
55 required: ['func', 'args'],
56 properties: {
57 func: { type: 'string' },
58 args: { type: 'object' }
59 }
60 }
61 }
62 };
63
64 ajv.addKeyword('dynamicDefaults', defFunc.definition);
65 return ajv;
66
67 function getDefault(d) {
68 var def = DEFAULTS[d];
69 if (def) return def;
70 throw new Error('invalid "dynamicDefaults" keyword property value: ' + d);
71 }
72};
Note: See TracBrowser for help on using the repository browser.