c0balt60/validate_tree
v0.1.3 ·
Promises a given tree of objects exists within an object
validate-tree
Validates a rojo-esque tree definition like so:
Demo
local projectTree = {
["$className"] = "Folder",
NumItems = {
["$className"] = "IntValue",
Data = "Folder", -- This is shorthand for { ["$className"] = "Folder" }
}
}
function g(o)
o.NumItems.Value += 1
return 0
end
function f(o: Instance)
if validateTree(o, projectTree) then
print(o.NumItems.Data:GetFullName()) -- Good
g(o) -- Good
end
o.NumItems.Data:GetFullName() -- error!
g(o) -- error!
promiseTree(o, projectTree):andThen(function(project)
print(project.NumItems.Value)
end)
end
The first parameter must be an Instance (or extend from it). The second parameter must be an object tree similar to ones considered valid by Rojo. Every tree must have a $className member, and can have any number of keys which represent the name of a child instance, which should have a corresponding value which is this same kind of tree. There is also a shorthand syntax, seen above with Folder: "Folder", which is equivalent to Folder: { $className: "Folder" }.