Json Schema Type Builder with Static Type Resolution for TypeScript
—
Additional TypeBox namespaces that provide specialized functionality for parsing, system configuration, and module management.
Schema parsing and validation utilities for working with external schema definitions.
/**
* Runtime schema parsing utilities
*/
namespace Runtime {
function Parse<T extends TSchema>(schema: T, value: unknown): Static<T>;
function Check<T extends TSchema>(schema: T, value: unknown): value is Static<T>;
}
/**
* Static schema parsing utilities
*/
namespace Static {
function Parse<T extends TSchema>(schema: T, value: unknown): Static<T>;
function Check<T extends TSchema>(schema: T, value: unknown): value is Static<T>;
}Usage Examples:
import { Runtime, Static } from "@sinclair/typebox/parser";
const UserSchema = Type.Object({
name: Type.String(),
age: Type.Number()
});
// Runtime parsing
const runtimeUser = Runtime.Parse(UserSchema, userData);
// Static parsing
const staticUser = Static.Parse(UserSchema, userData);System-level configuration and policy management for TypeBox behavior.
/**
* TypeSystem policy configuration
*/
interface TypeSystemPolicy {
/** Allow numeric strings in number validation */
allowNumericString: boolean;
/** Allow array-like objects in array validation */
allowArrayLike: boolean;
/** Allow void undefined policy */
allowVoidUndefined: boolean;
}
/**
* Configure TypeSystem policies
* @param policy - Policy configuration object
*/
function Policy(policy: Partial<TypeSystemPolicy>): void;
/**
* Get current TypeSystem policy
* @returns Current policy configuration
*/
function GetPolicy(): TypeSystemPolicy;Usage Examples:
import { TypeSystem } from "@sinclair/typebox/system";
// Configure system behavior
TypeSystem.Policy({
allowNumericString: true, // "123" validates as number
allowArrayLike: false, // Only true arrays allowed
allowVoidUndefined: true // void can be undefined
});
// Check current policy
const currentPolicy = TypeSystem.GetPolicy();
console.log(currentPolicy.allowNumericString); // trueCreates module types for complex schema organization and namespace management.
/**
* Creates a module type for schema organization
* @param properties - Module property schemas
* @param options - Schema options
* @returns TModule schema
*/
function Module<T extends TProperties>(properties: T, options?: SchemaOptions): TModule<T>;Usage Examples:
// Define a module with related schemas
const UserModule = Type.Module({
User: Type.Object({
id: Type.String(),
name: Type.String(),
email: Type.String({ format: 'email' })
}),
CreateUserRequest: Type.Object({
name: Type.String(),
email: Type.String({ format: 'email' })
}),
UpdateUserRequest: Type.Partial(Type.Object({
name: Type.String(),
email: Type.String({ format: 'email' })
}))
});
// Use module schemas
type User = Static<typeof UserModule.properties.User>;
type CreateUserRequest = Static<typeof UserModule.properties.CreateUserRequest>;Advanced recursive type definitions for self-referencing schemas.
/**
* Creates recursive schema types
* @param callback - Function defining recursive structure
* @param options - Schema options
* @returns TRecursive schema
*/
function Recursive<T extends TSchema>(
callback: (thisType: TThis) => T,
options?: SchemaOptions
): TRecursive<T>;
/**
* Reference to self in recursive definitions
*/
interface TThis extends TSchema {
[Kind]: 'This';
}Usage Examples:
// Self-referencing tree structure
const TreeNode = Type.Recursive(This => Type.Object({
value: Type.String(),
children: Type.Array(This)
}));
// Linked list structure
const LinkedList = Type.Recursive(This => Type.Object({
data: Type.Any(),
next: Type.Union([This, Type.Null()])
}));
// JSON-like recursive structure
const JsonValue = Type.Recursive(This => Type.Union([
Type.String(),
Type.Number(),
Type.Boolean(),
Type.Null(),
Type.Array(This),
Type.Object({}, { additionalProperties: This })
]));Schema references for modular schema composition and reuse.
/**
* Creates a reference to another schema by ID
* @param ref - Schema reference ID
* @param options - Schema options
* @returns TRef schema
*/
function Ref<T extends TSchema = TSchema>(ref: string, options?: SchemaOptions): TRef<T>;
/**
* Creates a schema with an ID for referencing
* @param id - Schema identifier
* @param schema - Schema to identify
* @returns Schema with $id property
*/
function Id<T extends TSchema>(id: string, schema: T): T & { $id: string };Usage Examples:
// Define reusable schemas with IDs
const PersonSchema = Type.Id('Person', Type.Object({
name: Type.String(),
age: Type.Number()
}));
const AddressSchema = Type.Id('Address', Type.Object({
street: Type.String(),
city: Type.String(),
country: Type.String()
}));
// Reference schemas by ID
const CustomerSchema = Type.Object({
person: Type.Ref<typeof PersonSchema>('Person'),
address: Type.Ref<typeof AddressSchema>('Address'),
customerSince: Type.Date()
});
// Use with references array in validation
const isValid = Value.Check(CustomerSchema, customerData, [
PersonSchema,
AddressSchema
]);interface TModule<T extends TProperties> extends TSchema {
type: 'object';
properties: T;
[Kind]: 'Module';
}
interface TRecursive<T extends TSchema> extends TSchema {
[Kind]: 'Recursive';
}
interface TThis extends TSchema {
[Kind]: 'This';
}
interface TRef<T extends TSchema = TSchema> extends TSchema {
$ref: string;
[Kind]: 'Ref';
}
interface TypeSystemPolicy {
allowNumericString: boolean;
allowArrayLike: boolean;
allowVoidUndefined: boolean;
}Install with Tessl CLI
npx tessl i tessl/npm-sinclair--typebox