import { AsyncValueProcessor, isIssue, Issue, Path, Result, ValueProcessor } from './types'; export class ValidationError extends Error { constructor(public readonly issues: Issue[]) { super('Validation failed'); this.name = 'ValidationError'; // Set the prototype explicitly Object.setPrototypeOf(this, new.target.prototype); } } const getValue = (result: Result): T => { if (isIssue(result)) throw new ValidationError(result.issues); return result.value; }; export const check = (valueProcessor: ValueProcessor, value: () => unknown, path: Path | Path[] = []): T => { const result = valueProcessor.process(value(), Array.isArray(path) ? path : [path]); return getValue(result); }; export const checkAsync = async (asyncValueProcessor: AsyncValueProcessor, value: () => unknown | Promise, path: Path | Path[] = []): Promise => { const result = await asyncValueProcessor.process(await value(), Array.isArray(path) ? path : [path]); return getValue(result); };