All files / services/validation shacl-validation-configuration.ts

100% Statements 70/70
97.82% Branches 45/46
100% Functions 14/14
100% Lines 68/68

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 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264                                                      19x 4x     15x 15x   15x 22x 1x     21x   21x 4x     17x 17x     15x             2x       2x                         9x                   9x   9x 4x     5x 3x     2x                   4x 4x 4x   4x 9x 2x     7x 7x     4x 2x 3x       4x 6x     4x                   3x 3x   3x 1x     2x                     5x 5x 5x   5x 1x             4x 5x   4x                     5x                   6x 6x   6x 1x               5x                                                         7x 1x     6x 12x 12x 6x       6x     6x       7x 5x   5x 8x       6x            
export interface ShaclGraphShapeConfiguration {
	includeDefaults?: boolean;
	includeShapes?: string[];
	excludeShapes?: string[];
}
 
export interface ShaclValidationConfiguration {
	defaults?: string[];
	graphs?: Record<string, ShaclGraphShapeConfiguration>;
}
 
export interface NormalizedShaclGraphShapeConfiguration {
	includeDefaults: boolean;
	includeShapes: string[];
	excludeShapes: string[];
}
 
export interface ShaclGraphSelectionState extends NormalizedShaclGraphShapeConfiguration {
	defaults: string[];
	effectiveShapes: string[];
	source: 'graph' | 'implicit';
}
 
/**
 * Returns a stable unique array of non-empty string values.
 */
export function toUniqueStringArray(value: unknown): string[] {
	if (!Array.isArray(value)) {
		return [];
	}
 
	const seen = new Set<string>();
	const result: string[] = [];
 
	for (const entry of value) {
		if (typeof entry !== 'string') {
			continue;
		}
 
		const trimmed = entry.trim();
 
		if (!trimmed || seen.has(trimmed)) {
			continue;
		}
 
		seen.add(trimmed);
		result.push(trimmed);
	}
 
	return result;
}
 
/**
 * Normalizes graph-level include/exclude settings to deterministic arrays.
 */
export function normalizeGraphShapeConfiguration(value: unknown): NormalizedShaclGraphShapeConfiguration {
	const config = typeof value === 'object' && value !== null
		? value as Record<string, unknown>
		: {};
 
	return {
		includeDefaults: config.includeDefaults !== false,
		includeShapes: toUniqueStringArray(config.includeShapes),
		excludeShapes: toUniqueStringArray(config.excludeShapes),
	};
}
 
/**
 * Reads default shape graph URIs from the explicit config model.
 */
export function getValidationDefaults(
	validationConfig: ShaclValidationConfiguration | undefined
): string[] {
	return toUniqueStringArray(validationConfig?.defaults);
}
 
/**
 * Returns a normalized graph config entry from the explicit `graphs` map.
 */
export function getGraphShapeConfiguration(
	validationConfig: ShaclValidationConfiguration | undefined,
	graphKey: string
): NormalizedShaclGraphShapeConfiguration | undefined {
	const graphs = validationConfig?.graphs;
 
	if (!graphs || typeof graphs !== 'object' || graphKey.length === 0) {
		return undefined;
	}
 
	if (!(graphKey in graphs)) {
		return undefined;
	}
 
	return normalizeGraphShapeConfiguration(graphs[graphKey]);
}
 
/**
 * Resolves effective shapes using include/exclude precedence.
 */
export function resolveEffectiveShapesFromGraphConfiguration(
	defaults: readonly string[],
	graphConfig: NormalizedShaclGraphShapeConfiguration
): string[] {
	const exclude = new Set(graphConfig.excludeShapes);
	const seen = new Set<string>();
	const result: string[] = [];
 
	const add = (shape: string) => {
		if (exclude.has(shape) || seen.has(shape)) {
			return;
		}
 
		seen.add(shape);
		result.push(shape);
	};
 
	if (graphConfig.includeDefaults) {
		for (const shape of defaults) {
			add(shape);
		}
	}
 
	for (const shape of graphConfig.includeShapes) {
		add(shape);
	}
 
	return result;
}
 
/**
 * Resolves effective shape URIs for a graph.
 */
