@rbxts/validate-tree
TypeScript icon, indicating that this package has built-in type declarations

2.0.2 • Public • Published

validate-tree

Validates a rojo-esque tree definition, like so:

Demo

const projectTree = {
	$className: "Folder",
	NumItems: {
		$className: "IntValue",
		Data: "Folder", // This is a shorthand for { $className: "Folder" }
	},
} as const;

function g(o: EvaluateInstanceTree<typeof projectTree>) {
	return o.NumItems.Value++;
}

function f(o: Instance) {
	if (validateTree(o, projectTree)) {
		print(o.NumItems.Data.GetFullName()); // good
		g(o); // good!
	}
	o.NumItems.Data.GetFullName(); // error!
	g(o); // error!

	promiseTree(o, projectTree).then(project => {
		print(project.NumItems.Value)
	})
}

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" }.

Note: Currently, the as const may be necessary to preserve the true type of the object tree. Your types will not work if you do not use the tree object in-line or declare it with as const after it.

This library also exports EvaluateInstanceTree if you want to use it for your own nefarious purposes.

Dependencies (0)

    Dev Dependencies (2)

    Package Sidebar

    Install

    npm i @rbxts/validate-tree

    Weekly Downloads

    108

    Version

    2.0.2

    License

    ISC

    Unpacked Size

    8.34 kB

    Total Files

    4

    Last publish

    Collaborators

    • validark