stcobbe
stcobbe7mo ago

Self-reference in validators

Hi folks, Is it possible to somehow use self-references in validators, e.g.:
export const folderValidator = v.object({
id: v.number(),
files: v.array(fileValidator),
subFolders: v.array(folderValidator),
});
export const folderValidator = v.object({
id: v.number(),
files: v.array(fileValidator),
subFolders: v.array(folderValidator),
});
Doing this now results in TypeScript error Variable 'folderValidator' is used before being assigned. Please let me know if I can provide additional context. Thank you!
2 Replies
ballingt
ballingt7mo ago
No, this isn't possible. You can either use IDs like foreign keys or validate this shape manually and use v.any() for the DB validator.
stcobbe
stcobbeOP7mo ago
Got it, thank you @ballingt

Did you find this page helpful?