From 03a3097a6861c10d66b7ecf9c4acfc74bb8f27ba Mon Sep 17 00:00:00 2001 From: Ben Wilber Date: Sun, 19 May 2024 15:43:52 -0400 Subject: [PATCH] Optional case-insensitive literal comparison for strings (#9) * 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 --- README.md | 22 ++++++++++++++++++++++ tests.lua | 12 ++++++++++++ valid.lua | 9 ++++++++- 3 files changed, 42 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index cf1a9fc..96f6067 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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. diff --git a/tests.lua b/tests.lua index e7ff5c8..c31d644 100644 --- a/tests.lua +++ b/tests.lua @@ -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()}, diff --git a/valid.lua b/valid.lua index 8ede7a2..d3f3c54 100644 --- a/valid.lua +++ b/valid.lua @@ -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