export function resolveEffectiveShapeGraphs(
	validationConfig: ShaclValidationConfiguration | undefined,
	graphKey: string
): string[] {
	const defaults = getValidationDefaults(validationConfig);
	const graphConfig = getGraphShapeConfiguration(validationConfig, graphKey);
 
	if (graphConfig) {
		return resolveEffectiveShapesFromGraphConfiguration(defaults, graphConfig);
	}
 
	return defaults;
}
 
/**
 * Builds graph-level include/exclude configuration from a selected shape set.
 */
export function buildGraphShapeConfigurationFromSelection(
	selectedShapes: readonly string[],
	defaults: readonly string[],
	includeDefaults: boolean
): NormalizedShaclGraphShapeConfiguration {
	const selected = toUniqueStringArray(selectedShapes);
	const defaultSet = new Set(defaults);
	const selectedSet = new Set(selected);
 
	if (!includeDefaults) {
		return {
			includeDefaults: false,
			includeShapes: selected,
			excludeShapes: [],
		};
	}
 
	const includeShapes = selected.filter(shape => !defaultSet.has(shape));
	const excludeShapes = [...defaultSet].filter(shape => !selectedSet.has(shape));
 
	return {
		includeDefaults: true,
		includeShapes,
		excludeShapes,
	};
}
 
/**
 * Returns true when a graph config is equivalent to implicit defaults behavior.
 */
export function isImplicitGraphShapeConfiguration(config: NormalizedShaclGraphShapeConfiguration): boolean {
	return config.includeDefaults && config.includeShapes.length === 0 && config.excludeShapes.length === 0;
}
 
/**
 * Returns a fully-resolved graph selection state for UI initialization.
 */
export function getGraphSelectionState(
	validationConfig: ShaclValidationConfiguration | undefined,
	graphKey: string
): ShaclGraphSelectionState {
	const defaults = getValidationDefaults(validationConfig);
	const graphConfig = getGraphShapeConfiguration(validationConfig, graphKey);
 
	if (graphConfig) {
		return {
			...graphConfig,
			defaults,
			effectiveShapes: resolveEffectiveShapesFromGraphConfiguration(defaults, graphConfig),
			source: 'graph',
		};
	}
 
	return {
		includeDefaults: true,
		includeShapes: [],
		excludeShapes: [],
		defaults,
		effectiveShapes: defaults,
		source: 'implicit',
	};
}
 
/**
 * Migrates a SHACL validation configuration when files or folders are renamed.
 *
 * Both `graphs` keys and `defaults` entries use workspace-relative `workspace:///...`
 * URI strings as identifiers. For folder renames the match is done by URI prefix with
 * a trailing `/` guard so that renaming `workspace:///models` does not accidentally
 * affect `workspace:///models-extra/thing.ttl`.
 *
 * This function is pure: it returns a new configuration object and does not write
 * to VS Code settings. The caller is responsible for persisting the result.
 *
 * @param config The current SHACL validation configuration.
 * @param renames An array of `{ oldKey, newKey }` pairs using workspace-relative URI strings.
 * @returns A new configuration with all affected keys/entries migrated.
 */
export function migrateShaclValidationConfig(
	config: ShaclValidationConfiguration | undefined,
	renames: ReadonlyArray<{ oldKey: string; newKey: string }>
): ShaclValidationConfiguration {
	if (!config) {
		return {};
	}
 
	const migrateUri = (uri: string): string => {
		for (const { oldKey, newKey } of renames) {
			if (uri === oldKey || uri.startsWith(oldKey + '/')) {
				return newKey + uri.slice(oldKey.length);
			}
		}
 
		return uri;
	};
 
	const migratedDefaults = config.defaults?.map(migrateUri);
 
	let migratedGraphs: Record<string, ShaclGraphShapeConfiguration> | undefined;
 
	if (config.graphs) {
		migratedGraphs = {};
 
		for (const [key, value] of Object.entries(config.graphs)) {
			migratedGraphs[migrateUri(key)] = value;
		}
	}
 
	return {
		...config,
		...(migratedDefaults !== undefined ? { defaults: migratedDefaults } : {}),
		...(migratedGraphs !== undefined ? { graphs: migratedGraphs } : {}),
	};
}