@waldzellai/clear-thought
Version:
MCP server for systematic thinking, mental models, debugging approaches, and memory management
31 lines (30 loc) • 1.04 kB
JavaScript
import { z } from "zod/v4";
export class ValidationError extends Error {
constructor(context, zodError, prettyError) {
super(`Validation failed in ${context}: ${prettyError}`);
this.context = context;
this.zodError = zodError;
this.prettyError = prettyError;
this.name = 'ValidationError';
}
}
export function validateInput(schema, input, context) {
const result = schema.safeParse(input);
if (!result.success) {
const prettyError = z.prettifyError(result.error);
throw new ValidationError(context, result.error, prettyError);
}
return result.data;
}
export function getFieldError(error, fieldPath) {
const tree = z.treeifyError(error);
// Navigate tree structure to get specific field error
const pathParts = fieldPath.split('.');
let current = tree;
for (const part of pathParts) {
if (!current?.properties?.[part])
return undefined;
current = current.properties[part];
}
return current?.errors?.[0];
}