Skip to content

Commit 07b5c42

Browse files
committed
First commit
0 parents  commit 07b5c42

File tree

5 files changed

+245
-0
lines changed

5 files changed

+245
-0
lines changed

LICENSE

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Copyright (C) 2024 J. Igor Melo <jigordev@gmail.com>
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy
4+
of this software and associated documentation files (the "Software"), to deal
5+
in the Software without restriction, including without limitation the rights
6+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
copies of the Software, and to permit persons to whom the Software is
8+
furnished to do so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in
11+
all copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
THE SOFTWARE.

README.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# lua-checkargs
2+
3+
`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.
4+
5+
## Installation
6+
7+
You can install `checkargs` using LuaRocks:
8+
9+
```bash
10+
luarocks install lua-checkargs
11+
```
12+
13+
## Usage
14+
15+
### Functions
16+
17+
#### check_arg(func, name, expected, value, optional, use_error)
18+
Validates a single argument `value` against expected types `expected`. Throws an error if validation fails when `use_error` is `true`.
19+
20+
#### check_list(func, name, expected, list, optional, use_error)
21+
Validates each element in `list` against expected types `expected`. Throws an error if any element fails validation when `use_error` is `true`.
22+
23+
#### check_range(func, name, value, min, max, use_error)
24+
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`.
25+
26+
#### check_fields(func, name, tbl, fields, use_error)
27+
Validates that table `tbl` contains all specified `fields`. Throws an error if any field is missing when `use_error` is `true`.
28+
29+
#### check_composite(func, name, value, expected_fields, use_error)
30+
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`.
31+
32+
#### check_not_nil(func, name, value, use_error)
33+
Validates that `value` is not `nil`. Throws an error if `value` is `nil` when `use_error` is `true`.
34+
35+
### Example
36+
37+
```lua
38+
local checkargs = require('checkargs')
39+
40+
function greet(name)
41+
checkargs.check_arg("greet", "name", {"string"}, name, false, true)
42+
print("Hello, " .. name .. "!")
43+
end
44+
45+
greet("Alice") -- Outputs: Hello, Alice!
46+
greet(123) -- Throws an error: Argument 'name' must be a string, got: number
47+
48+
```
49+
50+
## License
51+
52+
This library is open-source and available under the MIT License. See the [LICENSE](LICENSE) file for more details.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package = "lua-checkargs"
2+
version = "1.0-1"
3+
source = {
4+
url = "git://github.com/jigordev/lua-checkargs.git"
5+
}
6+
description = {
7+
summary = "checkargs is a Lua library designed for argument validation in Lua functions.",
8+
detailed = [[
9+
checkargs is a Lua library designed for argument validation in Lua functions.
10+
It provides functions to validate function arguments based on various criteria such as type, range, presence of fields in tables, and more.
11+
]],
12+
license = "MIT",
13+
homepage = "https://github.com/jigordev/lua-checkargs",
14+
maintainer = "J. Igor Melo <jigordev@gmail.com>",
15+
}
16+
dependencies = {
17+
"lua >= 5.1",
18+
}
19+
build = {
20+
type = "builtin",
21+
modules = {
22+
["checkargs"] = "src/checkargs.lua",
23+
}
24+
}

src/checkargs.lua

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
local checkargs = {}
2+
3+
local function contains(tbl, value)
4+
for _, v in ipairs(tbl) do
5+
if v == value then
6+
return true
7+
end
8+
end
9+
return false
10+
end
11+
12+
local function check(use_error, condition, message)
13+
if not condition then
14+
if use_error then
15+
error(message)
16+
else
17+
assert(condition, message)
18+
end
19+
end
20+
end
21+
22+
function checkargs.check_arg(func, name, expected, value, optional, use_error)
23+
check(use_error, contains(expected, type(value)) or (optional and value == nil),
24+
string.format("Error in %s: Argument '%s' must be a %s, got: %s", func, name, table.concat(expected, ", "),
25+
type(value)))
26+
end
27+
28+
function checkargs.check_list(func, name, expected, list, optional, use_error)
29+
for _, arg in ipairs(list) do
30+
checkargs.check_arg(func, name, expected, arg, optional, use_error)
31+
end
32+
end
33+
34+
function checkargs.check_range(func, name, value, min, max, use_error)
35+
check(use_error, type(value) == "number",
36+
string.format("Error in %s: Argument '%s' must be a number, got: %s", func, name, type(value)))
37+
check(use_error, value >= min and value <= max,
38+
string.format("Error in %s: Argument '%s' must be between %d and %d, got: %d", func, name, min, max, value))
39+
end
40+
41+
function checkargs.check_fields(func, name, tbl, fields, use_error)
42+
check(use_error, type(tbl) == "table",
43+
string.format("Error in %s: Argument '%s' must be a table, got: %s", func, name, type(tbl)))
44+
for _, field in ipairs(fields) do
45+
check(use_error, tbl[field] ~= nil,
46+
string.format("Error in %s: Table '%s' must contain field '%s'", func, name, field))
47+
end
48+
end
49+
50+
function checkargs.check_composite(func, name, value, expected_fields, use_error)
51+
check(use_error, type(value) == "table",
52+
string.format("Error in %s: Argument '%s' must be a table, got: %s", func, name, type(value)))
53+
for field, field_type in pairs(expected_fields) do
54+
check(use_error, type(value[field]) == field_type,
55+
string.format("Error in %s: Field '%s' in argument '%s' must be a %s, got: %s", func, field, name, field_type,
56+
type(value[field])))
57+
end
58+
end
59+
60+
function checkargs.check_not_nil(func, name, value, use_error)
61+
check(use_error, value ~= nil, string.format("Error in %s: Argument '%s' must not be nil", func, name))
62+
end
63+
64+
return checkargs

tests/test_checkargs.lua

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
local checkargs = require("checkargs")
2+
3+
local function test_check_arg()
4+
local status, err = pcall(function()
5+
checkargs.check_arg("test_func", "test_arg", { "string" }, "value", false, true)
6+
end)
7+
assert(status, err)
8+
9+
status, err = pcall(function()
10+
checkargs.check_arg("test_func", "test_arg", { "string" }, 123, false, true)
11+
end)
12+
assert(not status, "Expected an error but got none")
13+
end
14+
15+
local function test_check_list()
16+
local status, err = pcall(function()
17+
checkargs.check_list("test_func", "test_list", { "number" }, { 1, 2, 3 }, false, true)
18+
end)
19+
assert(status, err)
20+
21+
status, err = pcall(function()
22+
checkargs.check_list("test_func", "test_list", { "number" }, { 1, "two", 3 }, false, true)
23+
end)
24+
assert(not status, "Expected an error but got none")
25+
end
26+
27+
local function test_check_range()
28+
local status, err = pcall(function()
29+
checkargs.check_range("test_func", "test_value", 5, 1, 10, true)
30+
end)
31+
assert(status, err)
32+
33+
status, err = pcall(function()
34+
checkargs.check_range("test_func", "test_value", 15, 1, 10, true)
35+
end)
36+
assert(not status, "Expected an error but got none")
37+
end
38+
39+
local function test_check_fields()
40+
local status, err = pcall(function()
41+
checkargs.check_fields("test_func", "test_table", { a = 1, b = 2 }, { "a", "b" }, true)
42+
end)
43+
assert(status, err)
44+
45+
status, err = pcall(function()
46+
checkargs.check_fields("test_func", "test_table", { a = 1 }, { "a", "b" }, true)
47+
end)
48+
assert(not status, "Expected an error but got none")
49+
end
50+
51+
local function test_check_composite()
52+
local status, err = pcall(function()
53+
checkargs.check_composite("test_func", "test_composite", { a = 1, b = "test" }, { a = "number", b = "string" },
54+
true)
55+
end)
56+
assert(status, err)
57+
58+
status, err = pcall(function()
59+
checkargs.check_composite("test_func", "test_composite", { a = 1, b = 2 }, { a = "number", b = "string" }, true)
60+
end)
61+
assert(not status, "Expected an error but got none")
62+
end
63+
64+
local function test_check_not_nil()
65+
local status, err = pcall(function()
66+
checkargs.check_not_nil("test_func", "test_value", "value", true)
67+
end)
68+
assert(status, err)
69+
70+
status, err = pcall(function()
71+
checkargs.check_not_nil("test_func", "test_value", nil, true)
72+
end)
73+
assert(not status, "Expected an error but got none")
74+
end
75+
76+
local function runtests()
77+
test_check_arg()
78+
test_check_list()
79+
test_check_range()
80+
test_check_fields()
81+
test_check_composite()
82+
test_check_not_nil()
83+
print("All tests passed successfully!")
84+
end
85+
86+
runtests()

0 commit comments

Comments
 (0)