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,
27 traverse.arrayKeywords = {
34 traverse.propsKeywords = {
37 patternProperties: true,
41 traverse.skipKeywords = {
48 exclusiveMaximum: true,
49 exclusiveMinimum: true,
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);
73 } else if (key in traverse.propsKeywords) {
74 if (sch && typeof sch == 'object') {
76 _traverse(opts, pre, post, sch[prop], jsonPtr + '/' + key + '/' + escapeJsonPtr(prop), rootSchema, jsonPtr, key, schema, prop);
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);
82 post(schema, jsonPtr, rootSchema, parentJsonPtr, parentKeyword, parentSchema, keyIndex);
87 function escapeJsonPtr(str) {
88 return str.replace(/~/g, '~0').replace(/\//g, '~1');