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
This commit is contained in:
44
src/validation.ts
Normal file
44
src/validation.ts
Normal file
@@ -0,0 +1,44 @@
|
||||
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,
|
||||
}));
|
||||
}
|
||||
Reference in New Issue
Block a user