Module util.schema

Defines schemas and what each accepts.

local schema = require "util.schema"

local Number = schema.Type"number"

print(Number:validate(10))   --> true
print(Number:validate(3.14)) --> true
print(Number:validate"foo")  --> false "" "not number"
print(Number:validate(nil))  --> false "" "not number"

local Int = schema.Integer()

print(Int:validate(10))  --> true
print(Int:validate(1.5)) --> false "" "not integer"
print(Int:validate"foo") --> false "" "not number"
print(Int:validate(nil)) --> false "" "not number"

local Season = schema.Enum{
  "winter",
  "spring",
  "summer",
  "autumn",
}

print(Season:validate"winter") --> true
print(Season:validate"summer") --> true
print(Season:validate"frog")   --> false "" "not in enum"
print(Season:validate(123))    --> false "" "not in enum"

local Point = schema.Struct{
  x = Number,
  y = Number,
}

print(Point:validate{x = 10, y = -20})         --> true
print(Point:validate{x = 10, y = -20, z = 0})  --> true
print(Point:validate{x = 10, y = 'foo'})       --> false ".y" "not number"
print(Point:validate{x = 10})                  --> false ".y" "not number"

local OptionalNumber = schema.Option(Number)

print(OptionalNumber:validate(10))   --> true
print(OptionalNumber:validate(3.14)) --> true
print(OptionalNumber:validate"foo")  --> false "!" "not number"
print(OptionalNumber:validate(nil))  --> true

local Points = schema.Array(Point)

local p1 = {x = 10, y = -20}
local p2 = {x = 30, y = 50}
local p3 = {x = -40, y = 70}

print(Points:validate{})                  --> true
print(Points:validate{p1})                --> true
print(Points:validate{p1, p2, p3})        --> true
print(Points:validate{p1, p2, p3, a = 1}) --> true
print(Points:validate{p1, nil, p3})       --> true
print(Points:validate{p1, 123, p3})       --> false "[2]" "not table"

local RGB = schema.Array(Int, 3)

print(RGB:validate{0, 0, 0})    --> true
print(RGB:validate{0, 0})       --> false "" "not number"
print(RGB:validate{0, 0, 0, 0}) --> false "" "too big"

local MinTemp = schema.Map(Season, Number)

print(MinTemp:validate{})               --> true
print(MinTemp:validate{winter = -10})   --> true
print(MinTemp:validate{frog = 7})       --> false "[k]" "not in enum"
print(MinTemp:validate{summer = "foo"}) --> false "[v]" "not number"

Functions

Schema:new (t) Instantiate the schema and validate input
Schema:eq (t1, t2) Compares two inputs against a schema
Schema:isValid (t) Validate an input against a schema
Schema:validate (t) Validate an input against a schema
Type (T) Construct a "type" schema.
Integer () Construct an "integer" schema.
Enum (T) Construct an "enum" schema.
Struct (T) Construct a "struct" schema.
Option (S) Construct an "option" schema.
Array (S, n) Construct an "array" schema.
Map (Sk, Sv) Construct a "map" schema.


Functions

Schema:new (t)
Instantiate the schema and validate input

Parameters:

  • t input

Returns:

    input
Schema:eq (t1, t2)
Compares two inputs against a schema

Parameters:

  • t1 first input
  • t2 second input

Returns:

    boolean whether the inputs are equal according to the schema
Schema:isValid (t)
Validate an input against a schema

Parameters:

  • t input

Returns:

  1. boolean whether the schema accepted or rejected the input
  2. optional string an error message (if first return is false)
Schema:validate (t)
Validate an input against a schema

Parameters:

  • t input

Returns:

  1. boolean whether the schema accepted or rejected the input
  2. optional string the schema path where the error occurred (if first return is false)
  3. optional string the error message (if first return is false)
Type (T)
Construct a "type" schema.

Parameters:

Returns:

    Schema a schema that only accepts objects of type T
Integer ()
Construct an "integer" schema.

Returns:

    Schema a schema that only accepts integers
Enum (T)
Construct an "enum" schema.

Parameters:

  • T table an array of values

Returns:

    Schema a schema that only accepts values in array T
Struct (T)
Construct a "struct" schema.

Parameters:

  • T table a record of schemas with string keys

Returns:

    Schema a schema that only accepts tables t, s.t. for all key-value pairs (k,S) in T, S accepts t[k].
Option (S)
Construct an "option" schema.

Parameters:

  • S Schema

Returns:

    Schema a schema that accepts nil and anything accepted by S.
Array (S, n)
Construct an "array" schema.

Parameters:

  • S Schema
  • n optional number the array length

Returns:

    Schema a schema that only accepts tables t, s.t. for all index-value pairs (i,v) in t, S accepts v.
Map (Sk, Sv)
Construct a "map" schema.

Parameters:

  • Sk Schema
  • Sv Schema

Returns:

    Schema a schema that only accepts tables t, s.t. for all key-value pairs (k,v) in t, Sk accepts k and Sv accepts v.
generated by LDoc 1.5.0 Last updated 2023-07-02 05:10:58