random()
function
undefined
export function random<T>(g?: IRandomGenerator): Resolved<T>;
You can make every random data just by calling typia.random<T>()
function.
When you call the typia.random<T>()
function, typia
will analyze your type T
, and writes optimal random generation code for the type T
, in the compilation level. This is called AOT (Ahead of Time) compilation, and you may understand what it is just by reading below example code.
TypeScript Source Code
import typia, { tags } from "typia";
const member: IMember = typia.random<IMember>();
console.log(member);
interface IMember {
id: string & tags.Format<"uuid">;
email: string & tags.Format<"email">;
age: number &
tags.Type<"uint32"> &
tags.ExclusiveMinimum<19> &
tags.Maximum<100>;
}
Reusable function
undefined
export function createRandom<T>(): (g?: IRandomGenerator) => Resolved<T>;
Special Tags
Runtime validators of typia
provides additional type checking logic through Type Tags and Comment Tags. typia.random<T>()
function also like that. typia.random<T>()
function can utilize those tags to specialize the behavior of random data generation.
For reference, whether you choose Type Tags or Comment Tags. typia.random<T>()
, it is not a matter for typia.random<T>()
function. Below two TypeScript codes are generating exactly same JavaScript code. Therefore, you can choose whatever you want considering your preference.
TypeScript (Type Tags)
import typia, { tags } from "typia";
const data: TypeTag = typia.random<TypeTag>();
console.log(data);
interface TypeTag {
type: number & tags.Type<"int32">;
number?: number & tags.ExclusiveMinimum<19> & tags.Maximum<100>;
string: string & tags.MinLength<3>;
pattern: string & tags.Pattern<"^[a-z]+$">;
format: (string & tags.Format<"date-time">) | null;
}
Customization
undefined
export function random<T>(g?: IRandomGenerator): Resolved<T>;
You can add custom type tags for random data generation.
As above IRandomGenerator.CustomMap
has a little bit complicate type, it may hard to understand for newcomers. However, such newcomers may easily understand, how to customize the random generation, just by reading the following example.
Just define custom type tags like below, then everything would be done.
For reference, when defining custom type tag, typia
enforces user to define validate
function literal for type safety. Never forget it when you define custom type tags for random generation. Such validation logic definition may enhance your random data generator logic when combining with typia.assert<T>()
function.
TypeScript Source Code
import typia from "typia";
import { _randomNumber } from "typia/lib/internal/_randomNumber.js";
import { _randomString } from "typia/lib/internal/_randomString.js";
const data: TagCustom = typia.random<TagCustom>({
string: (schema) => {
if ((schema as any)["x-typia-monetary"] === "dollar")
return "$" + Math.floor(Math.random() * 1_000);
else if ((schema as any)["x-typia-postfix"] !== undefined)
return _randomString(schema) + (schema as any)["x-typia-postfix"];
return _randomString(schema);
},
number: (schema) => {
if ((schema as any)["x-typia-powerOf"] !== undefined) {
const powerOf = (schema as any)["x-typia-powerOf"];
return Math.pow(powerOf, Math.floor(Math.random() * 10) + 1);
}
return _randomNumber(schema);
},
});
console.log(data);
interface TagCustom {
id: string & typia.tags.Format<"uuid">;
dollar: string & Dollar;
postfix: string & Postfix<"abcd">;
powerOf: number & PowerOf<2>;
}
type Dollar = typia.tags.TagBase<{
kind: "monetary";
target: "string";
value: "dollar";
validate: `$input[0] === "$" && !isNaN(Number($input.substring(1).split(",").join("")))`;
schema: {
"x-typia-monetary": "dollar";
};
}>;
type Postfix<Value extends string> = typia.tags.TagBase<{
kind: "postfix";
target: "string";
value: Value;
validate: `$input.endsWith("${Value}")`;
schema: {
"x-typia-postfix": Value;
};
}>;
type PowerOf<Value extends number> = typia.tags.TagBase<{
kind: "powerOf";
target: "number";
value: Value;
validate: `(() => {
const denominator: number = Math.log(${Value});
const value: number = Math.log($input) / denominator;
return Math.abs(value - Math.round(value)) < 0.00000001;
})()`;
schema: {
"x-typia-powerOf": Value;
};
}>;