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 | 30x 53x 53x 79x 79x 79x 79x 79x 79x 13x 66x 79x 1x 37x 37x 37x 37x 37x 37x 37x 8x 14x 14x 14x 14x 14x 14x 38x 38x 1x 37x 259x 37x 222x 222x 38x 37x 7x 7x 2x 2x | /**
* @fds-uif/core - Identifier Validation
*
* Verifies that every identified array in a UIF (states, modifiers,
* variants, variant options, state classes, structure children, and
* accessibility requirements) uses unique identifiers. Duplicates here are
* always errors because identifier-keyed merging in {@link merge} relies on
* uniqueness, and silently letting the second entry win would surface as a
* confusing missing-feature bug downstream.
*/
import { UifDuplicateIdentifierError } from '../errors.js';
import { IDENTIFIED_ARRAYS } from '../constants.js';
import { getByPath } from './shared.js';
/**
* Configuration for identifier validation.
*/
interface IdentifierConfig {
/** JSON path pattern to the array */
path: string;
/** The field used as identifier */
field: string;
/** Whether to validate recursively (for nested children) */
recursive?: boolean;
}
/**
* All identified arrays in UIF and their configuration.
*/
const IDENTIFIER_CONFIGS: IdentifierConfig[] = [
{ path: 'states', field: IDENTIFIED_ARRAYS.states },
{ path: 'structure.modifiers', field: IDENTIFIED_ARRAYS.modifiers },
{ path: 'structure.variants', field: IDENTIFIED_ARRAYS.variants },
{ path: 'structure.variants.*.options', field: IDENTIFIED_ARRAYS.options },
{ path: 'stateClasses', field: IDENTIFIED_ARRAYS.stateClasses },
{ path: 'structure.children', field: IDENTIFIED_ARRAYS.children, recursive: true },
{ path: 'accessibility.requirements', field: IDENTIFIED_ARRAYS.requirements },
];
/**
* Result of identifier validation.
*/
export interface IdentifierValidationResult {
/** Whether all identifiers are valid (no duplicates) */
valid: boolean;
/** List of duplicate identifier errors */
errors: Array<{
path: string;
field: string;
duplicate: string;
}>;
}
/**
* Check an array for duplicate identifiers.
*/
function checkArrayDuplicates(
arr: unknown[],
field: string,
path: string,
errors: IdentifierValidationResult['errors'],
recursive: boolean = false,
childrenField: string = 'children',
): void {
const seen = new Set<string>();
for (let i = 0; i < arr.length; i++) {
const item = arr[i];
Iif (typeof item !== 'object' || item === null) {
continue;
}
const record = item as Record<string, unknown>;
const identifier = record[field];
Eif (typeof identifier === 'string') {
if (seen.has(identifier)) {
errors.push({
path: `${path}[${i}]`,
field,
duplicate: identifier,
});
} else {
seen.add(identifier);
}
}
if (recursive && Array.isArray(record[childrenField])) {
checkArrayDuplicates(
record[childrenField] as unknown[],
field,
`${path}[${i}].${childrenField}`,
errors,
recursive,
childrenField,
);
}
}
}
/**
* Validate paths containing wildcards (e.g. `'structure.variants.*.options'`).
* Walks the array at the wildcard position and checks the post-wildcard
* path on each item.
*/
function validateWildcardPath(
uif: unknown,
config: IdentifierConfig,
errors: IdentifierValidationResult['errors'],
): void {
const parts = config.path.split('.');
const wildcardIndex = parts.indexOf('*');
Iif (wildcardIndex === -1) return;
const prePath = parts.slice(0, wildcardIndex).join('.');
const postPath = parts.slice(wildcardIndex + 1).join('.');
const preValue = prePath ? getByPath(uif, prePath) : uif;
if (!Array.isArray(preValue)) return;
for (let i = 0; i < preValue.length; i++) {
const item = preValue[i];
const itemPath = prePath ? `${prePath}[${i}]` : `[${i}]`;
const targetArr = postPath ? getByPath(item, postPath) : item;
Eif (Array.isArray(targetArr)) {
const fullPath = postPath ? `${itemPath}.${postPath}` : itemPath;
checkArrayDuplicates(targetArr, config.field, fullPath, errors, config.recursive);
}
}
}
/**
* Validate that all identified arrays in a UIF have unique identifiers.
*
* @param uif - The UIF definition to validate
* @returns Validation result with any duplicate errors
*
* @example
* ```ts
* import { validateIdentifiers } from '@fds-uif/core';
*
* const result = validateIdentifiers(myUif);
* if (!result.valid) {
* console.error('Duplicate identifiers found:', result.errors);
* }
* ```
*/
export function validateIdentifiers(uif: unknown): IdentifierValidationResult {
const errors: IdentifierValidationResult['errors'] = [];
if (typeof uif !== 'object' || uif === null) {
return { valid: true, errors: [] };
}
for (const config of IDENTIFIER_CONFIGS) {
if (config.path.includes('*')) {
validateWildcardPath(uif, config, errors);
} else {
const arr = getByPath(uif, config.path);
if (Array.isArray(arr)) {
checkArrayDuplicates(arr, config.field, config.path, errors, config.recursive);
}
}
}
return {
valid: errors.length === 0,
errors,
};
}
/**
* Validate identifiers and throw if duplicates are found.
*
* @param uif - The UIF definition to validate
* @throws {UifDuplicateIdentifierError} If duplicate identifiers are found
*
* @example
* ```ts
* import { validateIdentifiersOrThrow } from '@fds-uif/core';
*
* try {
* validateIdentifiersOrThrow(myUif);
* } catch (error) {
* if (error instanceof UifDuplicateIdentifierError) {
* console.error('Duplicate found:', error.duplicateValue);
* }
* }
* ```
*/
export function validateIdentifiersOrThrow(uif: unknown): void {
const result = validateIdentifiers(uif);
if (!result.valid && result.errors.length > 0) {
const firstError = result.errors[0];
throw new UifDuplicateIdentifierError(firstError.path, firstError.field, firstError.duplicate);
}
}
|