Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
jigordev committed Jun 27, 2024
0 parents commit 07b5c42
Show file tree
Hide file tree
Showing 5 changed files with 245 additions and 0 deletions.
19 changes: 19 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright (C) 2024 J. Igor Melo <jigordev@gmail.com>

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.
52 changes: 52 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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.
24 changes: 24 additions & 0 deletions rockspec/lua-checkargs-1.0-1.rockspec
Original file line number Diff line number Diff line change
@@ -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 <jigordev@gmail.com>",
}
dependencies = {
"lua >= 5.1",
}
build = {
type = "builtin",
modules = {
["checkargs"] = "src/checkargs.lua",
}
}
64 changes: 64 additions & 0 deletions src/checkargs.lua
Original file line number Diff line number Diff line change
@@ -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
86 changes: 86 additions & 0 deletions tests/test_checkargs.lua
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit 07b5c42

Please sign in to comment.