Rod

Lazy Evaluation

Handling recursive and self-referencing schemas.

Lazy Evaluation

Lazy evaluation allows you to define recursive schemas where a schema references itself.

use rod_rs::{lazy, rod_obj, string, array, RodValidator};

// Recursive Tree structure
let schema = lazy(|| Box::new(rod_obj! {
    name: string(),
    children: array(lazy(|| Box::new(schema.clone())))
}));
import { rod } from 'rod-js';

const Category = rod.lazy(() => rod.object({
    name: rod.string(),
    subcategories: rod.array(Category)
}));
import rod

category = rod.lazy(lambda: rod.object({
    "name": rod.string(),
    "subcategories": rod.array(category)
}))

Rod enforces a default recursion limit of 256 to prevent stack overflows. Cyclic data structures that exceed this depth will fail validation.

On this page