Create Circular References
How do I make a validator schema reference itself?
interface LinkedListNode {
value: unknown
next: LinkedListNode | null
}const linkedListNodeValidator = validator`{
value: unknown
// Error: Cannot access 'linkedListNodeValidator' before initialization
next: ${linkedListNodeValidator} | null
}`;const linkedListNodeValidator = validator`{
value: unknown
next: ${validator.lazy(() => linkedListNodeValidator)} | null
}`;
// Example valid data
linkedListNodeValidator.assertMatches({
value: 1,
next: {
value: 2,
next: null,
},
});Last updated