UNPKG

typia

Version:

Superfast runtime validators with only one line

1 lines 16.3 kB
{"version":3,"file":"protobuf.mjs","names":[],"sources":["../src/protobuf.ts"],"sourcesContent":["import { IValidation, Resolved } from \"@typia/interface\";\n\nimport { TypeGuardError } from \"./TypeGuardError\";\nimport { NoTransformConfigurationError } from \"./transformers/NoTransformConfigurationError\";\n\n/* ===========================================================\n PROTOCOL BUFFER\n - MESSAGE\n - DECODE\n - ENCODE\n - FACTORY FUNCTIONS\n==============================================================\n SCHEMA\n----------------------------------------------------------- */\n/**\n * Generates Protocol Buffer message schema.\n *\n * @danger You must configure the generic argument `T`\n */\nexport function message(): never;\n\n/**\n * Generates Protocol Buffer message schema for type `T`.\n *\n * Creates a `.proto` message definition string from a TypeScript type. Use for\n * sharing schemas with other languages/frameworks.\n *\n * Protocol Buffer has limited expressiveness compared to TypeScript.\n * Incompatible types cause compilation errors.\n *\n * @template T Target type\n * @returns Protocol Buffer message schema string\n * @see https://typia.io/docs/protobuf/message/#restrictions\n */\nexport function message<T>(): string;\n\n/** @internal */\nexport function message(): never {\n NoTransformConfigurationError(\"protobuf.message\");\n}\n\n/* -----------------------------------------------------------\n DECODE\n----------------------------------------------------------- */\n/**\n * Decodes Protocol Buffer binary.\n *\n * @danger You must configure the generic argument `T`\n */\nexport function decode(input: Uint8Array): never;\n\n/**\n * Decodes Protocol Buffer binary to type `T`.\n *\n * Converts Protocol Buffer binary data to a TypeScript instance. Binary data\n * must have been encoded from a `T`-typed value—invalid data causes undefined\n * behavior or errors.\n *\n * For type safety, ensure data was encoded via:\n *\n * - {@link assertEncode} — Encodes with type assertion\n * - {@link isEncode} — Encodes with type checking\n * - {@link validateEncode} — Encodes with validation\n *\n * Note: Decoder validation ({@link assertDecode}, etc.) only checks custom tags\n * like `number & Minimum<7>`, not structural type safety.\n *\n * @template T Target type\n * @param input Protocol Buffer binary data\n * @returns Decoded value of type `T`\n */\nexport function decode<T>(input: Uint8Array): Resolved<T>;\n\n/** @internal */\nexport function decode(): never {\n NoTransformConfigurationError(\"protobuf.decode\");\n}\n\n/**\n * Decodes Protocol Buffer binary with assertion.\n *\n * @danger You must configure the generic argument `T`\n */\nexport function assertDecode(\n input: Uint8Array,\n errorFactory?: undefined | ((props: TypeGuardError.IProps) => Error),\n): never;\n\n/**\n * Decodes Protocol Buffer binary with assertion (partial safety).\n *\n * Combines {@link decode} with {@link assert}. Throws {@link TypeGuardError} on\n * validation failure.\n *\n * Warning: Only validates custom tags (e.g., `number & Minimum<7>`, `string &\n * Format<\"uuid\">`), not structural type correctness. Ensure source data was\n * encoded with type-safe encoders.\n *\n * Related functions:\n *\n * - {@link decode} — No validation\n * - {@link isDecode} — Returns `null` instead of throwing\n * - {@link validateDecode} — Returns detailed validation errors\n *\n * @template T Target type\n * @param input Protocol Buffer binary data\n * @param errorFactory Custom error factory receiving\n * {@link TypeGuardError.IProps}\n * @returns Decoded value of type `T`\n * @throws {TypeGuardError} When custom tag validation fails\n */\nexport function assertDecode<T>(\n input: Uint8Array,\n errorFactory?: undefined | ((props: TypeGuardError.IProps) => Error),\n): Resolved<T>;\n\n/** @internal */\nexport function assertDecode(): never {\n NoTransformConfigurationError(\"protobuf.assertDecode\");\n}\n\n/**\n * Decodes Protocol Buffer binary with type checking.\n *\n * @danger You must configure the generic argument `T`\n */\nexport function isDecode(input: Uint8Array): never;\n\n/**\n * Decodes Protocol Buffer binary with type checking (partial safety).\n *\n * Combines {@link decode} with {@link is}. Returns `null` on validation failure.\n *\n * Warning: Only validates custom tags (e.g., `number & Minimum<7>`, `string &\n * Format<\"uuid\">`), not structural type correctness. Ensure source data was\n * encoded with type-safe encoders.\n *\n * Related functions:\n *\n * - {@link decode} — No validation\n * - {@link assertDecode} — Throws instead of returning `null`\n * - {@link validateDecode} — Returns detailed validation errors\n *\n * @template T Target type\n * @param input Protocol Buffer binary data\n * @returns Decoded value of type `T`, or `null` if invalid\n */\nexport function isDecode<T>(input: Uint8Array): Resolved<T> | null;\n\n/** @internal */\nexport function isDecode(): never {\n NoTransformConfigurationError(\"protobuf.isDecode\");\n}\n\n/**\n * Decodes Protocol Buffer binary with validation.\n *\n * @danger You must configure the generic argument `T`\n */\nexport function validateDecode(input: Uint8Array): never;\n\n/**\n * Decodes Protocol Buffer binary with validation (partial safety).\n *\n * Combines {@link decode} with {@link validate}. Returns\n * {@link IValidation.IFailure} with errors on mismatch, or\n * {@link IValidation.ISuccess} with decoded value.\n *\n * Warning: Only validates custom tags (e.g., `number & Minimum<7>`, `string &\n * Format<\"uuid\">`), not structural type correctness. Ensure source data was\n * encoded with type-safe encoders.\n *\n * Related functions:\n *\n * - {@link decode} — No validation\n * - {@link assertDecode} — Throws on first error\n * - {@link isDecode} — Returns `null` instead of error details\n *\n * @template T Target type\n * @param input Protocol Buffer binary data\n * @returns Validation result containing decoded value or errors\n */\nexport function validateDecode<T>(input: Uint8Array): IValidation<Resolved<T>>;\n\n/** @internal */\nexport function validateDecode(): never {\n NoTransformConfigurationError(\"protobuf.validateDecode\");\n}\n\n/* -----------------------------------------------------------\n ENCODE\n----------------------------------------------------------- */\n/**\n * Encodes value to Protocol Buffer binary.\n *\n * Converts a TypeScript value to Protocol Buffer binary format.\n *\n * Does not validate the input. For validation, use:\n *\n * - {@link assertEncode} — Throws on type mismatch\n * - {@link isEncode} — Returns `null` on type mismatch\n * - {@link validateEncode} — Returns detailed validation errors\n *\n * Protocol Buffer has limited expressiveness compared to TypeScript.\n * Incompatible types cause compilation errors.\n *\n * @template T Type of input value\n * @param input Value to encode\n * @returns Protocol Buffer binary data\n * @see https://typia.io/docs/protobuf/message/#restrictions\n */\nexport function encode<T>(input: T): Uint8Array;\n\n/** @internal */\nexport function encode(): never {\n NoTransformConfigurationError(\"protobuf.encode\");\n}\n\n/**\n * Encodes value to Protocol Buffer binary with assertion.\n *\n * Combines {@link assert} with {@link encode}. Throws {@link TypeGuardError} on\n * type mismatch.\n *\n * Related functions:\n *\n * - {@link encode} — No validation\n * - {@link isEncode} — Returns `null` instead of throwing\n * - {@link validateEncode} — Returns detailed validation errors\n *\n * Protocol Buffer has limited expressiveness compared to TypeScript.\n * Incompatible types cause compilation errors.\n *\n * @template T Type of input value\n * @param input Value to encode\n * @param errorFactory Custom error factory receiving\n * {@link TypeGuardError.IProps}\n * @returns Protocol Buffer binary data\n * @throws {TypeGuardError} When input doesn't conform to type `T`\n * @see https://typia.io/docs/protobuf/message/#restrictions\n */\nexport function assertEncode<T>(\n input: T,\n errorFactory?: undefined | ((props: TypeGuardError.IProps) => Error),\n): Uint8Array;\n\n/** @internal */\nexport function assertEncode<T>(\n input: unknown,\n errorFactory?: undefined | ((props: TypeGuardError.IProps) => Error),\n): Uint8Array;\n\n/** @internal */\nexport function assertEncode(): never {\n NoTransformConfigurationError(\"protobuf.assertEncode\");\n}\n\n/**\n * Encodes value to Protocol Buffer binary with type checking.\n *\n * Combines {@link is} with {@link encode}. Returns `null` on type mismatch.\n *\n * Related functions:\n *\n * - {@link encode} — No validation\n * - {@link assertEncode} — Throws instead of returning `null`\n * - {@link validateEncode} — Returns detailed validation errors\n *\n * Protocol Buffer has limited expressiveness compared to TypeScript.\n * Incompatible types cause compilation errors.\n *\n * @template T Type of input value\n * @param input Value to encode\n * @returns Protocol Buffer binary data, or `null` if invalid\n * @see https://typia.io/docs/protobuf/message/#restrictions\n */\nexport function isEncode<T>(input: T): Uint8Array | null;\n\n/** @internal */\nexport function isEncode<T>(input: unknown): Uint8Array | null;\n\n/** @internal */\nexport function isEncode(): never {\n NoTransformConfigurationError(\"protobuf.isEncode\");\n}\n\n/**\n * Encodes value to Protocol Buffer binary with validation.\n *\n * Combines {@link validate} with {@link encode}. Returns\n * {@link IValidation.IFailure} with all errors on mismatch, or\n * {@link IValidation.ISuccess} with binary data.\n *\n * Related functions:\n *\n * - {@link encode} — No validation\n * - {@link assertEncode} — Throws on first error\n * - {@link isEncode} — Returns `null` instead of error details\n *\n * Protocol Buffer has limited expressiveness compared to TypeScript.\n * Incompatible types cause compilation errors.\n *\n * @template T Type of input value\n * @param input Value to encode\n * @returns Validation result containing binary data or errors\n * @see https://typia.io/docs/protobuf/message/#restrictions\n */\nexport function validateEncode<T>(input: T): IValidation<Uint8Array>;\n\n/** @internal */\nexport function validateEncode<T>(input: unknown): IValidation<Uint8Array>;\n\n/** @internal */\nexport function validateEncode(): never {\n NoTransformConfigurationError(\"protobuf.validateEncode\");\n}\n\n/* -----------------------------------------------------------\n FACTORY FUNCTIONS\n----------------------------------------------------------- */\n/**\n * Creates reusable {@link decode} function.\n *\n * @danger You must configure the generic argument `T`\n */\nexport function createDecode(): never;\n\n/**\n * Creates reusable {@link decode} function.\n *\n * @template T Target type\n * @returns Reusable decoder function\n */\nexport function createDecode<T>(): (input: Uint8Array) => Resolved<T>;\n\n/** @internal */\nexport function createDecode<T>(): (input: Uint8Array) => Resolved<T> {\n NoTransformConfigurationError(\"protobuf.createDecode\");\n}\n\n/**\n * Creates reusable {@link isDecode} function.\n *\n * @danger You must configure the generic argument `T`\n */\nexport function createIsDecode(): never;\n\n/**\n * Creates reusable {@link isDecode} function.\n *\n * @template T Target type\n * @returns Reusable decoder function\n */\nexport function createIsDecode<T>(): (input: Uint8Array) => Resolved<T> | null;\n\n/** @internal */\nexport function createIsDecode<T>(): (input: Uint8Array) => Resolved<T> | null {\n NoTransformConfigurationError(\"protobuf.createIsDecode\");\n}\n\n/**\n * Creates reusable {@link assertDecode} function.\n *\n * @danger You must configure the generic argument `T`\n */\nexport function createAssertDecode(\n errorFactory?: undefined | ((props: TypeGuardError.IProps) => Error),\n): never;\n\n/**\n * Creates reusable {@link assertDecode} function.\n *\n * @template T Target type\n * @param errorFactory Custom error factory receiving\n * {@link TypeGuardError.IProps}\n * @returns Reusable decoder function\n */\nexport function createAssertDecode<T>(\n errorFactory?: undefined | ((props: TypeGuardError.IProps) => Error),\n): (input: Uint8Array) => Resolved<T>;\n\n/** @internal */\nexport function createAssertDecode<T>(): (input: Uint8Array) => Resolved<T> {\n NoTransformConfigurationError(\"protobuf.createAssertDecode\");\n}\n\n/**\n * Creates reusable {@link validateDecode} function.\n *\n * @danger You must configure the generic argument `T`\n */\nexport function createValidateDecode(): never;\n\n/**\n * Creates reusable {@link validateDecode} function.\n *\n * @template T Target type\n * @returns Reusable decoder function\n */\nexport function createValidateDecode<T>(): (\n input: Uint8Array,\n) => IValidation<Resolved<T>>;\n\n/** @internal */\nexport function createValidateDecode<T>(): (\n input: Uint8Array,\n) => IValidation<Resolved<T>> {\n NoTransformConfigurationError(\"protobuf.createValidateDecode\");\n}\n\n/**\n * Creates reusable {@link encode} function.\n *\n * @danger You must configure the generic argument `T`\n */\nexport function createEncode(): never;\n\n/**\n * Creates reusable {@link encode} function.\n *\n * @template T Type of input value\n * @returns Reusable encoder function\n */\nexport function createEncode<T>(): (input: T) => Uint8Array;\n\n/** @internal */\nexport function createEncode<T>(): (input: T) => Uint8Array {\n NoTransformConfigurationError(\"protobuf.createEncode\");\n}\n\n/**\n * Creates reusable {@link isEncode} function.\n *\n * @danger You must configure the generic argument `T`\n */\nexport function createIsEncode(): never;\n\n/**\n * Creates reusable {@link isEncode} function.\n *\n * @template T Type of input value\n * @returns Reusable encoder function\n */\nexport function createIsEncode<T>(): (input: T) => Uint8Array | null;\n\n/** @internal */\nexport function createIsEncode<T>(): (input: T) => Uint8Array | null {\n NoTransformConfigurationError(\"protobuf.createIsEncode\");\n}\n\n/**\n * Creates reusable {@link assertEncode} function.\n *\n * @danger You must configure the generic argument `T`\n */\nexport function createAssertEncode(\n errorFactory?: undefined | ((props: TypeGuardError.IProps) => Error),\n): never;\n\n/**\n * Creates reusable {@link assertEncode} function.\n *\n * @template T Type of input value\n * @param errorFactory Custom error factory receiving\n * {@link TypeGuardError.IProps}\n * @returns Reusable encoder function\n */\nexport function createAssertEncode<T>(\n errorFactory?: undefined | ((props: TypeGuardError.IProps) => Error),\n): (input: T) => Uint8Array;\n\n/** @internal */\nexport function createAssertEncode<T>(): (input: T) => Uint8Array {\n NoTransformConfigurationError(\"protobuf.createAssertEncode\");\n}\n\n/**\n * Creates reusable {@link validateEncode} function.\n *\n * @danger You must configure the generic argument `T`\n */\nexport function createValidateEncode(): never;\n\n/**\n * Creates reusable {@link validateEncode} function.\n *\n * @template T Type of input value\n * @returns Reusable encoder function\n */\nexport function createValidateEncode<T>(): (\n input: T,\n) => IValidation<Uint8Array>;\n\n/** @internal */\nexport function createValidateEncode<T>(): (\n input: T,\n) => IValidation<Uint8Array> {\n NoTransformConfigurationError(\"protobuf.createValidateEncode\");\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;AAqCA,SAAgB,UAAiB;CAC/B,8BAA8B,kBAAkB;AAClD;;AAmCA,SAAgB,SAAgB;CAC9B,8BAA8B,iBAAiB;AACjD;;AAyCA,SAAgB,eAAsB;CACpC,8BAA8B,uBAAuB;AACvD;;AA+BA,SAAgB,WAAkB;CAChC,8BAA8B,mBAAmB;AACnD;;AAiCA,SAAgB,iBAAwB;CACtC,8BAA8B,yBAAyB;AACzD;;AA2BA,SAAgB,SAAgB;CAC9B,8BAA8B,iBAAiB;AACjD;;AAqCA,SAAgB,eAAsB;CACpC,8BAA8B,uBAAuB;AACvD;;AA2BA,SAAgB,WAAkB;CAChC,8BAA8B,mBAAmB;AACnD;;AA6BA,SAAgB,iBAAwB;CACtC,8BAA8B,yBAAyB;AACzD;;AAqBA,SAAgB,eAAsD;CACpE,8BAA8B,uBAAuB;AACvD;;AAkBA,SAAgB,iBAA+D;CAC7E,8BAA8B,yBAAyB;AACzD;;AAwBA,SAAgB,qBAA4D;CAC1E,8BAA8B,6BAA6B;AAC7D;;AAoBA,SAAgB,uBAEc;CAC5B,8BAA8B,+BAA+B;AAC/D;;AAkBA,SAAgB,eAA4C;CAC1D,8BAA8B,uBAAuB;AACvD;;AAkBA,SAAgB,iBAAqD;CACnE,8BAA8B,yBAAyB;AACzD;;AAwBA,SAAgB,qBAAkD;CAChE,8BAA8B,6BAA6B;AAC7D;;AAoBA,SAAgB,uBAEa;CAC3B,8BAA8B,+BAA+B;AAC/D"}