system.expect
The expect module provides error checking functions for other libraries.
expect(index: number, value: any, ...: string|function(v):boolean): any
Check that a numbered argument matches the expected type(s). If the type doesn’t match, throw an error. This function supports custom types by checking the __name metaproperty. Passing the result of @{expect.struct}, @{expect.array}, or @{expect.match} as a type parameter will use that function as a validator.
Arguments
index
: The index of the argument to checkvalue
: The value to check...
: The types to check for
Return Values
value
field(tbl: any, key: any, ...: string|function(v):boolean): any
Check that a key in a table matches the expected type(s). If the type doesn’t match, throw an error. This function supports custom types by checking the __name metaproperty. Passing the result of @{expect.struct}, @{expect.array}, or @{expect.match} as a type parameter will use that function as a validator.
Arguments
tbl
: The table (or other indexable value) to search throughkey
: The key of the table to check...
: The types to check for
Return Values
The indexed value in the table
range(num: number[, min: number = -math.huge][, max: number = math.huge]): number
Check that a number is between the specified minimum and maximum values. If the number is out of bounds, throw an error.
Arguments
num
: The number to checkmin
: The minimum value of the number (inclusive) (defaults to -math.huge)max
: The maximum value of the number (inclusive) (defaults to math.huge)
Return Values
num
struct(struct: table): function(v):boolean
Provides a special type that can check all of the fields of a table at once.
The struct
parameter defines the structure of the table. This is a key- value table, where the key is the name of the field and the value is the expected type(s) of the field.
- If the value is a single string, the field must be that type.
- If the value is a list of strings, the field must be one of those types.
- Any type can be replaced by one of the special types as with @{expect.expect}.
Arguments
struct
: The expected structure of the table
Return Values
A checker function, to be passed to expect.expect
array(types: string|string[]): function(v):boolean
Provides a special type that can check for an array.
Arguments
types
: The type(s) to check for in each member
Return Values
A checker function, to be passed to expect.expect
table(types: string|string[]): function(v):boolean
Provides a special type that can check for a table with all entries.
Arguments
types
: The type(s) to check for in each member
Return Values
A checker function, to be passed to expect.expect
match(pattern: string): function(v):boolean
Provides a special type that can check for a string matching a pattern.
Arguments
pattern
: The pattern to check on the string
Return Values
A checker function, to be passed to expect.expect