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 6.96 kB
{"version":3,"file":"standardSchema.cjs","names":["SchemaError"],"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\n/**\n * Synchronously validates input against a schema and returns the typed value.\n * This function performs synchronous validation and throws a HumanReadableSchemaError\n * if validation fails. Note that this function will throw an error if the schema\n * requires asynchronous validation.\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 * @throws {Error} If the schema requires asynchronous validation\n *\n * @example\n * // Basic usage\n * const value = handleSchemaValidationSync(schema, input);\n *\n * // With error handling\n * try {\n * const value = handleSchemaValidationSync(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 function handleSchemaValidationSync<T extends StandardSchemaV1>(schema: T, input: StandardSchemaV1.InferInput<T>): StandardSchemaV1.InferOutput<T> {\n const result = schema['~standard'].validate(input);\n if (result instanceof Promise) {\n throw new Error('Asynchronous validation is not supported, please use a validation library that supports synchronous validation.');\n }\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":";;;;;;;;;;;;;;;;;;;;;AAsBA,IAAa,2BAAb,cAA8C,MAAM;CACpB;CAA5B,YAAY,aAA0C;EAClD,MAAM,yCAAyC,YAAY,MAAM,CAAC;EAD1C,KAAA,cAAA;EAExB,KAAK,OAAO;CAChB;AACJ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAgCA,SAAgB,yCAAyC,QAAsC;CAC3F,IAAI,OAAO,WAAW,GAClB,OAAO;CAGX,IAAI,OAAO,WAAW,GAAG;EACrB,MAAM,QAAQ,OAAO;EACrB,MAAM,WAAA,GAAA,uBAAA,YAAqB,KAAK;EAChC,MAAM,aAAa,UAAU,QAAQ,QAAQ,KAAK;EAClD,OAAO,GAAG,MAAM,UAAU;CAC9B;CAEA,OAAO,OACF,KAAK,OAAO,UAAU;EACnB,MAAM,WAAA,GAAA,uBAAA,YAAqB,KAAK;EAChC,MAAM,aAAa,UAAU,QAAQ,QAAQ,KAAK;EAClD,OAAO,GAAG,QAAQ,EAAE,IAAI,MAAM,UAAU;CAC5C,CAAC,EACA,KAAK,IAAI;AAClB;;;;;;;;;;;;;;;;;;;;;;;;;;AA2BA,eAAsB,uBAClB,QACA,OACwC;CACxC,IAAI,SAAS,OAAO,aAAa,SAAS,KAAK;CAC/C,IAAI,kBAAkB,SAAS,SAAS,MAAM;CAE9C,IAAI,OAAO,QAEP,MAAM,IAAI,yBAAyB,IADXA,uBAAAA,YAAY,OAAO,MACE,CAAC;CAGlD,OAAO,OAAO;AAClB;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA6BA,SAAgB,2BAAuD,QAAW,OAAwE;CACtJ,MAAM,SAAS,OAAO,aAAa,SAAS,KAAK;CACjD,IAAI,kBAAkB,SAClB,MAAM,IAAI,MAAM,iHAAiH;CAGrI,IAAI,OAAO,QAEP,MAAM,IAAI,yBAAyB,IADXA,uBAAAA,YAAY,OAAO,MACE,CAAC;CAGlD,OAAO,OAAO;AAClB"}