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 | 13x 1x 13x 1x 2x 2x 1x 1x 1x 2x 13x 13x 2x 14x 1x 1x 13x 2x 2x 2x 2x 1x 11x 1x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 6x 6x 6x 6x 6x 5x 5x 5x 4x 4x 3x | /**
* @fds-uif/core - Validation Orchestrator
*
* Top-level entry points that combine each per-validator file in this
* directory into a single result. Each `collect*` helper bridges a
* validator's typed result shape into the orchestrator's flat
* {@link ValidationError}[] format and is responsible for formatting the
* human-readable message.
*
* Validator files do not depend on each other; this file is the single
* place where they're composed.
*/
import type { ValidationError, ValidationOptions, ValidationResult } from '../types.js';
import { formatPathLine, ValidationErrorCode } from './shared.js';
import { validateExtensions } from './extensions.js';
import { validateIdentifiers, validateIdentifiersOrThrow } from './identifiers.js';
import { validateMarkers, validateMarkersOrThrow } from './markers.js';
import { validateStateReferencesDetailed, validateStateReferencesOrThrow } from './state-references.js';
import { validateVariantConflicts, validateVariantConflictsOrThrow } from './variant-conflicts.js';
import { validateDeprecatedDefaults } from './deprecated-defaults.js';
// ============================================================================
// Per-validator collectors
// ============================================================================
function collectExtensionsErrors(uif: unknown, errors: ValidationError[]): void {
for (const error of validateExtensions(uif).errors) {
errors.push({
code: ValidationErrorCode.INVALID_EXTENSIONS,
message: `${error.message}\n\n ${formatPathLine(error.path)}`,
path: error.path,
severity: 'error',
details: { message: error.message },
});
}
}
function collectIdentifierErrors(uif: unknown, errors: ValidationError[]): void {
for (const error of validateIdentifiers(uif).errors) {
errors.push({
code: ValidationErrorCode.DUPLICATE_IDENTIFIER,
message: `Duplicate ${error.field} "${error.duplicate}"\n\n ${formatPathLine(error.path)}`,
path: error.path,
severity: 'error',
details: { field: error.field, duplicate: error.duplicate },
});
}
}
function formatStateRefMessage(
error: { state: string; path: string; suggestion?: string },
availableStates: string[],
): string {
let message = `Unknown state "${error.state}"`;
if (error.suggestion) {
message += `. Did you mean "${error.suggestion}"?`;
} else Eif (availableStates.length > 0) {
message += `\n\n Available states: ${availableStates.join(', ')}`;
}
return `${message}\n\n ${formatPathLine(error.path)}`;
}
function collectStateRefErrors(uif: unknown, errors: ValidationError[]): void {
const stateResult = validateStateReferencesDetailed(uif);
for (const error of stateResult.errors) {
errors.push({
code: ValidationErrorCode.INVALID_STATE_REFERENCE,
message: formatStateRefMessage(error, stateResult.availableStates),
path: error.path,
severity: 'error',
suggestion: error.suggestion,
details: {
invalidState: error.state,
availableStates: stateResult.availableStates,
},
});
}
}
function collectMarkerErrors(uif: unknown, errors: ValidationError[]): void {
for (const error of validateMarkers(uif).errors) {
const identifier = error.identifier ? ` (${error.identifier})` : '';
errors.push({
code: ValidationErrorCode.CONFLICTING_MARKERS,
message: `Item${identifier} has both $before and $after markers\n\n $before: "${error.beforeValue}"\n $after: "${error.afterValue}"\n\n ${formatPathLine(error.path)}`,
path: error.path,
severity: 'error',
details: {
identifier: error.identifier,
beforeValue: error.beforeValue,
afterValue: error.afterValue,
},
});
}
}
function collectVariantConflictErrors(
uif: unknown,
errors: ValidationError[],
warnings: ValidationError[],
includeWarnings: boolean,
): void {
for (const conflict of validateVariantConflicts(uif).errors) {
const location = conflict.childName === '(root)' ? 'root structure' : `child "${conflict.childName}"`;
const item: ValidationError = {
code: ValidationErrorCode.VARIANT_CONFLICT,
message: `Variant conflict on "${conflict.property}"\n\n ${conflict.variant1}: ${JSON.stringify(conflict.value1)}\n ${conflict.variant2}: ${JSON.stringify(conflict.value2)}\n\n Location: ${location}`,
path: 'structure.variants',
severity: conflict.severity,
details: {
variant1: conflict.variant1,
variant2: conflict.variant2,
childName: conflict.childName,
property: conflict.property,
value1: conflict.value1,
value2: conflict.value2,
},
};
Iif (conflict.severity === 'error') {
errors.push(item);
} else if (includeWarnings) {
warnings.push(item);
}
}
}
function collectDeprecatedDefaultWarnings(uif: unknown, warnings: ValidationError[]): void {
for (const w of validateDeprecatedDefaults(uif)) {
warnings.push(w);
}
}
// ============================================================================
// Public API
// ============================================================================
/**
* Comprehensive UIF validation combining all checks.
*
* @param uif - The UIF definition to validate
* @param options - Validation options
* @returns Combined validation result
*
* @example
* ```ts
* import { validateUif } from '@fds-uif/core';
*
* const result = validateUif(myUif);
*
* if (!result.valid) {
* console.error('Validation errors:', result.errors);
* }
*
* if (result.warnings.length > 0) {
* console.warn('Validation warnings:', result.warnings);
* }
* ```
*/
export function validateUif(uif: unknown, options: ValidationOptions = {}): ValidationResult {
const {
checkIdentifiers = true,
checkStateReferences = true,
checkVariantConflicts = true,
checkExtensions = true,
checkDeprecatedDefaults = true,
includeWarnings = true,
} = options;
const errors: ValidationError[] = [];
const warnings: ValidationError[] = [];
if (checkExtensions) collectExtensionsErrors(uif, errors);
if (checkIdentifiers) collectIdentifierErrors(uif, errors);
if (checkStateReferences) collectStateRefErrors(uif, errors);
collectMarkerErrors(uif, errors);
if (checkVariantConflicts) collectVariantConflictErrors(uif, errors, warnings, includeWarnings);
if (checkDeprecatedDefaults && includeWarnings) collectDeprecatedDefaultWarnings(uif, warnings);
return {
valid: errors.length === 0,
errors,
warnings,
};
}
/**
* Validate UIF and throw if errors are found.
*
* @param uif - The UIF definition to validate
* @param options - Validation options
* @throws {UifDuplicateIdentifierError} If duplicate identifiers are found
* @throws {UifInvalidStateReferenceError} If invalid state references are found
* @throws {UifConflictingMarkersError} If conflicting markers are found
* @throws {UifVariantConflictError} If variant conflicts are found
*
* @example
* ```ts
* import { validateUifOrThrow, UifError } from '@fds-uif/core';
*
* try {
* validateUifOrThrow(myUif);
* console.log('UIF is valid!');
* } catch (error) {
* if (error instanceof UifError) {
* console.error(`Validation failed: ${error.code}`);
* }
* }
* ```
*/
export function validateUifOrThrow(uif: unknown, options: ValidationOptions = {}): void {
const {
checkIdentifiers = true,
checkStateReferences = true,
checkVariantConflicts = true,
checkExtensions = true,
} = options;
Eif (checkExtensions) {
const extensionsResult = validateExtensions(uif);
Iif (!extensionsResult.valid && extensionsResult.errors.length > 0) {
const firstError = extensionsResult.errors[0];
throw new Error(`Invalid extensions: ${firstError.message} at ${firstError.path}`);
}
}
if (checkIdentifiers) {
validateIdentifiersOrThrow(uif);
}
Eif (checkStateReferences) {
validateStateReferencesOrThrow(uif);
}
validateMarkersOrThrow(uif);
if (checkVariantConflicts) {
validateVariantConflictsOrThrow(uif);
}
}
|