Skip to content

Commit

Permalink
* Add valid.boolean() function
Browse files Browse the repository at this point in the history
* Allow implicit literals to be used in valid.anyof and valid.allof
  • Loading branch information
benwilber committed Aug 17, 2024
1 parent 84cbde3 commit a7f4ca5
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 10 deletions.
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ A library for Lua to validate various values and table structures.
- [Validating Complex Data Types](#validating-complex-data-types)
- [Validation Definition Functions](#validation-definition-functions)
- [`valid.literal`](#validliteral)
- [`valid.boolean`](#validboolean)
- [`valid.number`](#validnumber)
- [`valid.string`](#validstring)
- [`valid.table`](#validtable)
Expand Down Expand Up @@ -142,6 +143,29 @@ assert(is_valid) -- true
* `func`: A custom validation function to call after the literal check.


### `valid.boolean`

Validates that a value is a literal boolean either `true` or `false`.

This is a shorthand for `valid.anyof {true, false}`.

#### Usage

```lua
local valid = require "valid"

local is_valid = valid.boolean()(true)
assert(is_valid) -- true

local is_valid = valid.boolean()("false")
assert(not is_valid) -- false, not the literal boolean false
```

#### Parameters

*(none)*


### `valid.number`

Validates that a value is a number within an optional range.
Expand Down
36 changes: 26 additions & 10 deletions valid.lua
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ _M.string = _string

-- Validates that a value is a table, with optional constraints for arrays and maps.
-- (Don't shadow Lua's "table")
local function _table(opts)
local function table_(opts)
opts = opts or {}
local array = false
local map = false
Expand Down Expand Up @@ -199,6 +199,8 @@ local function _table(opts)
local val_or_err
local badval_or_nil
local path_or_nil

-- the value at key (or index) in the table.
local v

for key_or_idx, func_or_lit in pairs(tabledef) do
Expand Down Expand Up @@ -230,11 +232,11 @@ local function _table(opts)
return callfunc(func, val)
end
end
_M.table = _table
_M.table = table_

-- A shorthand for valid.table with opts.array set to true.
local function array(opts)
return _table {
return table_ {
array = true,
empty = opts.empty,
required = opts.required,
Expand Down Expand Up @@ -335,7 +337,7 @@ _M.arrayof = arrayof

-- A shorthand for valid.table with opts.map set to true.
local function map(opts)
return _table {
return table_ {
map = true,
empty = opts.empty,
required = opts.required,
Expand Down Expand Up @@ -431,16 +433,20 @@ end
_M.func = func

-- Validates that a value satisfies at least one of the given validation functions.
local function anyof(deffuncs)
local function anyof(funcs_or_lits)
return function(val)
local is_valid
local val_or_err
local badval_or_nil
local path_or_nil
local errtabs = {}

for _, deffunc in ipairs(deffuncs) do
is_valid, val_or_err, badval_or_nil, path_or_nil = deffunc(val)
for _, func_or_lit in ipairs(funcs_or_lits) do
if type(func_or_lit) ~= "function" then
func_or_lit = literal(func_or_lit)
end

is_valid, val_or_err, badval_or_nil, path_or_nil = func_or_lit(val)

if is_valid then
-- is_valid, val_or_err, badval_or_nil, path_or_nil
Expand All @@ -457,16 +463,20 @@ end
_M.anyof = anyof

-- Validates that a value satisfies all of the given validation functions.
local function allof(deffuncs)
local function allof(funcs_or_lits)
return function(val)
local is_valid
local val_or_err
local badval_or_nil
local path_or_nil
local errtabs = {}

for _, deffunc in ipairs(deffuncs) do
is_valid, val_or_err, badval_or_nil, path_or_nil = deffunc(val)
for _, func_or_lit in ipairs(funcs_or_lits) do
if type(func_or_lit) ~= "function" then
func_or_lit = literal(func_or_lit)
end

is_valid, val_or_err, badval_or_nil, path_or_nil = func_or_lit(val)

if not is_valid then
errtabs[#errtabs + 1] = {val_or_err, badval_or_nil, path_or_nil}
Expand All @@ -484,4 +494,10 @@ local function allof(deffuncs)
end
_M.allof = allof

-- Validates that a value is a literal boolean either true or false.
local function boolean()
return anyof {true, false}
end
_M.boolean = boolean

return _M

0 comments on commit a7f4ca5

Please sign in to comment.