New Features
Protocol Buffer
https://typia.io/docs/protobuf/message/
Typia v5 supports Protocol Buffer functions.
Lots of TypeScript users had requested this feature, and I've implemented.
@lemon-mint helped me a lot, and repository as-proto
(of @piotr-oles) was great to reference.
Now, let's encode to protocol buffer binary data with only one line function call, as always we've done.
export namespace protobuf {
export function message<T>(): string; // Protocol Buffer message
export function assertDecode<T>(buffer: Buffer): T; // safe decoder
export function assertEncode<T>(input: T): Uint8Array; // safe encoder
}
Type Tags
https://typia.io/docs/validators/tags/
Typia v5 supports new type tags for custom validation.
From now on, making custom validation logic is much easier and safer than before.
import typia, { tags } from "typia";
export const checkSomething = typia.createIs<Something>();
//----
// DEFINE CUSTOM TYPE TAGS
//----
type Dollar = tags.TagBase<{
kind: "dollar";
target: "string";
value: undefined;
validate: `$input[0] === "$" && !isNaN(Number($input.substring(1).split(",").join("")))`;
}>;
type Postfix<Value extends string> = tags.TagBase<{
kind: "postfix";
target: "string";
value: Value;
validate: `$input.endsWith("${Value}")`;
}>;
type IsEven<Value extends number | bigint> = tags.TagBase<{
kind: "isEven";
target: Value extends number ? "number" : "bigint";
value: undefined;
validate: `$input % ${Numeric<2>} === ${Numeric<0>}`;
}>;
type Numeric<Value extends number | bigint> = Value extends number
? Value
: `BigInt(${Value})`;
Advances
Compile Error Reporting
https://typia.io/docs/protobuf/message/#restrictions
When user tries to call typia
function with unsupported type like bigint
type for JSON functions, typia
had occured exception that kills TypeScript compiler, even when running tsc --watch
command. It was not a critical problem, because except bigint
type, functions in typia
supported every TypeScript type.
However, as typia
starts supporting Protocol Buffer functions in v5 update, and expression power of Protocol Buffer is extremely narrower than TypeScript type specs. User may try many types in Protocol Buffer functions, and gradually fix their TypeScript types referencing error message from typia
. In that circumstance, if typia
kills tsc --watch
command whenever unsupported type comes, it would be a great annoying problem for users.
Therefore, I've changed typia
to create compilation error instead of throwing exception. From now on, whenever you take a mistake on typia
functions with unsupported types, it would be a compilation error, and would be printed on your console like below
main.ts:13:1 - error TS(typia.protobuf.message): unsupported type detected
- Something.any: any
- does not support any type
- Something.unknown: any
- does not support any type
- Something.closure: unknown
- does not support functional type
- Something.dict: (Set<string> | WeakMap | WeakSet)
- does not support Set type
- does not support WeakSet type. Use Array type instead.
- does not support WeakMap type. Use Map type instead.
- Something.date: Date
- does not support Date type. Use string type instead.
- Something.classic: String
- does not support String type. Use string type instead.
- Something.buffer: ArrayBuffer
- does not support ArrayBuffer type. Use Uint8Array type instead.
Much more test functions
As type system of Protocol Buffer is extremely different with TypeScript (expression power of Protocol Buffer is much lower than TypeScript), I had to develop much more test programs. Especially, to support restrictions of Protocol Buffer types, I've added a new genre of test programs that simulating compilation errors that must be happened.
As a result of such development, LOC of test programs are reaching to 1,400,000. It was a hard time for me, but of that, I believe that users of typia
can use it with more confidence. From now on, my slogan is "The word best validator library ensured by 1.4M LOC test codes.". You typia
users, as almost all types of TypeScript are rigorously verified with a test program, just use it with confidence.
random()
function
Until v4 version, typia.random<T>()
function could not generate bigint
or Uint8Array
type, because it had been designed for JSON data, especially for RestAPI backend environments (I'd used it for mockup simulator generator - https://nestia.io/docs/sdk/simulator/). By the way, as v5 version starts supporting Protocol Buffer features, I've also advanced typia.random<T>()
function.
Therefore, return type of typia.random<T>()
function has been changed from Primitive<T>
type to Resolved<T>
to support bigint
and Uint8Array
types. Also, as v5 version of typia
supports advanced custom validation feature, typia.random<T>()
function also changed its custom random data generation interface to be much efficient.
Breaking Changes
Namespace refactoring
// RUNTIME VALIDATORS
export function is<T>(input: unknown): input is T; // returns boolean
export function assert<T>(input: unknown): T; // throws TypeGuardError
export function validate<T>(input: unknown): IValidation<T>; // detailed
// JSON FUNCTIONS
export namespace json {
export function application<T>(): IJsonApplication; // JSON schema
export function assertParse<T>(input: string): T; // type safe parser
export function assertStringify<T>(input: T): string; // safe and faster
}
// PROTOCOL BUFFER
export namespace protobuf {
export function message<T>(): string; // Protocol Buffer message
export function assertDecode<T>(buffer: Buffer): T; // safe decoder
export function assertEncode<T>(input: T): Uint8Array; // safe encoder
}
// RANDOM GENERATOR
export function random<T>(g?: Partial<IRandomGenerator>): T;
As typia
has started supporting Protocol Buffer, and planning to support much more binary related protocols like CBOR
and MessagePack
(#514), I've wrapped JSON related functions into a json
namespace. Also, Protocol Buffer functions are capsuled into the protobuf
namespace.
I'm sorry for users about such critical changes, but I believe you can easily adjust to it. Also, such namespace wrapping would make typia
much efficient especially for extensionability in future.
Shrink of Comment Tags
https://typia.io/docs/validators/tags/#comment-tags
Until v4 version, comment tags could be adjusted to a element (value) type of Array
and Map
.
However, since v5 version, comment tags only be applied to the target type.
What's Changed
- Add Protocol Buffers related function by @lemon-mint in #381
- Close #364 -
TSON.message()
by @samchon in #391 - Revise README content for Protocol Buffer by @samchon in #436
- Separate schema files from test programs for debugging by @samchon in #437
- Complement #364 - changed naming rule by @samchon in #446
- Complement #364 - exact array and map types by @samchon in #468
- Complement #364 - validate through
ts-proto
by @samchon in #470 - Namespace separation by @samchon in #751
- Stop detour methods of protocol buffer by @samchon in #753
- Enhanced
typia.protobuf.message<T>()
function. by @samchon in #756 typia.protobuf.encode()
functions by @samchon in #765- Add range limit for numeric types. by @samchon in #767
tyipa.protobuf.decode<T>()
functions by @samchon in #776- Exact
typia.protobuf.encode<T>()
functions for complicate union types by @samchon in #778 - Enhance
typia.random<T>()
andtypia.misc.clone<T>()
functions. by @samchon in #779 - Prepare new validation tags utilizing type. by @samchon in #781
- Type tags on validation functions by @samchon in #782
- Complete type tag supporting by @samchon in #786
- Fix typos by @logico-philosophical in #787
- Make compilation error manually by @samchon in #788
- Complement #788 - test compile errors. by @samchon in #789
- Fix typo by @nukeguys in #790
- Update documents and publish v5 by @samchon in #791
New Contributors
- @lemon-mint made their first contribution in #381
- @logico-philosophical made their first contribution in #787
- @nukeguys made their first contribution in #790
Full Changelog: v4.3.3...v5.0.0