Projekt

Obecné

Profil

Stáhnout (2.32 KB) Statistiky
| Větev: | Revize:
1
'use strict';
2

    
3
var traverse = module.exports = function (schema, opts, cb) {
4
  // Legacy support for v0.3.1 and earlier.
5
  if (typeof opts == 'function') {
6
    cb = opts;
7
    opts = {};
8
  }
9

    
10
  cb = opts.cb || cb;
11
  var pre = (typeof cb == 'function') ? cb : cb.pre || function() {};
12
  var post = cb.post || function() {};
13

    
14
  _traverse(opts, pre, post, schema, '', schema);
15
};
16

    
17

    
18
traverse.keywords = {
19
  additionalItems: true,
20
  items: true,
21
  contains: true,
22
  additionalProperties: true,
23
  propertyNames: true,
24
  not: true
25
};
26

    
27
traverse.arrayKeywords = {
28
  items: true,
29
  allOf: true,
30
  anyOf: true,
31
  oneOf: true
32
};
33

    
34
traverse.propsKeywords = {
35
  definitions: true,
36
  properties: true,
37
  patternProperties: true,
38
  dependencies: true
39
};
40

    
41
traverse.skipKeywords = {
42
  default: true,
43
  enum: true,
44
  const: true,
45
  required: true,
46
  maximum: true,
47
  minimum: true,
48
  exclusiveMaximum: true,
49
  exclusiveMinimum: true,
50
  multipleOf: true,
51
  maxLength: true,
52
  minLength: true,
53
  pattern: true,
54
  format: true,
55
  maxItems: true,
56
  minItems: true,
57
  uniqueItems: true,
58
  maxProperties: true,
59
  minProperties: true
60
};
61

    
62

    
63
function _traverse(opts, pre, post, schema, jsonPtr, rootSchema, parentJsonPtr, parentKeyword, parentSchema, keyIndex) {
64
  if (schema && typeof schema == 'object' && !Array.isArray(schema)) {
65
    pre(schema, jsonPtr, rootSchema, parentJsonPtr, parentKeyword, parentSchema, keyIndex);
66
    for (var key in schema) {
67
      var sch = schema[key];
68
      if (Array.isArray(sch)) {
69
        if (key in traverse.arrayKeywords) {
70
          for (var i=0; i<sch.length; i++)
71
            _traverse(opts, pre, post, sch[i], jsonPtr + '/' + key + '/' + i, rootSchema, jsonPtr, key, schema, i);
72
        }
73
      } else if (key in traverse.propsKeywords) {
74
        if (sch && typeof sch == 'object') {
75
          for (var prop in sch)
76
            _traverse(opts, pre, post, sch[prop], jsonPtr + '/' + key + '/' + escapeJsonPtr(prop), rootSchema, jsonPtr, key, schema, prop);
77
        }
78
      } else if (key in traverse.keywords || (opts.allKeys && !(key in traverse.skipKeywords))) {
79
        _traverse(opts, pre, post, sch, jsonPtr + '/' + key, rootSchema, jsonPtr, key, schema);
80
      }
81
    }
82
    post(schema, jsonPtr, rootSchema, parentJsonPtr, parentKeyword, parentSchema, keyIndex);
83
  }
84
}
85

    
86

    
87
function escapeJsonPtr(str) {
88
  return str.replace(/~/g, '~0').replace(/\//g, '~1');
89
}
(5-5/6)