dist/@ajsf/core/esm2015/lib/shared/json-schema.functions.js
/**
 * 'resolveSchemaReferences' function
 *
 * Find all $ref links in schema and save links and referenced schemas in
 * schemaRefLibrary, schemaRecursiveRefMap, and dataRecursiveRefMap
 *
 * //  schema
 * //  schemaRefLibrary
 * // { Map<string, string> } schemaRecursiveRefMap
 * // { Map<string, string> } dataRecursiveRefMap
 * // { Map<string, number> } arrayMap
 * //
 */
'resolveSchemaReferences' function
Find all $ref links in schema and save links and referenced schemas in
schemaRefLibrary, schemaRecursiveRefMap, and dataRecursiveRefMap


' resolveSchemaReferences ' function Find all $ref links in schema and save links and referenced schemas in schemaRefLibrary schemaRecursiveRefMap and dataRecursiveRefMap
function resolveSchemaReferences(schema, schemaRefLibrary, schemaRecursiveRefMap, dataRecursiveRefMap, arrayMap) {
    if (!isObject(schema)) {
        console.error('resolveSchemaReferences error: schema must be an object.');
        return;
    }
    const refLinks = new Set();
    const refMapSet = new Set();
    const refMap = new Map();
    const recursiveRefMap = new Map();
    const refLibrary = {};
    JsonPointer.forEachDeep(schema, (subSchema, subSchemaPointer) => {
        if (hasOwn(subSchema, '$ref') && isString(subSchema['$ref'])) {
            const refPointer = JsonPointer.compile(subSchema['$ref']);
            refLinks.add(refPointer);
            refMapSet.add(subSchemaPointer + '~~' + refPointer);
            refMap.set(subSchemaPointer, refPointer);
        }
    });
    refLinks.forEach(ref => refLibrary[ref] = getSubSchema(schema, ref));
    let checkRefLinks = true;
    while (checkRefLinks) {
        checkRefLinks = false;
        Array.from(refMap).forEach(([fromRef1, toRef1]) => Array.from(refMap)
            .filter(([fromRef2, toRef2]) => JsonPointer.isSubPointer(toRef1, fromRef2, true) &&
            !JsonPointer.isSubPointer(toRef2, toRef1, true) &&
            !refMapSet.has(fromRef1 + fromRef2.slice(toRef1.length) + '~~' + toRef2))
            .forEach(([fromRef2, toRef2]) => {
            refMapSet.add(fromRef1 + fromRef2.slice(toRef1.length) + '~~' + toRef2);
            checkRefLinks = true;
        }));
    }
    Array.from(refMapSet)
        .map(refLink => refLink.split('~~'))
        .filter(([fromRef, toRef]) => JsonPointer.isSubPointer(toRef, fromRef))
        .forEach(([fromRef, toRef]) => recursiveRefMap.set(fromRef, toRef));
    Array.from(refMap)
        .filter(([fromRef1, toRef1]) => Array.from(recursiveRefMap.keys())
        .every(fromRef2 => !JsonPointer.isSubPointer(fromRef1, fromRef2, true)))
        .forEach(([fromRef1, toRef1]) => Array.from(recursiveRefMap)
        .filter(([fromRef2, toRef2]) => !recursiveRefMap.has(fromRef1 + fromRef2.slice(toRef1.length)) &&
        JsonPointer.isSubPointer(toRef1, fromRef2, true) &&
        !JsonPointer.isSubPointer(toRef1, fromRef1, true))
        .forEach(([fromRef2, toRef2]) => recursiveRefMap.set(fromRef1 + fromRef2.slice(toRef1.length), fromRef1 + toRef2.slice(toRef1.length))));
    let compiledSchema = Object.assign({}, schema);
    delete compiledSchema.definitions;
    compiledSchema =
        getSubSchema(compiledSchema, '', refLibrary, recursiveRefMap);
    JsonPointer.forEachDeep(compiledSchema, (subSchema, subSchemaPointer) => {
        if (isString(subSchema['$ref'])) {
            let refPointer = JsonPointer.compile(subSchema['$ref']);
            if (!JsonPointer.isSubPointer(refPointer, subSchemaPointer, true)) {
                refPointer = removeRecursiveReferences(subSchemaPointer, recursiveRefMap);
                JsonPointer.set(compiledSchema, subSchemaPointer, { $ref: `#${refPointer}` });
            }
            if (!hasOwn(schemaRefLibrary, 'refPointer')) {
                schemaRefLibrary[refPointer] = !refPointer.length ? compiledSchema :
                    getSubSchema(compiledSchema, refPointer, schemaRefLibrary, recursiveRefMap);
            }
            if (!schemaRecursiveRefMap.has(subSchemaPointer)) {
                schemaRecursiveRefMap.set(subSchemaPointer, refPointer);
            }
            const fromDataRef = JsonPointer.toDataPointer(subSchemaPointer, compiledSchema);
            if (!dataRecursiveRefMap.has(fromDataRef)) {
                const toDataRef = JsonPointer.toDataPointer(refPointer, compiledSchema);
                dataRecursiveRefMap.set(fromDataRef, toDataRef);
            }
        }
        if (subSchema.type === 'array' &&
            (hasOwn(subSchema, 'items') || hasOwn(subSchema, 'additionalItems'))) {
            const dataPointer = JsonPointer.toDataPointer(subSchemaPointer, compiledSchema);
            if (!arrayMap.has(dataPointer)) {
                const tupleItems = isArray(subSchema.items) ? subSchema.items.length : 0;
                arrayMap.set(dataPointer, tupleItems);
            }
        }
    }, true);
    return compiledSchema;
}
function resolveSchemaReferences ( schema , schemaRefLibrary , schemaRecursiveRefMap , dataRecursiveRefMap , arrayMap ) { if ( ! isObject ( schema ) ) { console . error ( 'resolveSchemaReferences error: schema must be an object.' ) ; return ; } const refLinks = new Set ( ) ; const refMapSet = new Set ( ) ; const refMap = new Map ( ) ; const recursiveRefMap = new Map ( ) ; const refLibrary = { } ; JsonPointer . forEachDeep ( schema , ( subSchema , subSchemaPointer ) => { if ( hasOwn ( subSchema , '$ref' ) && isString ( subSchema [ '$ref' ] ) ) { const refPointer = JsonPointer . compile ( subSchema [ '$ref' ] ) ; refLinks . add ( refPointer ) ; refMapSet . add ( subSchemaPointer + '~~' + refPointer ) ; refMap . set ( subSchemaPointer , refPointer ) ; } } ) ; refLinks . forEach ( ref => refLibrary [ ref ] = getSubSchema ( schema , ref ) ) ; let checkRefLinks = true ; while ( checkRefLinks ) { checkRefLinks = false ; Array . from ( refMap ) . forEach ( ( [ fromRef1 , toRef1 ] ) => Array . from ( refMap ) . filter ( ( [ fromRef2 , toRef2 ] ) => JsonPointer . isSubPointer ( toRef1 , fromRef2 , true ) && ! JsonPointer . isSubPointer ( toRef2 , toRef1 , true ) && ! refMapSet . has ( fromRef1 + fromRef2 . slice ( toRef1 . length ) + '~~' + toRef2 ) ) . forEach ( ( [ fromRef2 , toRef2 ] ) => { refMapSet . add ( fromRef1 + fromRef2 . slice ( toRef1 . length ) + '~~' + toRef2 ) ; checkRefLinks = true ; } ) ) ; } Array . from ( refMapSet ) . map ( refLink => refLink . split ( '~~' ) ) . filter ( ( [ fromRef , toRef ] ) => JsonPointer . isSubPointer ( toRef , fromRef ) ) . forEach ( ( [ fromRef , toRef ] ) => recursiveRefMap . set ( fromRef , toRef ) ) ; Array . from ( refMap ) . filter ( ( [ fromRef1 , toRef1 ] ) => Array . from ( recursiveRefMap . keys ( ) ) . every ( fromRef2 => ! JsonPointer . isSubPointer ( fromRef1 , fromRef2 , true ) ) ) . forEach ( ( [ fromRef1 , toRef1 ] ) => Array . from ( recursiveRefMap ) . filter ( ( [ fromRef2 , toRef2 ] ) => ! recursiveRefMap . has ( fromRef1 + fromRef2 . slice ( toRef1 . length ) ) && JsonPointer . isSubPointer ( toRef1 , fromRef2 , true ) && ! JsonPointer . isSubPointer ( toRef1 , fromRef1 , true ) ) . forEach ( ( [ fromRef2 , toRef2 ] ) => recursiveRefMap . set ( fromRef1 + fromRef2 . slice ( toRef1 . length ) , fromRef1 + toRef2 . slice ( toRef1 . length ) ) ) ) ; let compiledSchema = Object . assign ( { } , schema ) ; delete compiledSchema . definitions ; compiledSchema = getSubSchema ( compiledSchema , '' , refLibrary , recursiveRefMap ) ; JsonPointer . forEachDeep ( compiledSchema , ( subSchema , subSchemaPointer ) => { if ( isString ( subSchema [ '$ref' ] ) ) { let refPointer = JsonPointer . compile ( subSchema [ '$ref' ] ) ; if ( ! JsonPointer . isSubPointer ( refPointer , subSchemaPointer , true ) ) { refPointer = removeRecursiveReferences ( subSchemaPointer , recursiveRefMap ) ; JsonPointer . set ( compiledSchema , subSchemaPointer , { $ref : ` ${ refPointer } ` } ) ; } if ( ! hasOwn ( schemaRefLibrary , 'refPointer' ) ) { schemaRefLibrary [ refPointer ] = ! refPointer . length ? compiledSchema : getSubSchema ( compiledSchema , refPointer , schemaRefLibrary , recursiveRefMap ) ; } if ( ! schemaRecursiveRefMap . has ( subSchemaPointer ) ) { schemaRecursiveRefMap . set ( subSchemaPointer , refPointer ) ; } const fromDataRef = JsonPointer . toDataPointer ( subSchemaPointer , compiledSchema ) ; if ( ! dataRecursiveRefMap . has ( fromDataRef ) ) { const toDataRef = JsonPointer . toDataPointer ( refPointer , compiledSchema ) ; dataRecursiveRefMap . set ( fromDataRef , toDataRef ) ; } } if ( subSchema . type === 'array' && ( hasOwn ( subSchema , 'items' ) || hasOwn ( subSchema , 'additionalItems' ) ) ) { const dataPointer = JsonPointer . toDataPointer ( subSchemaPointer , compiledSchema ) ; if ( ! arrayMap . has ( dataPointer ) ) { const tupleItems = isArray ( subSchema . items ) ? subSchema . items . length : 0 ; arrayMap . set ( dataPointer , tupleItems ) ; } } } , true ) ; return compiledSchema ; }
'resolveSchemaReferences' function
 Find all $ref links in schema and save links and referenced schemas in
 schemaRefLibrary, schemaRecursiveRefMap, and dataRecursiveRefMap
' resolveSchemaReferences ' function Find all $ref links in schema and save links and referenced schemas in schemaRefLibrary schemaRecursiveRefMap and dataRecursiveRefMap
// Search schema for all $ref links, and build full refLibrary // Follow all ref links and save in refMapSet, // to find any multi-link recursive refernces // Build full recursiveRefMap // First pass - save all internally recursive refs from refMapSet // Second pass - create recursive versions of any other refs that link to recursive refs // Create compiled schema by replacing all non-recursive $ref links with // thieir linked schemas and, where possible, combining schemas in allOf arrays. // Make sure all remaining schema $refs are recursive, and build final // schemaRefLibrary, schemaRecursiveRefMap, dataRecursiveRefMap, & arrayMap