Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | 1x 1x 1x 76x 76x 149x 149x 149x 149x 104x 76x 28x 76x | /*
* © Copyright 2022 HP Development Company, L.P.
* SPDX-License-Identifier: MIT
*/
import set from 'immutable-set';
import { EntityDefinitionJSONSchema, TransformEntityDefinitionSchemaCallback } from '../types';
import { JSONSchemaTraverser } from './JSONSchemaTraverser';
/**
* This function helps to traverse an EntityDefinitionJSONSchema object.
* This can be useful for creating derived schemas, like those used by OpenAPI or Ajv.
* @param entityDefinitionSchema
* @param callback
*/
export function transformEntityDefinitionSchema<T extends object = {}>(
entityDefinitionSchema: Partial<EntityDefinitionJSONSchema>,
callback: TransformEntityDefinitionSchemaCallback
) {
let obj = {} as T;
JSONSchemaTraverser.traverse(
entityDefinitionSchema,
({ schema, jsonPtr, rootSchema, parentJsonPtr, parentKeyword, parentSchema, keyIndex }) => {
const pointerPath = jsonPtr
.replace(/\//g, '.') // replace slashes with dots
.replace(/(\.(\d))((\.)|$)/g, '[$2]$3') // replace `.{number}.` to `[number].
.slice(1); // remove trailing slash
const pointerPathParts = pointerPath.split('.');
const result = callback({
schema,
jsonPtr,
pointerPath,
pointerPathParts,
rootSchema,
parentJsonPtr,
parentKeyword,
parentSchema,
keyIndex
});
if (result && typeof result.path !== 'undefined' && result.path !== null) {
if (result.path === '') {
obj = result.value as T;
} else {
obj = set(obj, result.path, result.value, { withArrays: true });
}
}
},
{ allKeys: true }
);
return obj;
}
|