BlueGit

blue-star danique-bot

Files

danique-bot / node_modules / json-schema-traverse / index.js

index.js

Import template
timfa Tim · pushed by timfa · 4 years ago
c788ad20 History
index.js 2.0 KiB Download Raw
1'use strict';
2
3var traverse = module.exports = function (schema, opts, cb) {
4 if (typeof opts == 'function') {
5 cb = opts;
6 opts = {};
7 }
8 _traverse(opts, cb, schema, '', schema);
9};
10
11
12traverse.keywords = {
13 additionalItems: true,
14 items: true,
15 contains: true,
16 additionalProperties: true,
17 propertyNames: true,
18 not: true
19};
20
21traverse.arrayKeywords = {
22 items: true,
23 allOf: true,
24 anyOf: true,
25 oneOf: true
26};
27
28traverse.propsKeywords = {
29 definitions: true,
30 properties: true,
31 patternProperties: true,
32 dependencies: true
33};
34
35traverse.skipKeywords = {
36 enum: true,
37 const: true,
38 required: true,
39 maximum: true,
40 minimum: true,
41 exclusiveMaximum: true,
42 exclusiveMinimum: true,
43 multipleOf: true,
44 maxLength: true,
45 minLength: true,
46 pattern: true,
47 format: true,
48 maxItems: true,
49 minItems: true,
50 uniqueItems: true,
51 maxProperties: true,
52 minProperties: true
53};
54
55
56function _traverse(opts, cb, schema, jsonPtr, rootSchema, parentJsonPtr, parentKeyword, parentSchema, keyIndex) {
57 if (schema && typeof schema == 'object' && !Array.isArray(schema)) {
58 cb(schema, jsonPtr, rootSchema, parentJsonPtr, parentKeyword, parentSchema, keyIndex);
59 for (var key in schema) {
60 var sch = schema[key];
61 if (Array.isArray(sch)) {
62 if (key in traverse.arrayKeywords) {
63 for (var i=0; i<sch.length; i++)
64 _traverse(opts, cb, sch[i], jsonPtr + '/' + key + '/' + i, rootSchema, jsonPtr, key, schema, i);
65 }
66 } else if (key in traverse.propsKeywords) {
67 if (sch && typeof sch == 'object') {
68 for (var prop in sch)
69 _traverse(opts, cb, sch[prop], jsonPtr + '/' + key + '/' + escapeJsonPtr(prop), rootSchema, jsonPtr, key, schema, prop);
70 }
71 } else if (key in traverse.keywords || (opts.allKeys && !(key in traverse.skipKeywords))) {
72 _traverse(opts, cb, sch, jsonPtr + '/' + key, rootSchema, jsonPtr, key, schema);
73 }
74 }
75 }
76}
77
78
79function escapeJsonPtr(str) {
80 return str.replace(/~/g, '~0').replace(/\//g, '~1');
81}