From 07b5c423ac1e1ff21fb254951cf58daa174f0aa6 Mon Sep 17 00:00:00 2001 From: jigordev Date: Wed, 26 Jun 2024 21:30:28 -0300 Subject: [PATCH] First commit --- LICENSE | 19 ++++++ README.md | 52 ++++++++++++++++ rockspec/lua-checkargs-1.0-1.rockspec | 24 ++++++++ src/checkargs.lua | 64 ++++++++++++++++++++ tests/test_checkargs.lua | 86 +++++++++++++++++++++++++++ 5 files changed, 245 insertions(+) create mode 100644 LICENSE create mode 100644 README.md create mode 100644 rockspec/lua-checkargs-1.0-1.rockspec create mode 100644 src/checkargs.lua create mode 100644 tests/test_checkargs.lua diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..1f5c4e2 --- /dev/null +++ b/LICENSE @@ -0,0 +1,19 @@ +Copyright (C) 2024 J. Igor Melo + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..29f75a7 --- /dev/null +++ b/README.md @@ -0,0 +1,52 @@ +# lua-checkargs + +`checkargs` is a Lua library designed for argument validation in Lua functions. It provides functions to validate function arguments based on various criteria such as type, range, presence of fields in tables, and more. + +## Installation + +You can install `checkargs` using LuaRocks: + +```bash +luarocks install lua-checkargs +``` + +## Usage + +### Functions + +#### check_arg(func, name, expected, value, optional, use_error) +Validates a single argument `value` against expected types `expected`. Throws an error if validation fails when `use_error` is `true`. + +#### check_list(func, name, expected, list, optional, use_error) +Validates each element in `list` against expected types `expected`. Throws an error if any element fails validation when `use_error` is `true`. + +#### check_range(func, name, value, min, max, use_error) +Validates a numeric `value` to ensure it falls within the specified range `[min, max]`. Throws an error if validation fails when `use_error` is `true`. + +#### check_fields(func, name, tbl, fields, use_error) +Validates that table `tbl` contains all specified `fields`. Throws an error if any field is missing when `use_error` is `true`. + +#### check_composite(func, name, value, expected_fields, use_error) +Validates a table `value` against expected field types specified in `expected_fields`. Throws an error if any field type mismatch is found when `use_error` is `true`. + +#### check_not_nil(func, name, value, use_error) +Validates that `value` is not `nil`. Throws an error if `value` is `nil` when `use_error` is `true`. + +### Example + +```lua +local checkargs = require('checkargs') + +function greet(name) + checkargs.check_arg("greet", "name", {"string"}, name, false, true) + print("Hello, " .. name .. "!") +end + +greet("Alice") -- Outputs: Hello, Alice! +greet(123) -- Throws an error: Argument 'name' must be a string, got: number + +``` + +## License + +This library is open-source and available under the MIT License. See the [LICENSE](LICENSE) file for more details. \ No newline at end of file diff --git a/rockspec/lua-checkargs-1.0-1.rockspec b/rockspec/lua-checkargs-1.0-1.rockspec new file mode 100644 index 0000000..dba82cf --- /dev/null +++ b/rockspec/lua-checkargs-1.0-1.rockspec @@ -0,0 +1,24 @@ +package = "lua-checkargs" +version = "1.0-1" +source = { + url = "git://github.com/jigordev/lua-checkargs.git" +} +description = { + summary = "checkargs is a Lua library designed for argument validation in Lua functions.", + detailed = [[ + checkargs is a Lua library designed for argument validation in Lua functions. + It provides functions to validate function arguments based on various criteria such as type, range, presence of fields in tables, and more. + ]], + license = "MIT", + homepage = "https://github.com/jigordev/lua-checkargs", + maintainer = "J. Igor Melo ", +} +dependencies = { + "lua >= 5.1", +} +build = { + type = "builtin", + modules = { + ["checkargs"] = "src/checkargs.lua", + } +} diff --git a/src/checkargs.lua b/src/checkargs.lua new file mode 100644 index 0000000..2f1292e --- /dev/null +++ b/src/checkargs.lua @@ -0,0 +1,64 @@ +local checkargs = {} + +local function contains(tbl, value) + for _, v in ipairs(tbl) do + if v == value then + return true + end + end + return false +end + +local function check(use_error, condition, message) + if not condition then + if use_error then + error(message) + else + assert(condition, message) + end + end +end + +function checkargs.check_arg(func, name, expected, value, optional, use_error) + check(use_error, contains(expected, type(value)) or (optional and value == nil), + string.format("Error in %s: Argument '%s' must be a %s, got: %s", func, name, table.concat(expected, ", "), + type(value))) +end + +function checkargs.check_list(func, name, expected, list, optional, use_error) + for _, arg in ipairs(list) do + checkargs.check_arg(func, name, expected, arg, optional, use_error) + end +end + +function checkargs.check_range(func, name, value, min, max, use_error) + check(use_error, type(value) == "number", + string.format("Error in %s: Argument '%s' must be a number, got: %s", func, name, type(value))) + check(use_error, value >= min and value <= max, + string.format("Error in %s: Argument '%s' must be between %d and %d, got: %d", func, name, min, max, value)) +end + +function checkargs.check_fields(func, name, tbl, fields, use_error) + check(use_error, type(tbl) == "table", + string.format("Error in %s: Argument '%s' must be a table, got: %s", func, name, type(tbl))) + for _, field in ipairs(fields) do + check(use_error, tbl[field] ~= nil, + string.format("Error in %s: Table '%s' must contain field '%s'", func, name, field)) + end +end + +function checkargs.check_composite(func, name, value, expected_fields, use_error) + check(use_error, type(value) == "table", + string.format("Error in %s: Argument '%s' must be a table, got: %s", func, name, type(value))) + for field, field_type in pairs(expected_fields) do + check(use_error, type(value[field]) == field_type, + string.format("Error in %s: Field '%s' in argument '%s' must be a %s, got: %s", func, field, name, field_type, + type(value[field]))) + end +end + +function checkargs.check_not_nil(func, name, value, use_error) + check(use_error, value ~= nil, string.format("Error in %s: Argument '%s' must not be nil", func, name)) +end + +return checkargs diff --git a/tests/test_checkargs.lua b/tests/test_checkargs.lua new file mode 100644 index 0000000..3f8b90b --- /dev/null +++ b/tests/test_checkargs.lua @@ -0,0 +1,86 @@ +local checkargs = require("checkargs") + +local function test_check_arg() + local status, err = pcall(function() + checkargs.check_arg("test_func", "test_arg", { "string" }, "value", false, true) + end) + assert(status, err) + + status, err = pcall(function() + checkargs.check_arg("test_func", "test_arg", { "string" }, 123, false, true) + end) + assert(not status, "Expected an error but got none") +end + +local function test_check_list() + local status, err = pcall(function() + checkargs.check_list("test_func", "test_list", { "number" }, { 1, 2, 3 }, false, true) + end) + assert(status, err) + + status, err = pcall(function() + checkargs.check_list("test_func", "test_list", { "number" }, { 1, "two", 3 }, false, true) + end) + assert(not status, "Expected an error but got none") +end + +local function test_check_range() + local status, err = pcall(function() + checkargs.check_range("test_func", "test_value", 5, 1, 10, true) + end) + assert(status, err) + + status, err = pcall(function() + checkargs.check_range("test_func", "test_value", 15, 1, 10, true) + end) + assert(not status, "Expected an error but got none") +end + +local function test_check_fields() + local status, err = pcall(function() + checkargs.check_fields("test_func", "test_table", { a = 1, b = 2 }, { "a", "b" }, true) + end) + assert(status, err) + + status, err = pcall(function() + checkargs.check_fields("test_func", "test_table", { a = 1 }, { "a", "b" }, true) + end) + assert(not status, "Expected an error but got none") +end + +local function test_check_composite() + local status, err = pcall(function() + checkargs.check_composite("test_func", "test_composite", { a = 1, b = "test" }, { a = "number", b = "string" }, + true) + end) + assert(status, err) + + status, err = pcall(function() + checkargs.check_composite("test_func", "test_composite", { a = 1, b = 2 }, { a = "number", b = "string" }, true) + end) + assert(not status, "Expected an error but got none") +end + +local function test_check_not_nil() + local status, err = pcall(function() + checkargs.check_not_nil("test_func", "test_value", "value", true) + end) + assert(status, err) + + status, err = pcall(function() + checkargs.check_not_nil("test_func", "test_value", nil, true) + end) + assert(not status, "Expected an error but got none") +end + +local function runtests() + test_check_arg() + test_check_list() + test_check_range() + test_check_fields() + test_check_composite() + test_check_not_nil() + print("All tests passed successfully!") +end + +runtests()