4.0 KiB
Home | Installation | Usage | Types | Values | Syntax | TypeRegistry | TypeCheck
TypeCheck
TypeBox types target Json Schema Draft 7 and are compatible with any validator that supports this specification. TypeBox also provides a built-in type checking compiler designed specifically for TypeBox types that offers high performance compilation and value checking.
The following sections detail using Ajv and the TypeBox compiler infrastructure.
Ajv
The following shows the recommended setup for Ajv.
$ npm install ajv ajv-formats --save
import { Type } from "@alkdev/typebox";
import addFormats from "ajv-formats";
import Ajv from "ajv";
const ajv = addFormats(new Ajv({}), [
"date-time",
"time",
"date",
"email",
"hostname",
"ipv4",
"ipv6",
"uri",
"uri-reference",
"uuid",
"uri-template",
"json-pointer",
"relative-json-pointer",
"regex",
]);
const validate = ajv.compile(
Type.Object({
x: Type.Number(),
y: Type.Number(),
z: Type.Number(),
})
);
const R = validate({ x: 1, y: 2, z: 3 }); // const R = true
TypeCompiler
The TypeBox TypeCompiler is a high performance JIT validation compiler that transforms TypeBox types into optimized JavaScript validation routines. The compiler is tuned for fast compilation as well as fast value assertion. It is built to serve as a validation backend that can be integrated into larger applications. It can also be used for code generation.
The TypeCompiler is provided as an optional import.
import { TypeCompiler } from "@alkdev/typebox/compiler";
Use the Compile function to JIT compile a type. Note that compilation is generally an expensive operation and should only be performed once per type during application start up. TypeBox does not cache previously compiled types, and applications are expected to hold references to each compiled type for the lifetime of the application.
const C = TypeCompiler.Compile(
Type.Object({
// const C: TypeCheck<TObject<{
x: Type.Number(), // x: TNumber;
y: Type.Number(), // y: TNumber;
z: Type.Number(), // z: TNumber;
})
); // }>>
const R = C.Check({ x: 1, y: 2, z: 3 }); // const R = true
Use the Errors function to generate diagnostic errors for a value. The Errors function will return an iterator that when enumerated; will perform an exhaustive check across the entire value yielding any error found. For performance, this function should only be called after a failed Check. Applications may also choose to yield only the first value to avoid exhaustive error generation.
const C = TypeCompiler.Compile(
Type.Object({
// const C: TypeCheck<TObject<{
x: Type.Number(), // x: TNumber;
y: Type.Number(), // y: TNumber;
z: Type.Number(), // z: TNumber;
})
); // }>>
const value = {};
const first = C.Errors(value).First(); // const first = {
// schema: { type: 'number' },
// path: '/x',
// value: undefined,
// message: 'Expected number'
// }
const all = [...C.Errors(value)]; // const all = [{
// schema: { type: 'number' },
// path: '/x',
// value: undefined,
// message: 'Expected number'
// }, {
// schema: { type: 'number' },
// path: '/y',
// value: undefined,
// message: 'Expected number'
// }, {
// schema: { type: 'number' },
// path: '/z',
// value: undefined,
// message: 'Expected number'
// }]
Code
Use the Code function to generate assertion functions as strings. This function can be used to generate code that can be written to disk as importable modules. This technique is sometimes referred to as Ahead of Time (AOT) compilation. The following generates code to check a string.
const C = TypeCompiler.Code(Type.String()); // const C = `return function check(value) {
// return (
// (typeof value === 'string')
// )
// }`
Back to Home