UNPKG

@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.

1 lines 5.02 kB
{"version":3,"sources":["../src/validation/standardSchema.ts"],"sourcesContent":["import { StandardSchemaV1 } from '@standard-schema/spec';\nimport { getDotPath, SchemaError } from '@standard-schema/utils';\n\ntype Issue = StandardSchemaV1.Issue;\n\n/**\n * Error that wraps a SchemaError with a human-readable message.\n * This error provides both a human-readable message for display\n * and access to the original SchemaError for detailed validation information.\n *\n * @example\n * try {\n * await handleSchemaValidation(schema, input);\n * } catch (error) {\n * if (error instanceof HumanReadableSchemaError) {\n * // Get the human-readable message\n * console.error(error.message);\n * // Access the original SchemaError for detailed validation info\n * console.error(error.schemaError);\n * }\n * }\n */\nexport class HumanReadableSchemaError extends Error {\n constructor(public readonly schemaError: SchemaError) {\n super(formatStandardSchemaErrorToHumanReadable(schemaError.issues));\n this.name = 'HumanReadableSchemaError';\n }\n}\n\n/**\n * Formats validation issues into a human-readable message.\n * This function takes an array of validation issues and formats them\n * into a string that's easy to read and understand, including the path\n * to the invalid field.\n *\n * @param issues - Array of validation issues from Standard Schema validation\n * @returns A human-readable string describing the validation errors\n *\n * @example\n * // Single error\n * formatStandardSchemaErrorToHumanReadable([{\n * message: \"Invalid email\",\n * path: [\"user\", \"email\"]\n * }])\n * // Returns: \"Invalid email at \"user.email\"\"\n *\n * // Multiple errors\n * formatStandardSchemaErrorToHumanReadable([\n * { message: \"Invalid email\", path: [\"user\", \"email\"] },\n * { message: \"Required\", path: [\"user\", \"name\"] }\n * ])\n * // Returns:\n * // \"1. Invalid email at \"user.email\"\n * // 2. Required at \"user.name\"\"\n *\n * // No errors\n * formatStandardSchemaErrorToHumanReadable([])\n * // Returns: \"No validation errors\"\n */\nexport function formatStandardSchemaErrorToHumanReadable(issues: ReadonlyArray<Issue>): string {\n if (issues.length === 0) {\n return 'No validation errors';\n }\n\n if (issues.length === 1) {\n const issue = issues[0];\n const dotPath = getDotPath(issue);\n const pathString = dotPath ? ` at \"${dotPath}\"` : '';\n return `${issue.message}${pathString}`;\n }\n\n return issues\n .map((issue, index) => {\n const dotPath = getDotPath(issue);\n const pathString = dotPath ? ` at \"${dotPath}\"` : '';\n return `${index + 1}. ${issue.message}${pathString}`;\n })\n .join('\\n');\n}\n\n/**\n * Validates input against a schema and returns the typed value.\n * This function handles both synchronous and asynchronous validation,\n * and throws a HumanReadableSchemaError if validation fails.\n *\n * @param schema - The Standard Schema to validate against\n * @param input - The input value to validate\n * @returns The validated and typed output value\n * @throws {HumanReadableSchemaError} If validation fails\n *\n * @example\n * // Basic usage\n * const value = await handleSchemaValidation(schema, input);\n *\n * // With error handling\n * try {\n * const value = await handleSchemaValidation(schema, input);\n * // value is properly typed as StandardSchemaV1.InferOutput<T>\n * } catch (error) {\n * if (error instanceof HumanReadableSchemaError) {\n * console.error(error.message); // Human readable message\n * console.error(error.schemaError); // Original SchemaError\n * }\n * }\n */\nexport async function handleSchemaValidation<T extends StandardSchemaV1>(\n schema: T,\n input: StandardSchemaV1.InferInput<T>,\n): Promise<StandardSchemaV1.InferOutput<T>> {\n let result = schema['~standard'].validate(input);\n if (result instanceof Promise) result = await result;\n\n if (result.issues) {\n const schemaError = new SchemaError(result.issues);\n throw new HumanReadableSchemaError(schemaError);\n }\n\n return result.value;\n}\n"],"mappings":";AACA,SAAS,YAAY,mBAAmB;AAqBjC,IAAM,2BAAN,cAAuC,MAAM;AAAA,EAChD,YAA4B,aAA0B;AAClD,UAAM,yCAAyC,YAAY,MAAM,CAAC;AAD1C;AAExB,SAAK,OAAO;AAAA,EAChB;AACJ;AAgCO,SAAS,yCAAyC,QAAsC;AAC3F,MAAI,OAAO,WAAW,GAAG;AACrB,WAAO;AAAA,EACX;AAEA,MAAI,OAAO,WAAW,GAAG;AACrB,UAAM,QAAQ,OAAO,CAAC;AACtB,UAAM,UAAU,WAAW,KAAK;AAChC,UAAM,aAAa,UAAU,QAAQ,OAAO,MAAM;AAClD,WAAO,GAAG,MAAM,OAAO,GAAG,UAAU;AAAA,EACxC;AAEA,SAAO,OACF,IAAI,CAAC,OAAO,UAAU;AACnB,UAAM,UAAU,WAAW,KAAK;AAChC,UAAM,aAAa,UAAU,QAAQ,OAAO,MAAM;AAClD,WAAO,GAAG,QAAQ,CAAC,KAAK,MAAM,OAAO,GAAG,UAAU;AAAA,EACtD,CAAC,EACA,KAAK,IAAI;AAClB;AA2BA,eAAsB,uBAClB,QACA,OACwC;AACxC,MAAI,SAAS,OAAO,WAAW,EAAE,SAAS,KAAK;AAC/C,MAAI,kBAAkB,QAAS,UAAS,MAAM;AAE9C,MAAI,OAAO,QAAQ;AACf,UAAM,cAAc,IAAI,YAAY,OAAO,MAAM;AACjD,UAAM,IAAI,yBAAyB,WAAW;AAAA,EAClD;AAEA,SAAO,OAAO;AAClB;","names":[]}