Files
operations/src/validation.ts
glm-5.1 29f0dd7af0 Initial package implementation: operations registry, call protocol, and adapters
Extracted from alkhub_ts packages/core/operations/ and packages/core/mcp/.
- Runtime-agnostic (injected fs/env deps, no Deno globals)
- Direct @logtape/logtape import instead of logger wrapper
- PendingRequestMap with pubsub-wired call protocol
- Peer-dep isolation for MCP adapter (sub-path export)
- Schema const naming convention (XSchema + X type alias)
- 68 tests passing, build + lint + test all green
2026-04-30 12:34:26 +00:00

44 lines
1.2 KiB
TypeScript

import { KindGuard, type TSchema } from "@alkdev/typebox";
import { Value } from "@alkdev/typebox/value";
export function formatValueErrors(
errors: Iterable<{ path: string; message: string }>,
indent: string = " - ",
): string {
return [...errors]
.map((err) => `${indent}${err.path}: ${err.message}`)
.join("\n");
}
export function assertIsSchema(schema: unknown, context?: string): void {
const contextMsg = context ? ` for ${context}` : "";
if (!KindGuard.IsSchema(schema)) {
throw new Error(`Not a valid TypeBox schema${contextMsg}. Use FromSchema() to convert JSON Schema to TypeBox.`);
}
}
export function validateOrThrow(
schema: TSchema,
value: unknown,
context?: string,
): void {
if (!Value.Check(schema, value)) {
const errors = Value.Errors(schema, value);
const formatted = formatValueErrors(errors);
const contextMsg = context ? ` for ${context}` : "";
throw new Error(`Validation failed${contextMsg}:\n${formatted}`);
}
}
export function collectErrors(
schema: TSchema,
value: unknown,
): Array<{ path: string; message: string }> {
if (Value.Check(schema, value)) {
return [];
}
return [...Value.Errors(schema, value)].map((err) => ({
path: err.path,
message: err.message,
}));
}