source: trip-planner-front/node_modules/ajv-keywords/keywords/deepRequired.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: 1.3 KB
RevLine 
[6a3a178]1'use strict';
2
3module.exports = function defFunc(ajv) {
4 defFunc.definition = {
5 type: 'object',
6 inline: function (it, keyword, schema) {
7 var expr = '';
8 for (var i=0; i<schema.length; i++) {
9 if (i) expr += ' && ';
10 expr += '(' + getData(schema[i], it.dataLevel) + ' !== undefined)';
11 }
12 return expr;
13 },
14 metaSchema: {
15 type: 'array',
16 items: {
17 type: 'string',
18 format: 'json-pointer'
19 }
20 }
21 };
22
23 ajv.addKeyword('deepRequired', defFunc.definition);
24 return ajv;
25};
26
27
28function getData(jsonPointer, lvl) {
29 var data = 'data' + (lvl || '');
30 if (!jsonPointer) return data;
31
32 var expr = data;
33 var segments = jsonPointer.split('/');
34 for (var i=1; i<segments.length; i++) {
35 var segment = segments[i];
36 data += getProperty(unescapeJsonPointer(segment));
37 expr += ' && ' + data;
38 }
39 return expr;
40}
41
42
43var IDENTIFIER = /^[a-z$_][a-z$_0-9]*$/i;
44var INTEGER = /^[0-9]+$/;
45var SINGLE_QUOTE = /'|\\/g;
46function getProperty(key) {
47 return INTEGER.test(key)
48 ? '[' + key + ']'
49 : IDENTIFIER.test(key)
50 ? '.' + key
51 : "['" + key.replace(SINGLE_QUOTE, '\\$&') + "']";
52}
53
54
55function unescapeJsonPointer(str) {
56 return str.replace(/~1/g, '/').replace(/~0/g, '~');
57}
Note: See TracBrowser for help on using the repository browser.