1.3 KiB
1.3 KiB
Home | Installation | Usage | Types | Values | Syntax | TypeRegistry | TypeCheck
TypeRegistry
The TypeBox type system can be extended with additional types and formats using the TypeRegistry and FormatRegistry modules. These modules integrate deeply with TypeBox's internal type checking infrastructure and can be used to create application specific types, or register schematics for alternative specifications.
TypeRegistry
Use the TypeRegistry to register a type. The Kind must match the registered type name.
import { TSchema, Kind, TypeRegistry } from "@alkdev/typebox";
TypeRegistry.Set("Foo", (schema, value) => value === "foo");
const Foo = { [Kind]: "Foo" } as TSchema;
const A = Value.Check(Foo, "foo"); // const A = true
const B = Value.Check(Foo, "bar"); // const B = false
FormatRegistry
Use the FormatRegistry to register a string format.
import { FormatRegistry } from "@alkdev/typebox";
FormatRegistry.Set("foo", (value) => value === "foo");
const T = Type.String({ format: "foo" });
const A = Value.Check(T, "foo"); // const A = true
const B = Value.Check(T, "bar"); // const B = false
Back to Home