UNPKG

typia

Version:

Superfast runtime validators with only one line

1 lines 18.8 kB
{"version":3,"file":"plain.mjs","names":[],"sources":["../src/plain.ts"],"sourcesContent":["/* ===========================================================\n PLAIN\n - CLONE\n - PRUNE\n - CLASSIFY\n - FACTORY FUNCTIONS\n=========================================================== */\nimport {\n Classifiable,\n ClassifyResult,\n IValidation,\n Resolved,\n} from \"@typia/interface\";\n\nimport { TypeGuardError } from \"./TypeGuardError\";\nimport { NoTransformConfigurationError } from \"./transformers/NoTransformConfigurationError\";\n\n/* -----------------------------------------------------------\n CLONE\n----------------------------------------------------------- */\n/**\n * Deep clones value of type `T`.\n *\n * Creates a deep copy of the input value. Class instances with methods are\n * cloned as plain objects (methods are not copied).\n *\n * Does not validate the input. For validation, use:\n *\n * - {@link assertClone} — Throws on type mismatch\n * - {@link isClone} — Returns `null` on type mismatch\n * - {@link validateClone} — Returns detailed validation errors\n *\n * @template T Type of input value\n * @param input Value to clone\n * @returns Deep cloned value\n */\nexport function clone<T>(input: T): Resolved<T>;\n\n/** @internal */\nexport function clone(): never {\n NoTransformConfigurationError(\"plain.clone\");\n}\n\n/**\n * Deep clones value with assertion.\n *\n * Creates a deep copy with {@link assert} validation. Throws\n * {@link TypeGuardError} on type mismatch. Class instances with methods are\n * cloned as plain objects.\n *\n * Related functions:\n *\n * - {@link clone} — No validation\n * - {@link isClone} — Returns `null` instead of throwing\n * - {@link validateClone} — Returns detailed validation errors\n *\n * @template T Type of input value\n * @param input Value to clone\n * @param errorFactory Custom error factory receiving\n * {@link TypeGuardError.IProps}\n * @returns Deep cloned value\n * @throws {TypeGuardError} When input doesn't conform to type `T`\n */\nexport function assertClone<T>(\n input: T,\n errorFactory?: undefined | ((props: TypeGuardError.IProps) => Error),\n): Resolved<T>;\n\n/** @internal */\nexport function assertClone<T>(\n input: unknown,\n errorFactory?: undefined | ((props: TypeGuardError.IProps) => Error),\n): Resolved<T>;\n\n/** @internal */\nexport function assertClone(): never {\n NoTransformConfigurationError(\"plain.assertClone\");\n}\n\n/**\n * Deep clones value with type checking.\n *\n * Creates a deep copy with {@link is} validation. Returns `null` on type\n * mismatch. Class instances with methods are cloned as plain objects.\n *\n * Related functions:\n *\n * - {@link clone} — No validation\n * - {@link assertClone} — Throws instead of returning `null`\n * - {@link validateClone} — Returns detailed validation errors\n *\n * @template T Type of input value\n * @param input Value to clone\n * @returns Deep cloned value, or `null` if invalid\n */\nexport function isClone<T>(input: T): Resolved<T> | null;\n\n/** @internal */\nexport function isClone<T>(input: unknown): Resolved<T> | null;\n\n/** @internal */\nexport function isClone(): never {\n NoTransformConfigurationError(\"plain.isClone\");\n}\n\n/**\n * Deep clones value with validation.\n *\n * Creates a deep copy with {@link validate} validation. Returns\n * {@link IValidation.IFailure} with all errors on mismatch, or\n * {@link IValidation.ISuccess} with cloned value. Class instances with methods\n * are cloned as plain objects.\n *\n * Related functions:\n *\n * - {@link clone} — No validation\n * - {@link assertClone} — Throws on first error\n * - {@link isClone} — Returns `null` instead of error details\n *\n * @template T Type of input value\n * @param input Value to clone\n * @returns Validation result containing cloned value or errors\n */\nexport function validateClone<T>(input: T): IValidation<Resolved<T>>;\n\n/** @internal */\nexport function validateClone<T>(input: unknown): IValidation<Resolved<T>>;\n\n/** @internal */\nexport function validateClone(): never {\n NoTransformConfigurationError(\"plain.validateClone\");\n}\n\n/* -----------------------------------------------------------\n PRUNE\n----------------------------------------------------------- */\n/**\n * Removes superfluous properties from object.\n *\n * Deletes all properties not defined in type `T`, including in nested objects.\n * Mutates the input directly—removed properties cannot be recovered.\n *\n * Does not validate the input. For validation, use:\n *\n * - {@link assertPrune} — Throws on type mismatch\n * - {@link isPrune} — Returns `false` on type mismatch\n * - {@link validatePrune} — Returns detailed validation errors\n *\n * @template T Type of input value\n * @param input Object to prune\n */\nexport function prune<T extends object>(input: T): void;\n\n/** @internal */\nexport function prune(): never {\n NoTransformConfigurationError(\"plain.prune\");\n}\n\n/**\n * Removes superfluous properties with assertion.\n *\n * Combines {@link assert} with {@link prune}. Throws {@link TypeGuardError} on\n * type mismatch. Mutates the input directly—removed properties cannot be\n * recovered.\n *\n * Related functions:\n *\n * - {@link prune} — No validation\n * - {@link isPrune} — Returns `false` instead of throwing\n * - {@link validatePrune} — Returns detailed validation errors\n *\n * @template T Type of input value\n * @param input Object to assert and prune\n * @param errorFactory Custom error factory receiving\n * {@link TypeGuardError.IProps}\n * @returns The pruned input\n * @throws {TypeGuardError} When input doesn't conform to type `T`\n */\nexport function assertPrune<T>(\n input: T,\n errorFactory?: undefined | ((props: TypeGuardError.IProps) => Error),\n): T;\n\n/** @internal */\nexport function assertPrune<T>(\n input: unknown,\n errorFactory?: undefined | ((props: TypeGuardError.IProps) => Error),\n): T;\n\n/** @internal */\nexport function assertPrune(): unknown {\n NoTransformConfigurationError(\"plain.assertPrune\");\n}\n\n/**\n * Removes superfluous properties with type checking.\n *\n * Combines {@link is} with {@link prune}. Returns `false` on type mismatch (no\n * pruning occurs). Returns `true` after successful pruning. Mutates the input\n * directly.\n *\n * Related functions:\n *\n * - {@link prune} — No validation\n * - {@link assertPrune} — Throws instead of returning `false`\n * - {@link validatePrune} — Returns detailed validation errors\n *\n * @template T Type of input value\n * @param input Object to check and prune\n * @returns `true` if valid and pruned, `false` if type mismatch\n */\nexport function isPrune<T>(input: T): input is T;\n\n/** @internal */\nexport function isPrune<T>(input: unknown): input is T;\n\n/** @internal */\nexport function isPrune(): never {\n NoTransformConfigurationError(\"plain.isPrune\");\n}\n\n/**\n * Removes superfluous properties with validation.\n *\n * Combines {@link validate} with {@link prune}. Returns\n * {@link IValidation.IFailure} with all errors on mismatch (no pruning occurs),\n * or {@link IValidation.ISuccess} after successful pruning. Mutates the input\n * directly.\n *\n * Related functions:\n *\n * - {@link prune} — No validation\n * - {@link assertPrune} — Throws on first error\n * - {@link isPrune} — Returns `false` instead of error details\n *\n * @template T Type of input value\n * @param input Object to validate and prune\n * @returns Validation result\n */\nexport function validatePrune<T>(input: T): IValidation<T>;\n\n/** @internal */\nexport function validatePrune<T>(input: unknown): IValidation<T>;\n\n/** @internal */\nexport function validatePrune<T>(): IValidation<T> {\n NoTransformConfigurationError(\"plain.validatePrune\");\n}\n\n/* -----------------------------------------------------------\n CLASSIFY\n----------------------------------------------------------- */\n/**\n * Reconstructs a class instance from plain data.\n *\n * Builds a real instance of class type `T` from a plain object, driven by\n * typia's compile-time type information — no decorators required (unlike\n * `class-transformer`). Each class is constructed by exactly one strategy, in\n * precedence order: a static factory `T.from(x)`, then `new T(x)` (single\n * argument), then field copy onto the prototype. Nested classes and containers\n * are reconstructed recursively, and methods come from the prototype.\n *\n * Does not validate the input. For validation, use:\n *\n * - {@link assertClassify} — Throws on type mismatch\n * - {@link validateClassify} — Returns detailed validation errors\n *\n * @template T Target class type to reconstruct\n * @param input Plain data to classify\n * @returns A real instance of type `T`\n */\nexport function classify<T>(input: Classifiable<T>): ClassifyResult<T>;\n\n/** @internal */\nexport function classify(): never {\n NoTransformConfigurationError(\"plain.classify\");\n}\n\n/**\n * Reconstructs a class instance with assertion.\n *\n * Combines {@link assert} with {@link classify}: validates the plain input\n * against type `T`, throwing {@link TypeGuardError} on mismatch, then builds a\n * real instance of `T`.\n *\n * Related functions:\n *\n * - {@link classify} — No validation\n * - {@link validateClassify} — Returns detailed validation errors\n *\n * @template T Target class type to reconstruct\n * @param input Plain data to validate and classify\n * @param errorFactory Custom error factory receiving\n * {@link TypeGuardError.IProps}\n * @returns A real instance of type `T`\n * @throws {TypeGuardError} When input doesn't conform to type `T`\n */\nexport function assertClassify<T>(\n input: Classifiable<T>,\n errorFactory?: undefined | ((props: TypeGuardError.IProps) => Error),\n): ClassifyResult<T>;\n\n/** @internal */\nexport function assertClassify<T>(\n input: unknown,\n errorFactory?: undefined | ((props: TypeGuardError.IProps) => Error),\n): ClassifyResult<T>;\n\n/** @internal */\nexport function assertClassify(): never {\n NoTransformConfigurationError(\"plain.assertClassify\");\n}\n\n/**\n * Reconstructs a class instance with validation.\n *\n * Combines {@link validate} with {@link classify}: validates the plain input\n * against type `T`, returning {@link IValidation.IFailure} with all errors on\n * mismatch, or {@link IValidation.ISuccess} holding a real instance of `T`.\n *\n * Related functions:\n *\n * - {@link classify} — No validation\n * - {@link assertClassify} — Throws on first error\n *\n * @template T Target class type to reconstruct\n * @param input Plain data to validate and classify\n * @returns Validation result containing the instance or errors\n */\nexport function validateClassify<T>(\n input: Classifiable<T>,\n): IValidation<ClassifyResult<T>>;\n\n/** @internal */\nexport function validateClassify<T>(\n input: unknown,\n): IValidation<ClassifyResult<T>>;\n\n/** @internal */\nexport function validateClassify(): never {\n NoTransformConfigurationError(\"plain.validateClassify\");\n}\n\n/* -----------------------------------------------------------\n FACTORY FUNCTIONS\n----------------------------------------------------------- */\n/**\n * Creates reusable {@link clone} function.\n *\n * @danger You must configure the generic argument `T`\n */\nexport function createClone(): never;\n\n/**\n * Creates reusable {@link clone} function.\n *\n * @template T Type of input value\n * @returns Reusable clone function\n */\nexport function createClone<T>(): (input: T) => Resolved<T>;\n\n/** @internal */\nexport function createClone(): never {\n NoTransformConfigurationError(\"plain.createClone\");\n}\n\n/**\n * Creates reusable {@link assertClone} function.\n *\n * @danger You must configure the generic argument `T`\n */\nexport function createAssertClone(\n errorFactory?: undefined | ((props: TypeGuardError.IProps) => Error),\n): never;\n\n/**\n * Creates reusable {@link assertClone} function.\n *\n * @template T Type of input value\n * @param errorFactory Custom error factory receiving\n * {@link TypeGuardError.IProps}\n * @returns Reusable clone function\n */\nexport function createAssertClone<T>(\n errorFactory?: undefined | ((props: TypeGuardError.IProps) => Error),\n): (input: unknown) => Resolved<T>;\n\n/** @internal */\nexport function createAssertClone(): never {\n NoTransformConfigurationError(\"plain.createAssertClone\");\n}\n\n/**\n * Creates reusable {@link isClone} function.\n *\n * @danger You must configure the generic argument `T`\n */\nexport function createIsClone(): never;\n\n/**\n * Creates reusable {@link isClone} function.\n *\n * @template T Type of input value\n * @returns Reusable clone function\n */\nexport function createIsClone<T>(): (input: unknown) => Resolved<T> | null;\n\n/** @internal */\nexport function createIsClone(): never {\n NoTransformConfigurationError(\"plain.createIsClone\");\n}\n\n/**\n * Creates reusable {@link validateClone} function.\n *\n * @danger You must configure the generic argument `T`\n */\nexport function createValidateClone(): never;\n\n/**\n * Creates reusable {@link validateClone} function.\n *\n * @template T Type of input value\n * @returns Reusable clone function\n */\nexport function createValidateClone<T>(): (\n input: unknown,\n) => IValidation<Resolved<T>>;\n\n/** @internal */\nexport function createValidateClone(): never {\n NoTransformConfigurationError(\"plain.createValidateClone\");\n}\n\n/**\n * Creates reusable {@link prune} function.\n *\n * @danger You must configure the generic argument `T`\n */\nexport function createPrune(): never;\n\n/**\n * Creates reusable {@link prune} function.\n *\n * @template T Type of input value\n * @returns Reusable prune function\n */\nexport function createPrune<T extends object>(): (input: T) => void;\n\n/** @internal */\nexport function createPrune(): never {\n NoTransformConfigurationError(\"plain.createPrune\");\n}\n\n/**\n * Creates reusable {@link assertPrune} function.\n *\n * @danger You must configure the generic argument `T`\n */\nexport function createAssertPrune(\n errorFactory?: undefined | ((props: TypeGuardError.IProps) => Error),\n): never;\n\n/**\n * Creates reusable {@link assertPrune} function.\n *\n * @template T Type of input value\n * @param errorFactory Custom error factory receiving\n * {@link TypeGuardError.IProps}\n * @returns Reusable prune function\n */\nexport function createAssertPrune<T extends object>(\n errorFactory?: undefined | ((props: TypeGuardError.IProps) => Error),\n): (input: unknown) => T;\n\n/** @internal */\nexport function createAssertPrune(): never {\n NoTransformConfigurationError(\"plain.createAssertPrune\");\n}\n\n/**\n * Creates reusable {@link isPrune} function.\n *\n * @danger You must configure the generic argument `T`\n */\nexport function createIsPrune(): never;\n\n/**\n * Creates reusable {@link isPrune} function.\n *\n * @template T Type of input value\n * @returns Reusable prune function\n */\nexport function createIsPrune<T extends object>(): (\n input: unknown,\n) => input is T;\n\n/** @internal */\nexport function createIsPrune(): never {\n NoTransformConfigurationError(\"plain.createIsPrune\");\n}\n\n/**\n * Creates reusable {@link validatePrune} function.\n *\n * @danger You must configure the generic argument `T`\n */\nexport function createValidatePrune(): never;\n\n/**\n * Creates reusable {@link validatePrune} function.\n *\n * @template T Type of input value\n * @returns Reusable prune function\n */\nexport function createValidatePrune<T extends object>(): (\n input: unknown,\n) => IValidation<T>;\n\n/** @internal */\nexport function createValidatePrune(): never {\n NoTransformConfigurationError(\"plain.createValidatePrune\");\n}\n\n/**\n * Creates reusable {@link classify} function.\n *\n * @danger You must configure the generic argument `T`\n */\nexport function createClassify(): never;\n\n/**\n * Creates reusable {@link classify} function.\n *\n * @template T Target class type to reconstruct\n * @returns Reusable classify function\n */\nexport function createClassify<T>(): (\n input: Classifiable<T>,\n) => ClassifyResult<T>;\n\n/** @internal */\nexport function createClassify(): never {\n NoTransformConfigurationError(\"plain.createClassify\");\n}\n\n/**\n * Creates reusable {@link assertClassify} function.\n *\n * @danger You must configure the generic argument `T`\n */\nexport function createAssertClassify(\n errorFactory?: undefined | ((props: TypeGuardError.IProps) => Error),\n): never;\n\n/**\n * Creates reusable {@link assertClassify} function.\n *\n * @template T Target class type to reconstruct\n * @param errorFactory Custom error factory receiving\n * {@link TypeGuardError.IProps}\n * @returns Reusable classify function\n */\nexport function createAssertClassify<T>(\n errorFactory?: undefined | ((props: TypeGuardError.IProps) => Error),\n): (input: unknown) => ClassifyResult<T>;\n\n/** @internal */\nexport function createAssertClassify(): never {\n NoTransformConfigurationError(\"plain.createAssertClassify\");\n}\n\n/**\n * Creates reusable {@link validateClassify} function.\n *\n * @danger You must configure the generic argument `T`\n */\nexport function createValidateClassify(): never;\n\n/**\n * Creates reusable {@link validateClassify} function.\n *\n * @template T Target class type to reconstruct\n * @returns Reusable classify function\n */\nexport function createValidateClassify<T>(): (\n input: unknown,\n) => IValidation<ClassifyResult<T>>;\n\n/** @internal */\nexport function createValidateClassify(): never {\n NoTransformConfigurationError(\"plain.createValidateClassify\");\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAuCA,SAAgB,QAAe;CAC7B,8BAA8B,aAAa;AAC7C;;AAkCA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