@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.
144 lines (143 loc) • 5.45 kB
JavaScript
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
require("../chunk-CoPdw6nB.cjs");
let _standard_schema_utils = require("@standard-schema/utils");
//#region src/validation/standardSchema.ts
/**
* 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);
* }
* }
*/
var HumanReadableSchemaError = class extends Error {
schemaError;
constructor(schemaError) {
super(formatStandardSchemaErrorToHumanReadable(schemaError.issues));
this.schemaError = schemaError;
this.name = "HumanReadableSchemaError";
}
};
/**
* 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"
*/
function formatStandardSchemaErrorToHumanReadable(issues) {
if (issues.length === 0) return "No validation errors";
if (issues.length === 1) {
const issue = issues[0];
const dotPath = (0, _standard_schema_utils.getDotPath)(issue);
const pathString = dotPath ? ` at "${dotPath}"` : "";
return `${issue.message}${pathString}`;
}
return issues.map((issue, index) => {
const dotPath = (0, _standard_schema_utils.getDotPath)(issue);
const pathString = dotPath ? ` at "${dotPath}"` : "";
return `${index + 1}. ${issue.message}${pathString}`;
}).join("\n");
}
/**
* 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
* }
* }
*/
async function handleSchemaValidation(schema, input) {
let result = schema["~standard"].validate(input);
if (result instanceof Promise) result = await result;
if (result.issues) throw new HumanReadableSchemaError(new _standard_schema_utils.SchemaError(result.issues));
return result.value;
}
/**
* Synchronously validates input against a schema and returns the typed value.
* This function performs synchronous validation and throws a HumanReadableSchemaError
* if validation fails. Note that this function will throw an error if the schema
* requires asynchronous validation.
*
* @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
* @throws {Error} If the schema requires asynchronous validation
*
* @example
* // Basic usage
* const value = handleSchemaValidationSync(schema, input);
*
* // With error handling
* try {
* const value = handleSchemaValidationSync(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
* }
* }
*/
function handleSchemaValidationSync(schema, input) {
const result = schema["~standard"].validate(input);
if (result instanceof Promise) throw new Error("Asynchronous validation is not supported, please use a validation library that supports synchronous validation.");
if (result.issues) throw new HumanReadableSchemaError(new _standard_schema_utils.SchemaError(result.issues));
return result.value;
}
//#endregion
exports.HumanReadableSchemaError = HumanReadableSchemaError;
exports.formatStandardSchemaErrorToHumanReadable = formatStandardSchemaErrorToHumanReadable;
exports.handleSchemaValidation = handleSchemaValidation;
exports.handleSchemaValidationSync = handleSchemaValidationSync;
//# sourceMappingURL=standardSchema.cjs.map