All files / src/entity/json JSONSchemaTraverser.ts

100% Statements 26/26
88.46% Branches 23/26
100% Functions 6/6
100% Lines 26/26

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 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150            1x                                               1x 1x                     1x           1x             1x                                           77x                           162x 162x   162x 41x     121x 89x   89x 22x 10x 22x                       10x       79x 10x 10x 29x                       10x     69x 34x             29x      
/*
 * © Copyright 2022 HP Development Company, L.P.
 * SPDX-License-Identifier: MIT
 */
 
// code heavily inspired by: https://github.com/epoberezkin/json-schema-traverse
import { isPlainObject } from '../../lib/object-utils';
 
interface SchemaObject {
	$id?: string;
	$schema?: string;
	[key: string]: any;
}
 
export interface CallbackArgs {
	schema: SchemaObject;
	jsonPtr: string;
	rootSchema: SchemaObject;
	parentJsonPtr?: string;
	parentKeyword?: string;
	parentSchema?: SchemaObject;
	keyIndex?: string | number;
}
 
export type Callback = (args: CallbackArgs) => void;
 
export interface Options {
	allKeys?: boolean;
}
 
export class JSONSchemaTraverser {
	static KEYWORDS = {
		additionalItems: true,
		items: true,
		contains: true,
		additionalProperties: true,
		propertyNames: true,
		not: true,
		if: true,
		then: true,
		else: true
	};
	static ARRAY_KEYWORDS = {
		items: true,
		allOf: true,
		anyOf: true,
		oneOf: true
	};
	static PROPS_KEYWORDS = {
		$defs: true,
		definitions: true,
		properties: true,
		patternProperties: true,
		dependencies: true
	};
	static SKIP_KEYWORDS = {
		default: true,
		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
	};
 
	public static traverse(schema: SchemaObject, cb: Callback, opts: Options = {}) {
		this.recursiveTraverse(opts, cb, schema, '', schema);
	}
 
	private static recursiveTraverse(
		opts: Options,
		cb: Callback,
		schema: SchemaObject,
		jsonPtr: string,
		rootSchema: SchemaObject,
		parentJsonPtr?: string,
		parentKeyword?: string,
		parentSchema?: SchemaObject,
		keyIndex?: number | string
	) {
		Eif (schema && !Array.isArray(schema)) {
			cb({ schema, jsonPtr, rootSchema, parentJsonPtr, parentKeyword, parentSchema, keyIndex });
 
			if (!isPlainObject(schema)) {
				return;
			}
 
			Object.keys(schema).forEach(key => {
				const sch = schema[key];
 
				if (Array.isArray(sch)) {
					if (key in this.ARRAY_KEYWORDS) {
						sch.forEach((_v, i) =>
							this.recursiveTraverse(
								opts,
								cb,
								sch[i],
								`${jsonPtr}/${key}/${i}`,
								rootSchema,
								jsonPtr,
								key,
								schema,
								i
							)
						);
						return;
					}
				}
 
				if (key in this.PROPS_KEYWORDS) {
					Eif (sch && typeof sch === 'object') {
						Object.keys(sch).forEach(prop =>
							this.recursiveTraverse(
								opts,
								cb,
								sch[prop],
								`${jsonPtr}/${key}/${this.escapeJsonPtr(prop)}`,
								rootSchema,
								jsonPtr,
								key,
								schema,
								prop
							)
						);
						return;
					}
				}
				if (key in this.KEYWORDS || (opts?.allKeys && !(key in this.SKIP_KEYWORDS))) {
					this.recursiveTraverse(opts, cb, sch, `${jsonPtr}/${key}`, rootSchema, jsonPtr, key, schema);
				}
			});
		}
	}
 
	private static escapeJsonPtr(str: string): string {
		return str.replace(/~/g, '~0').replace(/\//g, '~1');
	}
}