3 var traverse = module.exports = function (schema, opts, cb) {
4 // Legacy support for v0.3.1 and earlier.
5 if (typeof opts == 'function') {
11 var pre = (typeof cb == 'function') ? cb : cb.pre || function() {};
12 var post = cb.post || function() {};
14 _traverse(opts, pre, post, schema, '', schema);
19 additionalItems: true,
22 additionalProperties: true,
30 traverse.arrayKeywords = {
37 traverse.propsKeywords = {
41 patternProperties: true,
45 traverse.skipKeywords = {
52 exclusiveMaximum: true,
53 exclusiveMinimum: true,
67 function _traverse(opts, pre, post, schema, jsonPtr, rootSchema, parentJsonPtr, parentKeyword, parentSchema, keyIndex) {
68 if (schema && typeof schema == 'object' && !Array.isArray(schema)) {
69 pre(schema, jsonPtr, rootSchema, parentJsonPtr, parentKeyword, parentSchema, keyIndex);
70 for (var key in schema) {
71 var sch = schema[key];
72 if (Array.isArray(sch)) {
73 if (key in traverse.arrayKeywords) {
74 for (var i=0; i<sch.length; i++)
75 _traverse(opts, pre, post, sch[i], jsonPtr + '/' + key + '/' + i, rootSchema, jsonPtr, key, schema, i);
77 } else if (key in traverse.propsKeywords) {
78 if (sch && typeof sch == 'object') {
80 _traverse(opts, pre, post, sch[prop], jsonPtr + '/' + key + '/' + escapeJsonPtr(prop), rootSchema, jsonPtr, key, schema, prop);
82 } else if (key in traverse.keywords || (opts.allKeys && !(key in traverse.skipKeywords))) {
83 _traverse(opts, pre, post, sch, jsonPtr + '/' + key, rootSchema, jsonPtr, key, schema);
86 post(schema, jsonPtr, rootSchema, parentJsonPtr, parentKeyword, parentSchema, keyIndex);
91 function escapeJsonPtr(str) {
92 return str.replace(/~/g, '~0').replace(/\//g, '~1');