JSPM

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

Validates whether a given instance matches a given instance tree

Package Exports

  • @rbxts/validate-tree

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: EvaluateTree<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!
}

yieldForTree(game.GetService("Workspace"), {
    $className: "Workspace",

    SpawnLocation: {
        $className: "SpawnLocation",

        Decal: {
            $className: "Decal",

            Value: {
                $className: "IntValue",
                Folder: "Folder",
            },
        },
    },
} as const).then(workspace => {
    print(++workspace.SpawnLocation.Decal.Value.Value);
    const count = workspace.SpawnLocation.Decal.Value.Clone();
    count.Parent = workspace;

    // Members are preserved by the `Clone` method!
    print((count.Folder.Name = "Soup"), ++count.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 is necessary to preserve the true type of the object tree. Your code will not work if you do not use it.

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

Note: This library doesn't mutate/modify anything, it only yields until the tree is fully detected.