Skip to content

Commit

Permalink
Optional case-insensitive literal comparison for strings (#9)
Browse files Browse the repository at this point in the history
* More descriptive docs for path_or_nil and adding luacheck lints

* Have to run luarocks install separately for each package

* Adding more contributing docs

* Adding link to valid.lua

* Conistent function calling conventions in docs

* Adding allof and anyof validation functions

* Adding optional case-insensitive comparisons for string literals

* Consistent notes in readme

* Consistent notes in readme

* Adding more examples for comparing table literals
  • Loading branch information
benwilber authored May 19, 2024
1 parent 260c995 commit 03a3097
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@ assert(is_valid) -- true

Validates that a value matches a specific literal.

The comparison is performed using the equality operator (`==`), which means that both the value and the type must match exactly.

Note: Two tables will not compare equal unless they are both references to *the same* table.

#### Usage

```lua
Expand All @@ -108,8 +112,26 @@ assert(is_valid) -- true

local is_valid = valid.literal("abc")("123")
assert(not is_valid) -- false

local is_valid = valid.literal("abc", {icase = true})("ABC")
assert(is_valid) -- true

local price_table = {price = 1.00}

local is_valid = valid.literal(price_table)({price = 1.00})
assert(not is_valid) -- false, not the same table

local is_valid = valid.literal(price_table)(price_table)
assert(is_valid) -- true
```

#### Parameters

* `opts` (optional): Table of options.
* `icase`: Set to `true` to allow case-insensitive validation of a string literal.
* `func`: A custom validation function to call after the literal check.


### `valid.number`

Validates that a value is a number within an optional range.
Expand Down
12 changes: 12 additions & 0 deletions tests.lua
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,18 @@ describe("Validation Library Tests", function()
local simple_function = function() end

local tests = {
{
description = "Case-insensitive literal",
definition = valid.literal("ALL CAPS", {icase = true}),
data = "all caps",
expected = {
is_valid = true,
val_or_err = "all caps",
badval_or_nil = nil,
path_or_nil = nil
}
},

{
description = "Valid string or number",
definition = valid.anyof {valid.string(), valid.number()},
Expand Down
9 changes: 8 additions & 1 deletion valid.lua
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,17 @@ end

local function literal(lit, opts)
opts = opts or {}
local icase = opts.icase or false
local func = opts.func or defaultfunc

return function(val)
if val ~= lit then
if icase and type(lit) == "string" and type(val) == "string" then

if val:lower() ~= lit:lower() then
return false, "literal", val
end

elseif val ~= lit then
return false, "literal", val
end

Expand Down

0 comments on commit 03a3097

Please sign in to comment.