Module system.expect

The expect module provides error checking functions for other libraries.

Index

Function

array()

Provides a special type that can check for an array.

expect()

Check that a numbered argument matches the expected type(s). If the type doesn't match, throw an error.

field()

Check that a key in a table matches the expected type(s). If the type doesn't match, throw an error.

match()

Provides a special type that can check for a string matching a pattern.

range()

Check that a number is between the specified minimum and maximum values. If the number is out of bounds, throw an error.

struct()

Provides a special type that can check all of the fields of a table at once.

table()

Provides a special type that can check for a table with all entries.

API Reference

system.expect.expect(
    index: number,
    value: <T>,
    ...: string | fun(v: any): boolean
): (result: <T>)

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.

Parameters:
  • index (number) – The index of the argument to check

  • value (<T>) – The value to check

  • ... (string | fun(v: any): boolean) – The types to check for

Returns:

result (<T>) – value

system.expect.field(tbl: any, key: any, ...: string | fun(v: any): boolean): (result: 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.

Parameters:
  • tbl (any) – The table (or other indexable value) to search through

  • key (any) – The key of the table to check

  • ... (string | fun(v: any): boolean) – The types to check for

Returns:

result (any) – The indexed value in the table

system.expect.range(num: number, min?: number, max?: number): (result: number)

Check that a number is between the specified minimum and maximum values. If the number is out of bounds, throw an error.

Parameters:
  • num (number) – The number to check

  • min? (number) – The minimum value of the number (inclusive) (defaults to -math.huge)

  • max? (number) – The maximum value of the number (inclusive) (defaults to math.huge)

Returns:

result (number) – num

system.expect.struct(struct: table()): (result: fun(v: any): 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.

@usage Checks the structure of a complex table.

expect(1, tbl, expect.struct {
    name = "string",
    age = "number",
    phone = {expect.match "%d%d%d%-%d%d%d%-%d%d%d%d", "number"},
    address = expect.struct {
        address = "string",
        state = "string",
        zip = {"number", "nil"},
        country = "string"
    },
    children = expect.array "string",
    jobs = expect.array {"string", expect.struct {
        title = "string",
        employer = "string",
        salary = {"number", "nil"}
    }}
})
Parameters:

struct (table()) – The expected structure of the table

Returns:

result (fun(v: any): boolean) – A checker function, to be passed to expect.expect

system.expect.array(types: string | string[]): (result: fun(v: any): boolean)

Provides a special type that can check for an array.

Parameters:

types (string | string[]) – The type(s) to check for in each member

Returns:

result (fun(v: any): boolean) – A checker function, to be passed to expect.expect

system.expect.table(types: string | string[]): (result: fun(v: any): boolean)

Provides a special type that can check for a table with all entries.

Parameters:

types (string | string[]) – The type(s) to check for in each member

Returns:

result (fun(v: any): boolean) – A checker function, to be passed to expect.expect

system.expect.match(pattern: string): (result: fun(v: any): boolean)

Provides a special type that can check for a string matching a pattern.

Parameters:

pattern (string) – The pattern to check on the string

Returns:

result (fun(v: any): boolean) – A checker function, to be passed to expect.expect