From a7f4ca5d4881bfd554900dbf57e797e0cfae89c4 Mon Sep 17 00:00:00 2001 From: Ben Wilber Date: Sat, 17 Aug 2024 10:36:35 -0400 Subject: [PATCH] * Add valid.boolean() function * Allow implicit literals to be used in valid.anyof and valid.allof --- README.md | 24 ++++++++++++++++++++++++ valid.lua | 36 ++++++++++++++++++++++++++---------- 2 files changed, 50 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 028a77d..ddc98fd 100644 --- a/README.md +++ b/README.md @@ -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) @@ -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. diff --git a/valid.lua b/valid.lua index 9f1a19e..68378ea 100644 --- a/valid.lua +++ b/valid.lua @@ -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 @@ -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 @@ -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, @@ -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, @@ -431,7 +433,7 @@ 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 @@ -439,8 +441,12 @@ local function anyof(deffuncs) 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 @@ -457,7 +463,7 @@ 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 @@ -465,8 +471,12 @@ local function allof(deffuncs) 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} @@ -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