Skip to content

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 float or udim2, describes one value. It returns a Schema<T> for a Luau type T.
  • 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 to rules.dynamic or rules.static.
text
field builders ──> schemas.new ──> schemas.withDefaults / schemas.extend ──> rules.dynamic / rules.static ──> rule.new

You 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.

luau
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 ​

luau
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.

luau
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

NameTypeDescription
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

TypeDescription
RuleSchema<Fields>A frozen rule schema with these fields.

Errors

CauseMessage
Errorfields is not a tableschemas.new expects a table of field schemas, got <type>
Errorfields is already a rule schemaschemas.new expects a table of fields, and this is already a schema
ErrorA field name is not a stringschemas.new expects string field names, got <type>
ErrorA field is not a field builderschemas.new expects a schema for field "<field>"

schemas.withDefaults Added in 0.8.0 ​

luau
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.

luau
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

NameTypeDescription
schemaRuleSchema<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

TypeDescription
RuleSchema<Fields>A new schema with the same fields and the defaults applied.

Errors

CauseMessage
Errorschema is not a rule schemaschemas.withDefaults expects a schema made with VideState.schemas.new, got <type>
Errordefaults is not a tableschemas.withDefaults expects a table of default values by field name, got <type>
ErrorA name in defaults is not a fieldschemas.withDefaults names fields that do not exist: <names>
ErrorA default is wrong for its fieldfield "<field>": followed by the withDefault error

schemas.extend Added in 0.8.0 Changed in 0.9.0 ​

luau
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.

luau
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:

luau
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

NameTypeDescription
baseRuleSchema<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

TypeDescription
RuleSchema<Base & Fields>A new schema with the fields of both.

Errors

CauseMessage
Errorbase is not a rule schemaschemas.extend expects a schema made with VideState.schemas.new, got <type>
Errorfields is not a table, or a field is not a field builderThe same messages as schemas.new, starting with schemas.extend
ErrorA field is already in base, including a field of a schema passed as fieldsschemas.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 ​

luau
VideState.schemas.boolean() -> Schema<boolean>

Accepts true or false, and rejects every other type.

Returns

TypeDescription
Schema``<boolean>A schema for booleans.

integer Added in 0.1.0 Changed in 0.8.0 ​

luau
VideState.schemas.integer(
    options: {
        min: number?, -- Smallest allowed value, inclusive
        max: number? -- Largest allowed value, inclusive
    }?
) -> Schema<number>

Accepts finite whole numbers.

Parameters

NameTypeDescription
optionsIntegerOptions?Optional. Bounds for the value.
options.minnumber?Optional. The smallest allowed value, inclusive. Must be an integer. No lower bound by default.
options.maxnumber?Optional. The largest allowed value, inclusive. Must be an integer. No upper bound by default.

Returns

TypeDescription
Schema``<number>A schema for whole numbers within the bounds.

Errors

CauseMessage
Erroroptions is not a tableexpected Integer options to be a table
Errormin is not an integerInteger minimum must be a finite integer
Errormax is not an integerInteger maximum must be a finite integer
Errormin is greater than maxInteger minimum must not exceed maximum

float Added in 0.1.0 Changed in 0.8.0 ​

luau
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

NameTypeDescription
optionsFloatOptions?Optional. Bounds and precision for the value.
options.minnumber?Optional. The smallest allowed value, inclusive. No lower bound by default.
options.maxnumber?Optional. The largest allowed value, inclusive. No upper bound by default.
options.stepnumber?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

TypeDescription
Schema``<number>A schema for finite numbers within the bounds and precision.

Errors

CauseMessage
Erroroptions is not a tableexpected Float options to be a table
Errormin is not finiteFloat minimum must be finite
Errormax is not finiteFloat maximum must be finite
Errorstep is not a positive, finite numberFloat step must be positive and finite
Errorstep has too many decimal places to validateFloat step precision is too small to validate
Errormin is greater than maxFloat 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.

luau
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

NameTypeDescription
optionsStringOptions?Optional. Length limits for the value.
options.maxLengthnumber?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.truncateboolean?Optional. When true, longer strings are cut down to maxLength instead of rejected. Defaults to false.

Returns

TypeDescription
Schema``<string>A schema for strings.

Errors

CauseMessage
Erroroptions is not a tableexpected String options to be a table
ErrormaxLength is not a non-negative integerString maxLength must be a finite integer greater than or equal to 0
Errortruncate is not a booleanString truncate must be a boolean

enum Added in 0.1.0 Changed in 0.8.0 ​

luau
VideState.schemas.enum(
    ...: string -- The accepted values
) -> Schema<string>

Accepts only the strings passed as arguments. Matching is case-sensitive.

Parameters

NameTypeDescription
...stringThe accepted values. At least one is required.

