Libraries
    Preparing search index...
    • Creates a lazy schema that defers the schema definition until first validation. Use this to define recursive/self-referential schemas.

      The getter function is called once on first use and the result is cached. You must provide an explicit TypeScript type annotation on the variable holding the outer schema — TypeScript cannot infer recursive types automatically.

      Type Parameters

      • TResult

      Parameters

      • getter: () => SchemaBuilder<TResult, any, any>

        A function that returns the schema to use for validation.

      Returns LazySchemaBuilder<TResult, true, false, false, {}>

      // Tree structure
      type TreeNode = { value: number; children: TreeNode[] };

      const treeNode: SchemaBuilder<TreeNode, true> = object({
      value: number(),
      children: array(lazy(() => treeNode))
      });
      // Optional recursive field (submenu)
      type MenuItem = { label: string; submenu?: MenuItem[] };

      const menuItem: SchemaBuilder<MenuItem, true> = object({
      label: string(),
      submenu: array(lazy(() => menuItem)).optional()
      });