How to use the json-ptr.decode function in json-ptr

To help you get started, we’ve selected a few json-ptr examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github exegesis-js / exegesis / src / utils / jsonSchema.ts View on Github external
if(ctx.replaced[schema.$ref]) {
                schema.$ref = ctx.replaced[schema.$ref];
            } else if(jsonPaths.jsonPointerStartsWith(schema.$ref, ctx.rootSubtreeRef + '/')) {
                ctx.replaced[schema.$ref] = jsonPaths.jsonPointerStripPrefix(schema.$ref, ctx.rootSubtreeRef);
                schema.$ref = ctx.replaced[schema.$ref];
            } else if(!refResolver(schema.$ref)) {
                // Don't know how to resolve this ref
                if(!options.skipUnknownRefs) {
                    throw new Error(`Can't find ref ${schema.$ref}`);
                }
            } else {
                ctx.result.definitions = ctx.result.definitions || {};

                // Find a name to store this under in 'definitions'.
                const origRef = schema.$ref;
                const jsonPath = jsonPtr.decode(schema.$ref);
                let newRefSuffix : string | undefined = jsonPath.length > 0 ? jsonPath[jsonPath.length - 1] : undefined;
                while(!newRefSuffix || ctx.result.definitions[newRefSuffix]) {
                    newRefSuffix = `schema${ctx.schemaCount++}`;
                }

                // Do the replacement.
                schema.$ref = ctx.replaced[schema.$ref] = `#/definitions/${newRefSuffix}`;
                ctx.result.definitions[newRefSuffix] = extractSchemaPriv(origRef, refResolver, options, ctx);
            }
        }
    });
github exegesis-js / exegesis / test / fixtures / index.ts View on Github external
export function makeContext(
    openApiDoc: oas3.OpenAPIObject,
    jsonPointer: string,
    options?: Partial
) {
    return new Oas3CompileContext(
        openApiDoc,
        jsonPtr.decode(jsonPointer),
        Object.assign({}, defaultCompiledOptions, options || {})
    );
}
github exegesis-js / exegesis / src / utils / json-schema-resolve-ref.ts View on Github external
function resolveRefPriv(document: any, ref: string) : any {
    if(!ref.startsWith('#/') && !ref.startsWith('/') && ref !== '') {
        throw new Error(`Cannot resolve non-local ref ${ref}`);
    }

    const path = jsonPtr.decode(ref);
    let currentDoc = document;
    while(path.length > 0) {
        const pathname = path.shift() as string;
        currentDoc = currentDoc && currentDoc[pathname];
        while(currentDoc && currentDoc.$ref) {
            currentDoc = resolveRefPriv(document, currentDoc.$ref);
        }
    }

    return currentDoc;
}

json-ptr

A complete implementation of JSON Pointer (RFC 6901) for nodejs and modern browsers.

MIT
Latest version published 2 years ago

Package Health Score

53 / 100
Full package analysis