Returns

TypeDescription
Schema``<string>A schema that accepts one of the given strings.

Errors

CauseMessage
ErrorNo values are givenEnum requires at least one string value
ErrorAn argument is not a stringexpected Enum argument <n> to be String, got <type>

optional Added in 0.1.0 Changed in 0.8.0 ​

luau
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

NameTypeDescription
schemaSchema``<T>The schema to apply when the value is not nil.

Returns

TypeDescription
Schema``<T?>A schema that accepts nil or a value accepted by schema.

union Added in 0.5.0 Changed in 0.8.0 ​

luau
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.

luau
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

NameTypeDescription
firstSchema``<A>The schema to try first.
secondSchema``<B>The schema to try when first rejects the value.

Returns

TypeDescription
`Schema``<AB>`

Errors

WhenMessage
ErrorAn argument is not a schemaexpected 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".

FromToResult
100200Tweens, as two numbers.
UDim2UDim2Tweens, 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.
UDim2Color3Switches 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 ​

luau
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

NameTypeDescription
fields{ [string]: Schema<any> }The schema for each field, keyed by field name. Keys must be strings, so arrays are rejected.

Returns

TypeDescription
Schema``<{ [string]: any }>A schema for tables with exactly these fields.

Errors

CauseMessage
Errorfields is not a tableexpected a table of named field schemas

record Added in 0.1.0 Changed in 0.8.0 ​

luau
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

NameTypeDescription
fields{ [string]: Schema<any> }The schema for each field, keyed by field name.

Returns

TypeDescription
Schema``<{ [string]: any }>A schema for records with exactly these fields.

Errors

CauseMessage
Errorfields is not a tablerecord expects a table of field schemas, got <type>
ErrorA field name is not a stringrecord expects string field names, got <type>
ErrorA field is not a schemarecord 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 ​

luau
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.

luau
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

NameTypeDescription
schemaSchema``<T>The field builder of the field.
defaultValueTThe default. It must be a plain value that schema accepts, not a Vide source.

Returns

TypeDescription
Schema``<T>A field builder that accepts the same values as schema and has a default.

Errors

CauseMessage
Errorschema is not a field builderexpected a schema to give a default value
Errorschema is an optional schemawithDefault cannot wrap an optional schema: ...
Errorschema already has a defaultwithDefault cannot wrap a schema that already has a default
ErrorThe default is a Vide sourcethe default must be a plain value, not a Vide source
ErrorThe schema rejects the defaultthe 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 ​

luau
VideState.schemas.color3() -> Schema<Color3>

Accepts a Color3.

numberRange Added in 0.4.0 Changed in 0.8.0 ​

luau
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 ​

luau
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 ​

luau
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 ​

luau
VideState.schemas.udim() -> Schema<UDim>

Accepts a UDim.

udim2 Added in 0.1.0 Changed in 0.8.0 ​

luau
VideState.schemas.udim2() -> Schema<UDim2>

Accepts a UDim2.

vector2 Added in 0.1.0 Changed in 0.8.0 ​

luau
VideState.schemas.vector2() -> Schema<Vector2>

Accepts a Vector2.

Types ​

Infer Added in 0.1.0 ​

luau
type Infer<S> = -- The Luau type that the schema S accepts

Gives the Luau type that a field builder accepts. For the values of a whole rule, use InferRule.

NameTypeDescription
Stypeof(schema)The schema to read the accepted type from.

RuleSchema Added in 0.8.0 ​

luau
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.

NameTypeDescription
Fields{ [string]: Schema<any> }The table type of the field builders.

IntegerOptions Added in 0.1.0 ​

luau
type IntegerOptions = {
    min: number?, -- Smallest allowed value, inclusive
    max: number?, -- Largest allowed value, inclusive
}

Options for integer.

FieldTypeRequiredDefaultDescription
minnumberNoNo lower boundThe smallest allowed value, inclusive. Must be an integer.
maxnumberNoNo upper boundThe largest allowed value, inclusive. Must be an integer.

FloatOptions Added in 0.1.0 ​

luau
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.

FieldTypeRequiredDefaultDescription
minnumberNoNo lower boundThe smallest allowed value, inclusive.
maxnumberNoNo upper boundThe largest allowed value, inclusive.
stepnumberNoNo limitA 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 ​

luau
type StringOptions = {
    maxLength: number?, -- Maximum length in Unicode code points
    truncate: boolean?, -- Cut longer strings instead of rejecting them
}

Options for string.

FieldTypeRequiredDefaultDescription
maxLengthnumberNoNo limitThe maximum length, counted in Unicode code points rather than bytes. When set, strings with invalid UTF-8 are rejected.
truncatebooleanNofalseWhen true, longer strings are cut down to maxLength instead of rejected.