[Home](./readme.md) | [Installation](./installation.md) | [Usage](./usage.md) | [Types](./types/) | [Values](./values/) | [Syntax](./syntax/) | [TypeRegistry](./type-registry.md) | [TypeCheck](./type-check.md) # 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. ```bash $ npm install ajv ajv-formats --save ``` ```typescript 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. ```typescript 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. ```typescript const C = TypeCompiler.Compile( Type.Object({ // const C: TypeCheck> 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. ```typescript const C = TypeCompiler.Compile( Type.Object({ // const C: TypeCheck> 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. ```typescript const C = TypeCompiler.Code(Type.String()); // const C = `return function check(value) { // return ( // (typeof value === 'string') // ) // }` ``` Back to [Home](../readme.md)