Schemas
Every builder and function on this page is in VideState.schemas. The examples keep it in a local, const schemas = VideState.schemas, and call schemas.float() and schemas.new({ ... }), which keeps rules short.
A schema says which fields a component has and which values each field accepts. VideState has two kinds:
- A field builder, such as
floatorudim2, describes one value. It returns aSchema<T>for a Luau typeT. - A rule schema is the set of named fields of a component. You make it with
schemas.new, from a table of field builders, and pass it torules.dynamicorrules.static.
field builders ──> schemas.new ──> schemas.withDefaults / schemas.extend ──> rules.dynamic / rules.static ──> rule.newYou never call a schema yourself. The rule checks the values of every state against its schema when a handler is created, and a value that does not match raises an error that names the state, the field, and what was wrong. Every builder checks its own options when it is called, so a wrong option fails where it is written.
Rule schemas
A rule schema is checked once, when it is made, and it is frozen, so it can be shared. One schema can make any number of rules, dynamic and static, and the functions below make a new schema from an existing one without changing it.
const VideState = require("@VideState")
const schemas = VideState.schemas
const rules = VideState.rules
-- 1. A schema: the fields, checked once when it is made.
const ButtonSchema = schemas.new({
alpha = schemas.float({ min = 0, max = 1 }),
size = schemas.udim2(),
visible = schemas.boolean(),
})
-- 2. Defaults: a new schema where states may leave these fields out.
const DefaultedButton = schemas.withDefaults(ButtonSchema, {
alpha = 1,
visible = true,
})
-- 3. Inheritance: the base fields and new ones. A field defined twice is an error.
const IconButtonSchema = schemas.extend(DefaultedButton, {
iconRotation = schemas.withDefault(schemas.float(), 0),
iconColor = schemas.color3(),
})
-- 4. Rules take a schema.
const IconButtonRule = rules.dynamic(IconButtonSchema, {
name = "IconButton",
tags = { "Buttons" },
})
const FixedIconButtonRule = rules.static(IconButtonSchema, {
name = "FixedIconButton",
})
return {
IconButtonRule = IconButtonRule,
FixedIconButtonRule = FixedIconButtonRule,
}A handler of either rule may leave out alpha, visible, and iconRotation, because they have defaults. It must give size and iconColor in every state. See Default values for how a handler fills them in.
Functions, not methods
schemas.new, schemas.extend, and schemas.withDefaults are functions of the VideState.schema namespace, and a schema has no methods. Pass the schema as the first argument.
schemas.new Added in 0.8.0
VideState.schemas.new(
fields: { [string]: Schema<any> } -- One field builder per field name
) -> RuleSchema<Fields>Makes a rule schema from a table of named field builders. Every field is checked now, and the table is copied, so a later change to it does not reach the schema. The field types are kept, so a handler of a rule made from the schema infers the type of each source.
const VideState = require("@VideState")
const schemas = VideState.schemas
const rules = VideState.rules
const PanelSchema = schemas.new({
alpha = schemas.float({ min = 0, max = 1 }),
position = schemas.udim2(),
title = schemas.string({ maxLength = 40 }),
})
return rules.dynamic(PanelSchema, { name = "Panel" })Parameters
| Name | Type | Description |
|---|---|---|
fields | { [string]: Schema``<any> } | The field builder of each field, keyed by field name. Keys must be strings, so arrays are rejected. An empty table is allowed. |
Returns
| Type | Description |
|---|---|
RuleSchema<Fields> | A frozen rule schema with these fields. |
Errors
| Cause | Message | |
|---|---|---|
| Error | fields is not a table | schemas.new expects a table of field schemas, got <type> |
| Error | fields is already a rule schema | schemas.new expects a table of fields, and this is already a schema |
| Error | A field name is not a string | schemas.new expects string field names, got <type> |
| Error | A field is not a field builder | schemas.new expects a schema for field "<field>" |
schemas.withDefaults Added in 0.8.0
VideState.schemas.withDefaults(
schema: RuleSchema<Fields>, -- The schema to start from
defaults: { [string]: unknown } -- The default of each field that gets one, by field name
) -> RuleSchema<Fields>Makes a new rule schema where the named fields have a default, so a state may leave them out. Each name in defaults must be a field of schema, and that field is wrapped as withDefault would. A field that defaults does not name is left as it is, and schema does not change. The field types stay the same.
Use it to give defaults to fields of a schema you did not write, or to make two versions of one schema. To give a default to a field you are writing, withDefault in the field table is shorter.
const VideState = require("@VideState")
const schemas = VideState.schemas
const rules = VideState.rules
const PanelSchema = schemas.new({
alpha = schemas.float({ min = 0, max = 1 }),
scale = schemas.float({ min = 0 }),
visible = schemas.boolean(),
})
-- States of this rule only need to give visible.
const DefaultedPanel = schemas.withDefaults(PanelSchema, { alpha = 1, scale = 1 })
return rules.dynamic(DefaultedPanel, { name = "Panel" })Parameters
| Name | Type | Description |
|---|---|---|
schema | RuleSchema<Fields> | The schema to start from. |
defaults | { [string]: unknown } | The default of each field that gets one. Each must be a plain value that the field accepts. |
Returns
| Type | Description |
|---|---|
RuleSchema<Fields> | A new schema with the same fields and the defaults applied. |
Errors
| Cause | Message | |
|---|---|---|
| Error | schema is not a rule schema | schemas.withDefaults expects a schema made with VideState.schemas.new, got <type> |
| Error | defaults is not a table | schemas.withDefaults expects a table of default values by field name, got <type> |
| Error | A name in defaults is not a field | schemas.withDefaults names fields that do not exist: <names> |
| Error | A default is wrong for its field | field "<field>": followed by the withDefault error |
schemas.extend Added in 0.8.0 Changed in 0.9.0
VideState.schemas.extend(
base: RuleSchema<Base>, -- The schema to build on
fields: { [string]: Schema<any> } | RuleSchema<Fields> -- The new fields, or another schema
) -> RuleSchema<Base & Fields>Makes a new rule schema with every field of base and the new fields. fields is a table of field builders, or another rule schema, which merges the two schemas. Every field keeps its default. A field that base already has raises an error, because a field that changes under the same name would break the states written for the base. Neither argument changes. The types of both sets of fields are kept.
const VideState = require("@VideState")
const schemas = VideState.schemas
const ButtonSchema = schemas.new({
alpha = schemas.float({ min = 0, max = 1 }),
size = schemas.udim2(),
})
const IconButtonSchema = schemas.extend(ButtonSchema, {
iconColor = schemas.color3(),
})
return { ButtonSchema = ButtonSchema, IconButtonSchema = IconButtonSchema }To share a group of fields between schemas that do not build on each other, make it a schema of its own and merge it:
const VideState = require("@VideState")
const schemas = VideState.schemas
-- The fields every card has, with a default for the corner radius.
const CardFields = schemas.withDefaults(schemas.new({
cornerRadius = schemas.udim(),
strokeTransparency = schemas.float({ min = 0, max = 1 }),
}), { cornerRadius = UDim.new(0, 8) })
const ProfileCardSchema = schemas.extend(schemas.new({
avatarSize = schemas.udim2(),
}), CardFields)
const ShopCardSchema = schemas.extend(schemas.new({
priceColor = schemas.color3(),
}), CardFields)
return { ProfileCardSchema = ProfileCardSchema, ShopCardSchema = ShopCardSchema }schemas.extend builds a schema only. To build a rule on another rule, with its state names, transitions, and tags, use rules.extendDynamic or rules.extendStatic, which take a rule schema of extra fields.
Parameters
| Name | Type | Description |
|---|---|---|
base | RuleSchema<Base> | The schema to build on. |
fields | { [string]: Schema``<any> } or RuleSchema<Fields> | The new fields, as a plain table of field builders, as schemas.new takes them, or another rule schema to merge. |
Returns
| Type | Description |
|---|---|
RuleSchema<Base & Fields> | A new schema with the fields of both. |
Errors
| Cause | Message | |
|---|---|---|
| Error | base is not a rule schema | schemas.extend expects a schema made with VideState.schemas.new, got <type> |
| Error | fields is not a table, or a field is not a field builder | The same messages as schemas.new, starting with schemas.extend |
| Error | A field is already in base, including a field of a schema passed as fields | schemas.extend cannot redefine fields of the base schema: <names> |
Primitives
Field builders for Luau values.
boolean Added in 0.1.0 Changed in 0.8.0
VideState.schemas.boolean() -> Schema<boolean>Accepts true or false, and rejects every other type.
Returns
| Type | Description |
|---|---|
Schema``<boolean> | A schema for booleans. |
integer Added in 0.1.0 Changed in 0.8.0
VideState.schemas.integer(
options: {
min: number?, -- Smallest allowed value, inclusive
max: number? -- Largest allowed value, inclusive
}?
) -> Schema<number>Accepts finite whole numbers.
Parameters
| Name | Type | Description |
|---|---|---|
options | IntegerOptions? | Optional. Bounds for the value. |
options.min | number? | Optional. The smallest allowed value, inclusive. Must be an integer. No lower bound by default. |
options.max | number? | Optional. The largest allowed value, inclusive. Must be an integer. No upper bound by default. |
Returns
| Type | Description |
|---|---|
Schema``<number> | A schema for whole numbers within the bounds. |
Errors
| Cause | Message | |
|---|---|---|
| Error | options is not a table | expected Integer options to be a table |
| Error | min is not an integer | Integer minimum must be a finite integer |
| Error | max is not an integer | Integer maximum must be a finite integer |
| Error | min is greater than max | Integer minimum must not exceed maximum |
float Added in 0.1.0 Changed in 0.8.0
VideState.schemas.float(
options: {
min: number?, -- Smallest allowed value, inclusive
max: number?, -- Largest allowed value, inclusive
step: number? -- Sets how many decimal places a value may have
}?
) -> Schema<number>Accepts finite numbers, including whole numbers.
Parameters
| Name | Type | Description |
|---|---|---|
options | FloatOptions? | Optional. Bounds and precision for the value. |
options.min | number? | Optional. The smallest allowed value, inclusive. No lower bound by default. |
options.max | number? | Optional. The largest allowed value, inclusive. No upper bound by default. |
options.step | number? | Optional. A positive, finite number. Its decimal places set how many decimal places a value may have, so 0.01 allows up to two. No limit by default. |
Returns
| Type | Description |
|---|---|
Schema``<number> | A schema for finite numbers within the bounds and precision. |
Errors
| Cause | Message | |
|---|---|---|
| Error | options is not a table | expected Float options to be a table |
| Error | min is not finite | Float minimum must be finite |
| Error | max is not finite | Float maximum must be finite |
| Error | step is not a positive, finite number | Float step must be positive and finite |
| Error | step has too many decimal places to validate | Float step precision is too small to validate |
| Error | min is greater than max | Float minimum must not exceed maximum |
string Added in 0.1.0 Changed in 0.8.0
Strings are not transitioned
A string field cannot be tweened by onTransition. When the state changes, it switches to the new text at the start of the transition. See Transitions.
VideState.schemas.string(
options: {
maxLength: number?, -- Maximum length in Unicode code points
truncate: boolean? -- Cut longer strings instead of rejecting them
}?
) -> Schema<string>Accepts strings.
Parameters
| Name | Type | Description |
|---|---|---|
options | StringOptions? | Optional. Length limits for the value. |
options.maxLength | number? | Optional. The maximum length, counted in Unicode code points rather than bytes. When set, strings with invalid UTF-8 are rejected. No limit by default. |
options.truncate | boolean? | Optional. When true, longer strings are cut down to maxLength instead of rejected. Defaults to false. |
Returns
| Type | Description |
|---|---|
Schema``<string> | A schema for strings. |
Errors
| Cause | Message | |
|---|---|---|
| Error | options is not a table | expected String options to be a table |
| Error | maxLength is not a non-negative integer | String maxLength must be a finite integer greater than or equal to 0 |
| Error | truncate is not a boolean | String truncate must be a boolean |
enum Added in 0.1.0 Changed in 0.8.0
VideState.schemas.enum(
...: string -- The accepted values
) -> Schema<string>Accepts only the strings passed as arguments. Matching is case-sensitive.
Parameters
| Name | Type | Description |
|---|---|---|
... | string | The accepted values. At least one is required. |
Returns
| Type | Description |
|---|---|
Schema``<string> | A schema that accepts one of the given strings. |
Errors
| Cause | Message | |
|---|---|---|
| Error | No values are given | Enum requires at least one string value |
| Error | An argument is not a string | expected Enum argument <n> to be String, got <type> |
optional Added in 0.1.0 Changed in 0.8.0
VideState.schemas.optional(
schema: Schema<T> -- The schema for the value when it is not nil
) -> Schema<T?>Accepts nil and any value the inner schema accepts. A Vide source that holds nil is also accepted.
Parameters
| Name | Type | Description |
|---|---|---|
schema | Schema``<T> | The schema to apply when the value is not nil. |
Returns
| Type | Description |
|---|---|
Schema``<T?> | A schema that accepts nil or a value accepted by schema. |
union Added in 0.5.0 Changed in 0.8.0
VideState.schemas.union(
first: Schema<A>, -- The schema that is tried first
second: Schema<B> -- The schema that is tried when the first one rejects the value
) -> Schema<A | B>Accepts any value that first or second accepts. The first schema is tried first, and its result is used when it accepts the value. A Vide source that holds a value of either schema is also accepted.
const VideState = require("@VideState")
const schemas = VideState.schemas
-- A width that is a number of pixels or the string "Auto".
const width = schemas.union(schemas.float({ min = 0 }), schemas.enum("Auto"))To accept more than two schemas, nest the union: VideState.schemas.union(a, VideState.schemas.union(b, c)).
Parameters
| Name | Type | Description |
|---|---|---|
first | Schema``<A> | The schema to try first. |
second | Schema``<B> | The schema to try when first rejects the value. |
Returns
| Type | Description |
|---|---|
| `Schema``<A | B>` |
Errors
| When | Message | |
|---|---|---|
| Error | An argument is not a schema | expected the first union schema to be a schema, and the same for the second |
A union does not tween between different types
A transition blends the old and the new value only when both have the same animatable type. When the types differ, or the type cannot be animated, the field switches at the start: the new value is sent on the first frame and does not move after that. A union cannot animate from 100 to "Auto".
| From | To | Result |
|---|---|---|
100 | 200 | Tweens, as two numbers. |
UDim2 | UDim2 | Tweens, as two UDim2 values. |
100 | "Auto" | Switches at the start, because a number and a string cannot be blended. |
"Auto" | "Fill" | Switches at the start, because a string cannot be animated. |
UDim2 | Color3 | Switches at the start, because the types differ. |
The union does not check which schema accepted a value, so two numbers from different branches still tween. The other fields of the same state tween as usual. To animate toward a size that the layout decides, use a number for the field and a separate state for the automatic layout.
table Added in 0.1.0 Changed in 0.8.0
VideState.schemas.table(
fields: { [string]: Schema<any> } -- One schema per field name
) -> Schema<{ [string]: any }>A field whose value is a table with exactly the listed fields, each checked by its own schema. Prefer record, which also checks the fields when it is made. A missing field is rejected unless its schema is optional. Extra keys are rejected. The accepted type is inferred from the field schemas, so Infer returns a table with the same field types.
Parameters
| Name | Type | Description |
|---|---|---|
fields | { [string]: Schema<any> } | The schema for each field, keyed by field name. Keys must be strings, so arrays are rejected. |
Returns
| Type | Description |
|---|---|
Schema``<{ [string]: any }> | A schema for tables with exactly these fields. |
Errors
| Cause | Message | |
|---|---|---|
| Error | fields is not a table | expected a table of named field schemas |
record Added in 0.1.0 Changed in 0.8.0
VideState.schemas.record(
fields: { [string]: Schema<any> } -- One schema per field name
) -> Schema<{ [string]: any }>A field whose value is a table of named values, such as { enabled = true, count = 2 }. It accepts the same values as table, and its type is inferred from the field schemas. Each field is checked when the record is created, so a field that is not a schema fails early. Fields can be any schema, including another record.
Parameters
| Name | Type | Description |
|---|---|---|
fields | { [string]: Schema<any> } | The schema for each field, keyed by field name. |
Returns
| Type | Description |
|---|---|
Schema``<{ [string]: any }> | A schema for records with exactly these fields. |
Errors
| Cause | Message | |
|---|---|---|
| Error | fields is not a table | record expects a table of field schemas, got <type> |
| Error | A field name is not a string | record expects string field names, got <type> |
| Error | A field is not a schema | record expects a schema for field "<field>" |
Default values
A default lets a state leave a field out. withDefault gives one field a default where you write it, and schemas.withDefaults gives defaults to several fields of a rule schema at once. How a handler uses them is described in Default values of the rules guide.
withDefault Added in 0.8.0
VideState.schemas.withDefault(
schema: Schema<T>, -- The field builder of the field
defaultValue: T -- The value a state gets when it leaves the field out
) -> Schema<T>Gives one field a default value. A state that leaves the field out gets a Vide source that holds the default, so the field is never nil. The result accepts what schema accepts, and the inferred type is still T, unlike optional, which allows nil and leaves the field nil.
The default is checked against schema when withDefault is called, so a wrong one fails where it is written. A table default, such as a record value, is copied and frozen, so no state can change the value that the others share.
const VideState = require("@VideState")
const schemas = VideState.schemas
const rules = VideState.rules
const PanelSchema = schemas.new({
alpha = schemas.withDefault(schemas.float({ min = 0, max = 1 }), 1),
visible = schemas.boolean(),
})
return rules.dynamic(PanelSchema, { name = "Panel" })Parameters
| Name | Type | Description |
|---|---|---|
schema | Schema``<T> | The field builder of the field. |
defaultValue | T | The default. It must be a plain value that schema accepts, not a Vide source. |
Returns
| Type | Description |
|---|---|
Schema``<T> | A field builder that accepts the same values as schema and has a default. |
Errors
| Cause | Message | |
|---|---|---|
| Error | schema is not a field builder | expected a schema to give a default value |
| Error | schema is an optional schema | withDefault cannot wrap an optional schema: ... |
| Error | schema already has a default | withDefault cannot wrap a schema that already has a default |
| Error | The default is a Vide source | the default must be a plain value, not a Vide source |
| Error | The schema rejects the default | the default does not match the schema: followed by the issues |
Roblox datatypes
Field builders for Roblox datatypes. Each of these builders takes no parameters and rejects every other type.
color3 Added in 0.1.0 Changed in 0.8.0
VideState.schemas.color3() -> Schema<Color3>Accepts a Color3.
numberRange Added in 0.4.0 Changed in 0.8.0
VideState.schemas.numberRange() -> Schema<NumberRange>Accepts a NumberRange. A transition tweens its minimum and its maximum, and keeps the minimum at or below the maximum when an easing overshoots.
numberSequence Added in 0.4.0 Changed in 0.8.0
VideState.schemas.numberSequence() -> Schema<NumberSequence>Accepts a NumberSequence, such as the Transparency of a UIGradient. A transition tweens it when both values have the same number of keypoints. Otherwise it switches to the next value at the start.
colorSequence Added in 0.4.0 Changed in 0.8.0
VideState.schemas.colorSequence() -> Schema<ColorSequence>Accepts a ColorSequence, such as the Color of a UIGradient. A transition tweens it when both values have the same number of keypoints. Otherwise it switches to the next value at the start.
udim Added in 0.1.0 Changed in 0.8.0
VideState.schemas.udim() -> Schema<UDim>Accepts a UDim.
udim2 Added in 0.1.0 Changed in 0.8.0
VideState.schemas.udim2() -> Schema<UDim2>Accepts a UDim2.
vector2 Added in 0.1.0 Changed in 0.8.0
VideState.schemas.vector2() -> Schema<Vector2>Accepts a Vector2.
Types
Infer Added in 0.1.0
type Infer<S> = -- The Luau type that the schema S acceptsGives the Luau type that a field builder accepts. For the values of a whole rule, use InferRule.
| Name | Type | Description |
|---|---|---|
S | typeof(schema) | The schema to read the accepted type from. |
RuleSchema Added in 0.8.0
type RuleSchema<Fields> = {
read _fields: Fields, -- The field builders, by field name
}A rule schema made by schemas.new, schemas.extend, or schemas.withDefaults. It is frozen. Only schemas made by those functions are accepted, so a table of the same shape is rejected. _fields is for VideState and for types, such as typeof(schema._fields). Do not change or read it in game code.
| Name | Type | Description |
|---|---|---|
Fields | { [string]: Schema<any> } | The table type of the field builders. |
IntegerOptions Added in 0.1.0
type IntegerOptions = {
min: number?, -- Smallest allowed value, inclusive
max: number?, -- Largest allowed value, inclusive
}Options for integer.
| Field | Type | Required | Default | Description |
|---|---|---|---|---|
min | number | No | No lower bound | The smallest allowed value, inclusive. Must be an integer. |
max | number | No | No upper bound | The largest allowed value, inclusive. Must be an integer. |
FloatOptions Added in 0.1.0
type FloatOptions = {
min: number?, -- Smallest allowed value, inclusive
max: number?, -- Largest allowed value, inclusive
step: number?, -- Sets how many decimal places a value may have
}Options for float.
| Field | Type | Required | Default | Description |
|---|---|---|---|---|
min | number | No | No lower bound | The smallest allowed value, inclusive. |
max | number | No | No upper bound | The largest allowed value, inclusive. |
step | number | No | No limit | A positive, finite number. Its decimal places set how many decimal places a value may have, so 0.01 allows up to two. |
StringOptions Added in 0.1.0
type StringOptions = {
maxLength: number?, -- Maximum length in Unicode code points
truncate: boolean?, -- Cut longer strings instead of rejecting them
}Options for string.
| Field | Type | Required | Default | Description |
|---|---|---|---|---|
maxLength | number | No | No limit | The maximum length, counted in Unicode code points rather than bytes. When set, strings with invalid UTF-8 are rejected. |
truncate | boolean | No | false | When true, longer strings are cut down to maxLength instead of rejected. |