UNPKG

typia

Version:

Superfast runtime validators with only one line

1 lines 4.99 kB
{"version":3,"file":"TypeGuardError.mjs","names":[],"sources":["../src/TypeGuardError.ts"],"sourcesContent":["/**\n * Error thrown when type assertion fails.\n *\n * Thrown by {@link assert}, {@link assertGuard}, and other assert-family\n * functions when input doesn't match expected type `T`. Contains detailed\n * information about the first assertion failure:\n *\n * - `method`: Which typia function threw (e.g., `\"typia.assert\"`)\n * - `path`: Property path where error occurred (e.g., `\"input.user.age\"`)\n * - `expected`: Expected type string (e.g., `\"number & ExclusiveMinimum<19>\"`)\n * - `value`: Actual value that failed validation\n *\n * @template T Expected type (for type safety)\n */\nexport class TypeGuardError<T = any> extends Error {\n /**\n * Name of the typia method that threw this error.\n *\n * E.g., `\"typia.assert\"`, `\"typia.assertEquals\"`, `\"typia.assertGuard\"`.\n */\n public readonly method: string;\n\n /**\n * Property path where assertion failed.\n *\n * Uses dot notation for nested properties. `undefined` if error occurred at\n * root level.\n *\n * E.g., `\"input.age\"`, `\"input.profile.email\"`, `\"input[0].name\"`.\n */\n public readonly path: string | undefined;\n\n /**\n * String representation of expected type.\n *\n * E.g., `\"string\"`, `\"number & ExclusiveMinimum<19>\"`, `\"{ name: string; age:\n * number }\"`.\n */\n public readonly expected: string;\n\n /**\n * Actual value that failed assertion.\n *\n * The raw value at the error path, useful for debugging.\n */\n public readonly value: unknown;\n\n /**\n * Optional human-readable error description.\n *\n * Primarily for AI agent libraries or custom validation scenarios needing\n * additional context. Standard assertions rely on `path`, `expected`, and\n * `value` for error reporting.\n */\n public readonly description?: string | undefined;\n\n /**\n * Phantom property for TypeScript type safety.\n *\n * Not used at runtime—exists only to preserve generic type `T` in the type\n * system. Always `undefined`.\n *\n * @internal\n */\n protected readonly fake_expected_typed_value_?: T | undefined;\n\n /**\n * Creates a new TypeGuardError instance.\n *\n * @param props Error properties\n */\n public constructor(props: TypeGuardError.IProps) {\n // MESSAGE CONSTRUCTION\n // Use custom message if provided, otherwise generate default format\n super(\n props.message ||\n `Error on ${props.method}(): invalid type${\n props.path ? ` on ${props.path}` : \"\"\n }, expect to be ${props.expected}`,\n );\n\n // INHERITANCE POLYFILL\n // Set up prototype for compatibility across different JavaScript environments\n const proto = new.target.prototype;\n if (Object.setPrototypeOf) Object.setPrototypeOf(this, proto);\n else (this as any).__proto__ = proto;\n\n // ASSIGN MEMBERS\n this.name = \"TypeGuardError\";\n this.method = props.method;\n this.path = props.path;\n this.expected = props.expected;\n this.value = props.value;\n if (props.description || props.value === undefined)\n this.description =\n props.description ??\n [\n \"The value at this path is `undefined`.\",\n \"\",\n `Please fill the \\`${props.expected}\\` typed value next time.`,\n ].join(\"\\n\");\n }\n}\n\nexport namespace TypeGuardError {\n /** Properties for constructing a TypeGuardError. */\n export interface IProps {\n /**\n * Name of the typia method that threw the error.\n *\n * E.g., `\"typia.assert\"`, `\"typia.assertEquals\"`.\n */\n method: string;\n\n /**\n * Property path where assertion failed (optional).\n *\n * E.g., `\"input.age\"`, `\"input.profile.email\"`.\n */\n path?: undefined | string;\n\n /**\n * String representation of expected type.\n *\n * E.g., `\"string\"`, `\"number & ExclusiveMinimum<19>\"`.\n */\n expected: string;\n\n /** Actual value that failed assertion. */\n value: unknown;\n\n /**\n * Optional human-readable error description.\n *\n * For AI agent libraries or custom validation needing additional context.\n */\n description?: string;\n\n /**\n * Custom error message (optional).\n *\n * If not provided, a default message is generated from other properties.\n */\n message?: undefined | string;\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;AAcA,IAAa,iBAAb,cAA6C,MAAM;;;;;;CAyDjD,YAAmB,OAA8B;EAG/C,MACE,MAAM,WACJ,YAAY,MAAM,OAAO,kBACvB,MAAM,OAAO,OAAO,MAAM,SAAS,GACpC,iBAAiB,MAAM,UAC5B;EAIA,MAAM,QAAQ,IAAI,OAAO;EACzB,IAAI,OAAO,gBAAgB,OAAO,eAAe,MAAM,KAAK;OACvD,KAAc,YAAY;EAG/B,KAAK,OAAO;EACZ,KAAK,SAAS,MAAM;EACpB,KAAK,OAAO,MAAM;EAClB,KAAK,WAAW,MAAM;EACtB,KAAK,QAAQ,MAAM;EACnB,IAAI,MAAM,eAAe,MAAM,UAAU,KAAA,GACvC,KAAK,cACH,MAAM,eACN;GACE;GACA;GACA,qBAAqB,MAAM,SAAS;EACtC,CAAC,CAAC,KAAK,IAAI;CACjB;AACF"}