UNPKG

json-machete

Version:
75 lines (74 loc) 2.65 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.visitJSONSchema = visitJSONSchema; const identicalFn = (a) => a; const objectFields = [ 'additionalProperties', 'additionalItems', 'contains', 'else', 'if', 'items', 'not', 'then', ]; const dictFields = [ 'anyOf', 'allOf', 'oneOf', 'definitions', 'properties', 'patternProperties', 'discriminatorMapping', ]; async function visitJSONSchema(schema, { enter = identicalFn, leave = identicalFn, }, { visitedSubschemaResultMap, path } = { visitedSubschemaResultMap: new WeakMap(), path: '', }) { if (typeof schema === 'object') { if (!visitedSubschemaResultMap.has(schema)) { const enterResult = await enter(schema, { visitedSubschemaResultMap, path, }); visitedSubschemaResultMap.set(schema, enterResult); for (const key of objectFields) { if (enterResult[key]) { enterResult[key] = await visitJSONSchema(enterResult[key], { enter, leave }, { visitedSubschemaResultMap, path: `${path}/${key}`, }); } } for (const key of dictFields) { if (enterResult[key]) { const entries = Object.entries(enterResult[key]); for (const [itemKey, itemValue] of entries) { enterResult[key][itemKey] = await visitJSONSchema(itemValue, { enter, leave }, { visitedSubschemaResultMap, path: `${path}/${key}/${itemKey}` }); } } } if (enterResult.components?.schemas) { const entries = Object.entries(enterResult.components.schemas); for (const [schemaName, subSchema] of entries) { enterResult.components.schemas[schemaName] = await visitJSONSchema(subSchema, { enter, leave }, { visitedSubschemaResultMap, path: `${path}/components/schemas/${schemaName}` }); } } const leaveResult = await leave(enterResult, { visitedSubschemaResultMap, path, }); visitedSubschemaResultMap.set(schema, leaveResult); return leaveResult; } return visitedSubschemaResultMap.get(schema); } const enterResult = await enter(schema, { visitedSubschemaResultMap, path, }); return leave(enterResult, { visitedSubschemaResultMap, path, }); }