JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 144
  • Score
    100M100P100Q71089F
  • License ISC

Validates whether a given instance matches a given instance tree

Package Exports

  • @rbxts/validate-tree
  • @rbxts/validate-tree/init.lua

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@rbxts/validate-tree) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

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.