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 | 110x 18x 6x 6x 12x 13x 13x 8x 104x 18x 22x 9x 9x 9x 9x 9x 9x 32x 32x 32x 32x 3x 5x 32x 32x 5x 9x 72x 2x 36x 36x 36x 36x 36x 27x 36x 36x 36x 36x 36x | /**
* @fds-uif/core - Extensions Validation
*
* Verifies the structural shape of `extensions` blocks per the RFC:
* 1. `extensions` must be a plain object when present.
* 2. Every key must be a non-empty namespace string.
* 3. Every namespace value must itself be a plain object.
* 4. Namespace contents are NOT inspected. Each namespace owner is
* responsible for validating its own payload.
*
* Extensions can attach to many places (root, structure, every child,
* modifiers, variants, variant options, states, state classes,
* accessibility, composition), so the bulk of this file is a careful
* structural walk that visits each location once.
*/
/**
* Result of extensions validation.
*/
export interface ExtensionsValidationResult {
/** Whether all extensions are structurally valid */
valid: boolean;
/** Structural errors found in extensions */
errors: Array<{
path: string;
message: string;
}>;
}
/**
* Validate a single `extensions` object's structural rules and append any
* errors to the accumulator.
*/
function checkExtensions(
extensions: unknown,
path: string,
errors: ExtensionsValidationResult['errors'],
): void {
if (extensions === undefined) return;
if (typeof extensions !== 'object' || extensions === null || Array.isArray(extensions)) {
errors.push({ path, message: 'extensions must be a plain object' });
return;
}
for (const [key, value] of Object.entries(extensions as Record<string, unknown>)) {
Iif (typeof key !== 'string' || key.length === 0) {
errors.push({ path: `${path}[""]`, message: 'Namespace key must be a non-empty string' });
continue;
}
if (typeof value !== 'object' || value === null || Array.isArray(value)) {
errors.push({
path: `${path}["${key}"]`,
message: `Namespace value for "${key}" must be a plain object`,
});
}
}
}
/**
* Check `extensions` on each item of an array (e.g. modifiers, states).
* Centralized to avoid five copies of the same indexed loop.
*/
function checkArrayItemExtensions(
arr: unknown,
arrayPath: string,
errors: ExtensionsValidationResult['errors'],
): void {
if (!Array.isArray(arr)) return;
for (let i = 0; i < arr.length; i++) {
checkExtensions((arr[i] as Record<string, unknown>)?.extensions, `${arrayPath}[${i}].extensions`, errors);
}
}
/**
* Check a single variant's `extensions` and walk its options. Split out so
* the caller's loop stays trivial.
*/
function checkVariantExtensions(
variant: unknown,
path: string,
errors: ExtensionsValidationResult['errors'],
): void {
Iif (typeof variant !== 'object' || variant === null) return;
const record = variant as Record<string, unknown>;
checkExtensions(record.extensions, `${path}.extensions`, errors);
Eif (Array.isArray(record.options)) {
for (let j = 0; j < record.options.length; j++) {
checkExtensions(
(record.options[j] as Record<string, unknown>)?.extensions,
`${path}.options[${j}].extensions`,
errors,
);
}
}
}
/**
* Walk a structure node, validating `extensions` on the node itself, its
* modifiers, its variants (and variant options), and recursing into its
* children. This is the only recursive walker; the orchestrator
* (`validateExtensions`) handles the flat top-level locations.
*/
function walkStructureExtensions(
node: unknown,
path: string,
errors: ExtensionsValidationResult['errors'],
): void {
Iif (typeof node !== 'object' || node === null) return;
const record = node as Record<string, unknown>;
checkExtensions(record.extensions, `${path}.extensions`, errors);
if (Array.isArray(record.children)) {
for (let i = 0; i < record.children.length; i++) {
walkStructureExtensions(record.children[i], `${path}.children[${i}]`, errors);
}
}
checkArrayItemExtensions(record.modifiers, `${path}.modifiers`, errors);
if (Array.isArray(record.variants)) {
for (let i = 0; i < record.variants.length; i++) {
checkVariantExtensions(record.variants[i], `${path}.variants[${i}]`, errors);
}
}
}
/**
* Check the `extensions` field of a single object property (e.g.
* `accessibility.extensions`, `composition.extensions`).
*/
function checkObjectPropertyExtensions(
parent: unknown,
propertyPath: string,
errors: ExtensionsValidationResult['errors'],
): void {
if (typeof parent !== 'object' || parent === null) return;
checkExtensions((parent as Record<string, unknown>).extensions, `${propertyPath}.extensions`, errors);
}
/**
* Validate all extensions in a UIF definition.
*
* Checks structural rules per the RFC:
* 1. `extensions` must be a plain object if present
* 2. Every key must be a non-empty string
* 3. Every namespace value must be a plain object
* 4. Namespace contents are not inspected
*/
export function validateExtensions(uif: unknown): ExtensionsValidationResult {
const errors: ExtensionsValidationResult['errors'] = [];
Iif (typeof uif !== 'object' || uif === null) {
return { valid: true, errors: [] };
}
const record = uif as Record<string, unknown>;
checkExtensions(record.extensions, 'extensions', errors);
if (record.structure) {
walkStructureExtensions(record.structure, 'structure', errors);
}
checkArrayItemExtensions(record.states, 'states', errors);
checkArrayItemExtensions(record.stateClasses, 'stateClasses', errors);
checkObjectPropertyExtensions(record.accessibility, 'accessibility', errors);
checkObjectPropertyExtensions(record.composition, 'composition', errors);
return {
valid: errors.length === 0,
errors,
};
}
|