Files
WRNexusJS/packages/design-system/src/validate.ts
T

48 lines
1.6 KiB
TypeScript

import type { DesignDiagnostic, ResolveOptions } from "./types.ts";
import {
validateDesignSystem as validateSchema,
DesignSystemError,
assertDesignSystem,
} from "./validation.ts";
import { compileDesignSystem } from "./compile.ts";
/** Validate schema and every available mode/density combination, including output bindings. */
export function validateDesignSystem(
input: unknown,
options: ResolveOptions = {},
): DesignDiagnostic[] {
const diagnostics = validateSchema(input);
if (diagnostics.length) return diagnostics;
assertDesignSystem(input);
try {
const initial = compileDesignSystem(input, options);
// Discover inherited density names using a bounded traversal of validated parents.
const densities = new Set<string>();
const seen = new Set<string>();
const walk = (system: typeof input) => {
if (seen.has(system.id)) return;
seen.add(system.id);
for (const name of Object.keys(system.densities ?? {})) densities.add(name);
for (const id of system.extends ?? []) {
const parent = options.registry?.[id];
if (parent) walk(parent);
}
};
walk(input);
if (densities.size * Math.max(initial.resolved.modes.length, 1) > 1024)
return [
{
code: "WRN-DS-LIMIT",
path: "densities",
message: "Validation exceeds 1024 mode/density combinations.",
},
];
for (const density of [...densities].sort())
compileDesignSystem(input, { ...options, density });
return [];
} catch (error) {
if (error instanceof DesignSystemError) return error.diagnostics;
throw error;
}
}