asarfuckery/appasar/development/node_modules/json-schema-traverse/index.js

90 lines
2.3 KiB
JavaScript
Raw Normal View History

2019-01-17 18:22:05 +00:00
'use strict';
var traverse = module.exports = function (schema, opts, cb) {
2019-03-08 00:15:47 +00:00
// Legacy support for v0.3.1 and earlier.
2019-01-17 18:22:05 +00:00
if (typeof opts == 'function') {
cb = opts;
opts = {};
}
2019-03-08 00:15:47 +00:00
cb = opts.cb || cb;
var pre = (typeof cb == 'function') ? cb : cb.pre || function() {};
var post = cb.post || function() {};
_traverse(opts, pre, post, schema, '', schema);
2019-01-17 18:22:05 +00:00
};
traverse.keywords = {
additionalItems: true,
items: true,
contains: true,
additionalProperties: true,
propertyNames: true,
not: true
};
traverse.arrayKeywords = {
items: true,
allOf: true,
anyOf: true,
oneOf: true
};
traverse.propsKeywords = {
definitions: true,
properties: true,
patternProperties: true,
dependencies: true
};
traverse.skipKeywords = {
2019-03-08 00:15:47 +00:00
default: true,
2019-01-17 18:22:05 +00:00
enum: true,
const: true,
required: true,
maximum: true,
minimum: true,
exclusiveMaximum: true,
exclusiveMinimum: true,
multipleOf: true,
maxLength: true,
minLength: true,
pattern: true,
format: true,
maxItems: true,
minItems: true,
uniqueItems: true,
maxProperties: true,
minProperties: true
};
2019-03-08 00:15:47 +00:00
function _traverse(opts, pre, post, schema, jsonPtr, rootSchema, parentJsonPtr, parentKeyword, parentSchema, keyIndex) {
2019-01-17 18:22:05 +00:00
if (schema && typeof schema == 'object' && !Array.isArray(schema)) {
2019-03-08 00:15:47 +00:00
pre(schema, jsonPtr, rootSchema, parentJsonPtr, parentKeyword, parentSchema, keyIndex);
2019-01-17 18:22:05 +00:00
for (var key in schema) {
var sch = schema[key];
if (Array.isArray(sch)) {
if (key in traverse.arrayKeywords) {
for (var i=0; i<sch.length; i++)
2019-03-08 00:15:47 +00:00
_traverse(opts, pre, post, sch[i], jsonPtr + '/' + key + '/' + i, rootSchema, jsonPtr, key, schema, i);
2019-01-17 18:22:05 +00:00
}
} else if (key in traverse.propsKeywords) {
if (sch && typeof sch == 'object') {
for (var prop in sch)
2019-03-08 00:15:47 +00:00
_traverse(opts, pre, post, sch[prop], jsonPtr + '/' + key + '/' + escapeJsonPtr(prop), rootSchema, jsonPtr, key, schema, prop);
2019-01-17 18:22:05 +00:00
}
} else if (key in traverse.keywords || (opts.allKeys && !(key in traverse.skipKeywords))) {
2019-03-08 00:15:47 +00:00
_traverse(opts, pre, post, sch, jsonPtr + '/' + key, rootSchema, jsonPtr, key, schema);
2019-01-17 18:22:05 +00:00
}
}
2019-03-08 00:15:47 +00:00
post(schema, jsonPtr, rootSchema, parentJsonPtr, parentKeyword, parentSchema, keyIndex);
2019-01-17 18:22:05 +00:00
}
}
function escapeJsonPtr(str) {
return str.replace(/~/g, '~0').replace(/\//g, '~1');
}