typia
Version:
Superfast runtime validators with only one line
1 lines • 18.2 kB
Source Map (JSON)
{"version":3,"file":"json.mjs","names":[],"sources":["../src/json.ts"],"sourcesContent":["import {\n IJsonSchemaApplication,\n IJsonSchemaCollection,\n IJsonSchemaUnit,\n IValidation,\n Primitive,\n} from \"@typia/interface\";\n\nimport { TypeGuardError } from \"./TypeGuardError\";\nimport { NoTransformConfigurationError } from \"./transformers/NoTransformConfigurationError\";\n\n/* ===========================================================\n JSON\n - METADATA\n - PARSE\n - STRINGIFY\n - FACTORY FUNCTIONS\n==============================================================\n METADATA\n----------------------------------------------------------- */\n/**\n * Generates JSON schema for type `T`.\n *\n * @danger You must configure the generic argument `Type`\n */\nexport function schema(): never;\n\n/**\n * Generates JSON schema for type `T`.\n *\n * Creates {@link IJsonSchemaUnit} containing a main schema and shared\n * components. Named types are stored in `components` for `$ref` referencing.\n *\n * Specify OpenAPI version via `Version` generic (`\"3.0\"` or `\"3.1\"`). Default\n * is `\"3.1\"`. Key difference: `\"3.1\"` supports tuple types.\n *\n * @template Type Target type\n * @template Version OpenAPI version (`\"3.0\"` | `\"3.1\"`). Default `\"3.1\"`\n * @returns JSON schema unit\n */\nexport function schema<\n Type extends unknown,\n Version extends \"3.0\" | \"3.1\" = \"3.1\",\n>(): IJsonSchemaUnit<Version, Type>;\n\n/** @internal */\nexport function schema(): never {\n NoTransformConfigurationError(\"json.schema\");\n}\n\n/**\n * Generates JSON schemas for multiple types.\n *\n * @danger You must configure the generic argument `Types`\n */\nexport function schemas(): never;\n\n/**\n * Generates JSON schemas for multiple types.\n *\n * Creates {@link IJsonSchemaCollection} containing schemas for all types in the\n * tuple. Named types are stored in `components` for `$ref` referencing.\n *\n * Specify OpenAPI version via `Version` generic (`\"3.0\"` or `\"3.1\"`). Default\n * is `\"3.1\"`. Key difference: `\"3.1\"` supports tuple types.\n *\n * @template Types Tuple of target types\n * @template Version OpenAPI version (`\"3.0\"` | `\"3.1\"`). Default `\"3.1\"`\n * @returns JSON schema collection\n */\nexport function schemas<\n Types extends unknown[],\n Version extends \"3.0\" | \"3.1\" = \"3.1\",\n>(): IJsonSchemaCollection<Version, Types>;\n\n/** @internal */\nexport function schemas(): never {\n NoTransformConfigurationError(\"json.schemas\");\n}\n\n/**\n * Generates JSON function schema application.\n *\n * @danger You must configure the generic argument `Class`\n */\nexport function application(): never;\n\n/**\n * Generates JSON function schema application from class/interface.\n *\n * Creates {@link IJsonSchemaApplication} from a TypeScript class or interface,\n * generating JSON schemas for all methods, parameters, and return types.\n * Designed for building custom LLM function calling schemas.\n *\n * The returned object contains:\n *\n * - `functions`: Array of function metadata with parameter/return schemas\n * - `components`: Shared schema components for `$ref` referencing\n *\n * Use cases:\n *\n * - Custom LLM function calling schema formats\n * - API documentation or code generation tools\n * - Alternative LLM integrations that need full JSON Schema\n *\n * For standard LLM function calling, use {@link llm.application} instead, which\n * builds the LLM-optimized function schema directly.\n *\n * @template Class Target class or interface type\n * @template Version OpenAPI version (`\"3.0\"` | `\"3.1\"`). Default `\"3.1\"`\n * @returns JSON function schema application\n */\nexport function application<\n Class extends object,\n Version extends \"3.0\" | \"3.1\" = \"3.1\",\n>(): IJsonSchemaApplication<Version, Class>;\n\n/** @internal */\nexport function application(): never {\n NoTransformConfigurationError(\"json.application\");\n}\n\n/* -----------------------------------------------------------\n PARSE\n----------------------------------------------------------- */\n/**\n * Parses JSON string with assertion.\n *\n * @danger You must configure the generic argument `T`\n */\nexport function assertParse(\n input: string,\n errorFactory?: undefined | ((props: TypeGuardError.IProps) => Error),\n): never;\n\n/**\n * Parses JSON string with assertion.\n *\n * Combines `JSON.parse()` with {@link assert}. Throws {@link TypeGuardError} when\n * parsed value doesn't match type `T`.\n *\n * Related functions:\n *\n * - {@link isParse} — Returns `null` instead of throwing\n * - {@link validateParse} — Returns detailed validation errors\n *\n * @template T Target type for parsed value\n * @param input JSON string to parse\n * @param errorFactory Custom error factory receiving\n * {@link TypeGuardError.IProps}\n * @returns Parsed value of type `T`\n * @throws {TypeGuardError} When parsed value doesn't conform to type `T`\n */\nexport function assertParse<T>(\n input: string,\n errorFactory?: undefined | ((props: TypeGuardError.IProps) => Error),\n): Primitive<T>;\n\n/** @internal */\nexport function assertParse<T>(): Primitive<T> {\n NoTransformConfigurationError(\"json.assertParse\");\n}\n\n/**\n * Parses JSON string with type checking.\n *\n * @danger You must configure the generic argument `T`\n */\nexport function isParse(input: string): never;\n\n/**\n * Parses JSON string with type checking.\n *\n * Combines `JSON.parse()` with {@link is}. Returns `null` when parsed value\n * doesn't match type `T`.\n *\n * Related functions:\n *\n * - {@link assertParse} — Throws instead of returning `null`\n * - {@link validateParse} — Returns detailed validation errors\n *\n * @template T Target type for parsed value\n * @param input JSON string to parse\n * @returns Parsed value of type `T`, or `null` if invalid\n */\nexport function isParse<T>(input: string): Primitive<T> | null;\n\n/** @internal */\nexport function isParse<T>(): Primitive<T> | null {\n NoTransformConfigurationError(\"json.isParse\");\n}\n\n/**\n * Parses JSON string with validation.\n *\n * @danger You must configure the generic argument `T`\n */\nexport function validateParse(input: string): never;\n\n/**\n * Parses JSON string with validation.\n *\n * Combines `JSON.parse()` with {@link validate}. Returns\n * {@link IValidation.IFailure} with all errors on mismatch, or\n * {@link IValidation.ISuccess} with parsed value.\n *\n * Related functions:\n *\n * - {@link assertParse} — Throws on first error\n * - {@link isParse} — Returns `null` instead of error details\n *\n * @template T Target type for parsed value\n * @param input JSON string to parse\n * @returns Validation result containing parsed value or errors\n */\nexport function validateParse<T>(input: string): IValidation<Primitive<T>>;\n\n/** @internal */\nexport function validateParse<T>(): IValidation<Primitive<T>> {\n NoTransformConfigurationError(\"json.validateParse\");\n}\n\n/* -----------------------------------------------------------\n STRINGIFY\n----------------------------------------------------------- */\n/**\n * Converts value to JSON string (8x faster).\n *\n * Generates optimized JSON conversion code specific to type `T`, achieving ~8x\n * faster performance than native `JSON.stringify()`.\n *\n * Does not validate the input. For validation, use:\n *\n * - {@link assertStringify} — Throws on type mismatch\n * - {@link isStringify} — Returns `null` on type mismatch\n * - {@link validateStringify} — Returns detailed validation errors\n *\n * @template T Type of input value\n * @param input Value to stringify\n * @returns JSON string\n */\nexport function stringify<T>(input: T): string;\n\n/** @internal */\nexport function stringify(): never {\n NoTransformConfigurationError(\"json.stringify\");\n}\n\n/**\n * Converts value to JSON string with assertion (5x faster).\n *\n * Combines {@link assert} with {@link stringify}. Throws {@link TypeGuardError}\n * when input doesn't match type `T`. Achieves ~5x faster performance than\n * native `JSON.stringify()`.\n *\n * Related functions:\n *\n * - {@link stringify} — No validation\n * - {@link isStringify} — Returns `null` instead of throwing\n * - {@link validateStringify} — Returns detailed validation errors\n *\n * @template T Type of input value\n * @param input Value to assert and stringify\n * @param errorFactory Custom error factory receiving\n * {@link TypeGuardError.IProps}\n * @returns JSON string\n * @throws {TypeGuardError} When input doesn't conform to type `T`\n */\nexport function assertStringify<T>(\n input: T,\n errorFactory?: undefined | ((props: TypeGuardError.IProps) => Error),\n): string;\n\n/**\n * Converts value to JSON string with assertion (5x faster).\n *\n * Combines {@link assert} with {@link stringify}. Throws {@link TypeGuardError}\n * when input doesn't match type `T`. Achieves ~5x faster performance than\n * native `JSON.stringify()`.\n *\n * Related functions:\n *\n * - {@link stringify} — No validation\n * - {@link isStringify} — Returns `null` instead of throwing\n * - {@link validateStringify} — Returns detailed validation errors\n *\n * @template T Type of input value\n * @param input Value to assert and stringify\n * @param errorFactory Custom error factory receiving\n * {@link TypeGuardError.IProps}\n * @returns JSON string\n * @throws {TypeGuardError} When input doesn't conform to type `T`\n */\nexport function assertStringify<T>(\n input: T,\n errorFactory?: undefined | ((props: TypeGuardError.IProps) => Error),\n): unknown;\n\n/** @internal */\nexport function assertStringify(): string {\n NoTransformConfigurationError(\"json.assertStringify\");\n}\n\n/**\n * Converts value to JSON string with type checking (7x faster).\n *\n * Combines {@link is} with {@link stringify}. Returns `null` when input doesn't\n * match type `T`. Achieves ~7x faster performance than native\n * `JSON.stringify()`.\n *\n * Related functions:\n *\n * - {@link stringify} — No validation\n * - {@link assertStringify} — Throws instead of returning `null`\n * - {@link validateStringify} — Returns detailed validation errors\n *\n * @template T Type of input value\n * @param input Value to check and stringify\n * @returns JSON string, or `null` if type check fails\n */\nexport function isStringify<T>(input: T): string | null;\n\n/**\n * Converts value to JSON string with type checking (7x faster).\n *\n * Combines {@link is} with {@link stringify}. Returns `null` when input doesn't\n * match type `T`. Achieves ~7x faster performance than native\n * `JSON.stringify()`.\n *\n * Related functions:\n *\n * - {@link stringify} — No validation\n * - {@link assertStringify} — Throws instead of returning `null`\n * - {@link validateStringify} — Returns detailed validation errors\n *\n * @template T Type of input value\n * @param input Value to check and stringify\n * @returns JSON string, or `null` if type check fails\n */\nexport function isStringify<T>(input: unknown): string | null;\n\n/** @internal */\nexport function isStringify(): string | null {\n NoTransformConfigurationError(\"json.isStringify\");\n}\n\n/**\n * Converts value to JSON string with validation (5x faster).\n *\n * Combines {@link validate} with {@link stringify}. Returns\n * {@link IValidation.IFailure} with all errors on mismatch, or\n * {@link IValidation.ISuccess} with JSON string. Achieves ~5x faster performance\n * than native `JSON.stringify()`.\n *\n * Related functions:\n *\n * - {@link stringify} — No validation\n * - {@link assertStringify} — Throws on first error\n * - {@link isStringify} — Returns `null` instead of error details\n *\n * @template T Type of input value\n * @param input Value to validate and stringify\n * @returns Validation result containing JSON string or errors\n */\nexport function validateStringify<T>(input: T): IValidation<string>;\n\n/**\n * Converts value to JSON string with validation (5x faster).\n *\n * Combines {@link validate} with {@link stringify}. Returns\n * {@link IValidation.IFailure} with all errors on mismatch, or\n * {@link IValidation.ISuccess} with JSON string. Achieves ~5x faster performance\n * than native `JSON.stringify()`.\n *\n * Related functions:\n *\n * - {@link stringify} — No validation\n * - {@link assertStringify} — Throws on first error\n * - {@link isStringify} — Returns `null` instead of error details\n *\n * @template T Type of input value\n * @param input Value to validate and stringify\n * @returns Validation result containing JSON string or errors\n */\nexport function validateStringify<T>(input: unknown): IValidation<string>;\n\n/** @internal */\nexport function validateStringify(): IValidation<string> {\n NoTransformConfigurationError(\"json.validateStringify\");\n}\n\n/* -----------------------------------------------------------\n FACTORY FUNCTIONS\n----------------------------------------------------------- */\n/**\n * Creates reusable {@link isParse} function.\n *\n * @danger You must configure the generic argument `T`\n */\nexport function createIsParse(): never;\n\n/**\n * Creates reusable {@link isParse} function.\n *\n * @template T Target type for parsed value\n * @returns Reusable parser function\n */\nexport function createIsParse<T>(): (input: string) => Primitive<T> | null;\n\n/** @internal */\nexport function createIsParse<T>(): (input: string) => Primitive<T> | null {\n NoTransformConfigurationError(\"json.createIsParse\");\n}\n\n/**\n * Creates reusable {@link assertParse} function.\n *\n * @danger You must configure the generic argument `T`\n */\nexport function createAssertParse(\n errorFactory?: undefined | ((props: TypeGuardError.IProps) => Error),\n): never;\n\n/**\n * Creates reusable {@link assertParse} function.\n *\n * @template T Target type for parsed value\n * @param errorFactory Custom error factory receiving\n * {@link TypeGuardError.IProps}\n * @returns Reusable parser function\n */\nexport function createAssertParse<T>(\n errorFactory?: undefined | ((props: TypeGuardError.IProps) => Error),\n): (input: string) => Primitive<T>;\n\n/** @internal */\nexport function createAssertParse<T>(): (input: string) => Primitive<T> {\n NoTransformConfigurationError(\"json.createAssertParse\");\n}\n\n/**\n * Creates reusable {@link validateParse} function.\n *\n * @danger You must configure the generic argument `T`\n */\nexport function createValidateParse(): never;\n\n/**\n * Creates reusable {@link validateParse} function.\n *\n * @template T Target type for parsed value\n * @returns Reusable parser function\n */\nexport function createValidateParse<T>(): (\n input: string,\n) => IValidation<Primitive<T>>;\n\n/** @internal */\nexport function createValidateParse<T>(): (\n input: string,\n) => IValidation<Primitive<T>> {\n NoTransformConfigurationError(\"json.createValidateParse\");\n}\n\n/**\n * Creates reusable {@link stringify} function.\n *\n * @danger You must configure the generic argument `T`\n */\nexport function createStringify(): never;\n\n/**\n * Creates reusable {@link stringify} function.\n *\n * @template T Type of input value\n * @returns Reusable stringify function\n */\nexport function createStringify<T>(): (input: T) => string;\n\n/** @internal */\nexport function createStringify<T>(): (input: T) => string {\n NoTransformConfigurationError(\"json.createStringify\");\n}\n\n/**\n * Creates reusable {@link assertStringify} function.\n *\n * @danger You must configure the generic argument `T`\n */\nexport function createAssertStringify(\n errorFactory?: undefined | ((props: TypeGuardError.IProps) => Error),\n): never;\n\n/**\n * Creates reusable {@link assertStringify} function.\n *\n * @template T Type of input value\n * @param errorFactory Custom error factory receiving\n * {@link TypeGuardError.IProps}\n * @returns Reusable stringify function\n */\nexport function createAssertStringify<T>(\n errorFactory?: undefined | ((props: TypeGuardError.IProps) => Error),\n): (input: unknown) => string;\n\n/** @internal */\nexport function createAssertStringify(): (input: unknown) => string {\n NoTransformConfigurationError(\"json.createAssertStringify\");\n}\n\n/**\n * Creates reusable {@link isStringify} function.\n *\n * @danger You must configure the generic argument `T`\n */\nexport function createIsStringify(): never;\n\n/**\n * Creates reusable {@link isStringify} function.\n *\n * @template T Type of input value\n * @returns Reusable stringify function\n */\nexport function createIsStringify<T>(): (input: unknown) => string | null;\n\n/** @internal */\nexport function createIsStringify(): (input: unknown) => string | null {\n NoTransformConfigurationError(\"json.createIsStringify\");\n}\n\n/**\n * Creates reusable {@link validateStringify} function.\n *\n * @danger You must configure the generic argument `T`\n */\nexport function createValidateStringify(): never;\n\n/**\n * Creates reusable {@link validateStringify} function.\n *\n * @template T Type of input value\n * @returns Reusable stringify function\n */\nexport function createValidateStringify<T>(): (\n input: unknown,\n) => IValidation<string>;\n\n/** @internal */\nexport function createValidateStringify(): (\n input: unknown,\n) => IValidation<string> {\n NoTransformConfigurationError(\"json.createValidateStringify\");\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;AA8CA,SAAgB,SAAgB;CAC9B,8BAA8B,aAAa;AAC7C;;AA4BA,SAAgB,UAAiB;CAC/B,8BAA8B,cAAc;AAC9C;;AAwCA,SAAgB,cAAqB;CACnC,8BAA8B,kBAAkB;AAClD;;AAuCA,SAAgB,cAA+B;CAC7C,8BAA8B,kBAAkB;AAClD;;AA2BA,SAAgB,UAAkC;CAChD,8BAA8B,cAAc;AAC9C;;AA4BA,SAAgB,gBAA8C;CAC5D,8BAA8B,oBAAoB;AACpD;;AAwBA,SAAgB,YAAmB;CACjC,8BAA8B,gBAAgB;AAChD;;AAqDA,SAAgB,kBAA0B;CACxC,8BAA8B,sBAAsB;AACtD;;AAyCA,SAAgB,cAA6B;CAC3C,8BAA8B,kBAAkB;AAClD;;AA2CA,SAAgB,oBAAyC;CACvD,8BAA8B,wBAAwB;AACxD;;AAqBA,SAAgB,gBAA2D;CACzE,8BAA8B,oBAAoB;AACpD;;AAwBA,SAAgB,oBAAwD;CACtE,8BAA8B,wBAAwB;AACxD;;AAoBA,SAAgB,sBAEe;CAC7B,8BAA8B,0BAA0B;AAC1D;;AAkBA,SAAgB,kBAA2C;CACzD,8BAA8B,sBAAsB;AACtD;;AAwBA,SAAgB,wBAAoD;CAClE,8BAA8B,4BAA4B;AAC5D;;AAkBA,SAAgB,oBAAuD;CACrE,8BAA8B,wBAAwB;AACxD;;AAoBA,SAAgB,0BAES;CACvB,8BAA8B,8BAA8B;AAC9D"}