OPX//77
opx_lib

Lib.Validate

Last updated on

Turn a value that came from outside into one you can use, or nil.

Pure. No permission. Each function answers the value, or nil.

Lib.Validate.Number

A finite number within the inclusive bounds. Numeric strings are converted (tonumber); NaN and infinity are refused.

Lib.Validate.Number(value, low, high)

Parameters

  • value: any
  • low?: number
  • high?: number

Returns

  • number?

Lib.Validate.Integer

A whole number within the bounds. A whole float (3.0) is accepted and narrowed; a fractional one is refused, never rounded.

Lib.Validate.Integer(value, low, high)

Parameters

  • value: any
  • low?: integer
  • high?: integer

Returns

  • integer?
Example
local amount = Lib.Validate.Integer(payload.amount, 1, 100)
if not amount then return end

Lib.Validate.Text

A non-empty string of at most limit bytes. Not trimmed.

Lib.Validate.Text(value, limit)

Parameters

  • value: any
  • limit?: integer

Returns

  • string?

Lib.Validate.Word

An identifier: letters, digits, _ - . only.

Lib.Validate.Word(value, limit)

Parameters

  • value: any
  • limit?: integer
    • Bytes. Default: 64.

Returns

  • string?
Example
local name = Lib.Validate.Word(payload.name, 24)

Lib.Validate.OneOf

One of a closed set.

Lib.Validate.OneOf(value, allowed)

Parameters

  • value: any
  • allowed: table
    • A set ({ trunk = true }) or a list ({ 'trunk', 'glovebox' }).

Returns

  • any?
Example
local where = Lib.Validate.OneOf(payload.where, { 'trunk', 'glovebox' })

Lib.Validate.Table

A table with no metatable and at most limit entries (counted over pairs, so maps count too).

Lib.Validate.Table(value, limit)

Parameters

  • value: any
  • limit?: integer

Returns

  • table?

On this page