-
Notifications
You must be signed in to change notification settings - Fork 143
Opt-in PropTypes implementation #24
Comments
Great feature. It can probably be prototyped as a library, maybe one that provides a decorator-like function? It could patch the component's Something like: local MyComponent = Roact.Component:extend("MyComponent")
local propTypes = {
foo = "Vector3",
bar = PropTypes.oneOf("Vector3", "CFrame"),
}
-- implementation of MyComponent
-- We can patch the existing class object...
PropTypes.apply(MyComponent, propTypes)
-- ...or we can create a new component and avoid mutating this one!
MyComponent = PropTypes.apply(MyComponent, propTypes)
return MyComponent Something I'd like to be able to express with types is "the set of all properties valid for this Roblox instance type." This lines up with a pattern I've noticed in Roact: local defaultProps = {
-- 'Legacy' is an awful default font!
Font = Enum.Font.SourceSans,
BorderSizePixel = 0,
BackgroundColor3 = Color3.new(1, 1, 1),
}
local function TextLabel(props)
return Roact.createElement("TextLabel", merge(defaultProps, props))
end |
Super prototype: https://github.com/AmaranthineCodices/rbx-prop-types Docs here: https://amaranthinecodices.github.io/rbx-prop-types/ tl;dr example: local someRules = {
requiredString = PropTypes.string,
optionalString = PropTypes.string.optional,
shaped = PropTypes.shape {
num = PropTypes.number,
udim = PropTypes.UDim,
sub = PropTypes.shape {
a = PropTypes.string,
b = PropTypes.boolean
}
}
}
local someData = {
requiredString = "hello, world!",
-- optionalString not specified - it's optional!
-- this shouldn't be here and will cause validation to fail in strict mode!
unknown = 1,
shaped = {
num = 1,
udim = UDim.new(0, 1),
sub = {
a = "hi",
b = 1,
}
},
}
-- you can use `assert` to throw errors when validation fails
assert(PropTypes.validate(someData, someRules)) I want to only do property checking when debug is enabled but I can't access that from outside Roact (#25), so for now it's just on every property change. |
This is still on my mind! I like going the route of making things required by default, but I'm not sure the best technique to actually structure the |
I've found a lot of uses for |
I suspect that this will now be very trivial to effectively toggle with the new global configuration API! |
This is next on my plate after #79 merges. |
It would be nice to have a feature similar to React's PropTypes functionality.
Overview
Each stateful component may have a key defined on it - with a symbol, ideally, to avoid name collisions - with a table. This table is structured like this:
When the stateful component's
props
table is changed andDEBUG_ENABLE
has been called, Roact will check each key in theprops
table, match it to the corresponding value in the property types table, and error if the types differ.The property types need not be limited to simple
typeof(value) == expectedType
expressions. Custom rules that provide more granular type checking allow for checking if, for example, a table is an array (e.g.ThirdProperty
in the above example).Type rules
A "type rule" is the value of an entry in the property types table. It can be one of three possible types:
typeof
.true
orfalse
, with an optional second return value explaining why the value was rejected (unused if the function returnstrue
).Some built-in rules will be provided, which will usually be functions. Usually these functions will return the actual validators themselves, e.g.
PropTypes.enumOf("Font")
would return a validator function that returnstrue
if and only if the value is an EnumItem from the Font Enum.Holes
The text was updated successfully, but these errors were encountered: