source: trip-planner-front/node_modules/ajv-keywords/keywords/transform.js@ 76712b2

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

primeNG components

  • Property mode set to 100644
File size: 2.1 KB
Line 
1'use strict';
2
3module.exports = function defFunc (ajv) {
4 var transform = {
5 trimLeft: function (value) {
6 return value.replace(/^[\s]+/, '');
7 },
8 trimRight: function (value) {
9 return value.replace(/[\s]+$/, '');
10 },
11 trim: function (value) {
12 return value.trim();
13 },
14 toLowerCase: function (value) {
15 return value.toLowerCase();
16 },
17 toUpperCase: function (value) {
18 return value.toUpperCase();
19 },
20 toEnumCase: function (value, cfg) {
21 return cfg.hash[makeHashTableKey(value)] || value;
22 }
23 };
24
25 defFunc.definition = {
26 type: 'string',
27 errors: false,
28 modifying: true,
29 valid: true,
30 compile: function (schema, parentSchema) {
31 var cfg;
32
33 if (schema.indexOf('toEnumCase') !== -1) {
34 // build hash table to enum values
35 cfg = {hash: {}};
36
37 // requires `enum` in schema
38 if (!parentSchema.enum)
39 throw new Error('Missing enum. To use `transform:["toEnumCase"]`, `enum:[...]` is required.');
40 for (var i = parentSchema.enum.length; i--; i) {
41 var v = parentSchema.enum[i];
42 if (typeof v !== 'string') continue;
43 var k = makeHashTableKey(v);
44 // requires all `enum` values have unique keys
45 if (cfg.hash[k])
46 throw new Error('Invalid enum uniqueness. To use `transform:["toEnumCase"]`, all values must be unique when case insensitive.');
47 cfg.hash[k] = v;
48 }
49 }
50
51 return function (data, dataPath, object, key) {
52 // skip if value only
53 if (!object) return;
54
55 // apply transform in order provided
56 for (var j = 0, l = schema.length; j < l; j++)
57 data = transform[schema[j]](data, cfg);
58
59 object[key] = data;
60 };
61 },
62 metaSchema: {
63 type: 'array',
64 items: {
65 type: 'string',
66 enum: [
67 'trimLeft', 'trimRight', 'trim',
68 'toLowerCase', 'toUpperCase', 'toEnumCase'
69 ]
70 }
71 }
72 };
73
74 ajv.addKeyword('transform', defFunc.definition);
75 return ajv;
76
77 function makeHashTableKey (value) {
78 return value.toLowerCase();
79 }
80};
Note: See TracBrowser for help on using the repository browser.