@smooai/utils
Version:
A collection of shared utilities and tools used across SmooAI projects. This package provides common functionality to standardize and simplify development across all SmooAI repositories.
85 lines (82 loc) • 3.08 kB
TypeScript
import { StandardSchemaV1 } from '@standard-schema/spec';
import { SchemaError } from '@standard-schema/utils';
type Issue = StandardSchemaV1.Issue;
/**
* Error that wraps a SchemaError with a human-readable message.
* This error provides both a human-readable message for display
* and access to the original SchemaError for detailed validation information.
*
* @example
* try {
* await handleSchemaValidation(schema, input);
* } catch (error) {
* if (error instanceof HumanReadableSchemaError) {
* // Get the human-readable message
* console.error(error.message);
* // Access the original SchemaError for detailed validation info
* console.error(error.schemaError);
* }
* }
*/
declare class HumanReadableSchemaError extends Error {
readonly schemaError: SchemaError;
constructor(schemaError: SchemaError);
}
/**
* Formats validation issues into a human-readable message.
* This function takes an array of validation issues and formats them
* into a string that's easy to read and understand, including the path
* to the invalid field.
*
* @param issues - Array of validation issues from Standard Schema validation
* @returns A human-readable string describing the validation errors
*
* @example
* // Single error
* formatStandardSchemaErrorToHumanReadable([{
* message: "Invalid email",
* path: ["user", "email"]
* }])
* // Returns: "Invalid email at "user.email""
*
* // Multiple errors
* formatStandardSchemaErrorToHumanReadable([
* { message: "Invalid email", path: ["user", "email"] },
* { message: "Required", path: ["user", "name"] }
* ])
* // Returns:
* // "1. Invalid email at "user.email"
* // 2. Required at "user.name""
*
* // No errors
* formatStandardSchemaErrorToHumanReadable([])
* // Returns: "No validation errors"
*/
declare function formatStandardSchemaErrorToHumanReadable(issues: ReadonlyArray<Issue>): string;
/**
* Validates input against a schema and returns the typed value.
* This function handles both synchronous and asynchronous validation,
* and throws a HumanReadableSchemaError if validation fails.
*
* @param schema - The Standard Schema to validate against
* @param input - The input value to validate
* @returns The validated and typed output value
* @throws {HumanReadableSchemaError} If validation fails
*
* @example
* // Basic usage
* const value = await handleSchemaValidation(schema, input);
*
* // With error handling
* try {
* const value = await handleSchemaValidation(schema, input);
* // value is properly typed as StandardSchemaV1.InferOutput<T>
* } catch (error) {
* if (error instanceof HumanReadableSchemaError) {
* console.error(error.message); // Human readable message
* console.error(error.schemaError); // Original SchemaError
* }
* }
*/
declare function handleSchemaValidation<T extends StandardSchemaV1>(schema: T, input: StandardSchemaV1.InferInput<T>): Promise<StandardSchemaV1.InferOutput<T>>;
export { HumanReadableSchemaError, formatStandardSchemaErrorToHumanReadable, handleSchemaValidation };