Skip to content

Commit d225507

Browse files
committed
Added tointeger validator from Lua 5.3.
1 parent 1645395 commit d225507

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ Type validators can be used to validate the type of the validated value. These v
6363
* `userdata`
6464
* `function` or `func` (as the function is a reserved keyword in Lua)
6565
* `thread`
66-
* `integer` (works only with Lua > 5.3, `math.type(nbr) == 'integer'`)
67-
* `float` (works only with Lua > 5.3, `math.type(nbr) == 'float'`)
66+
* `integer` (works only with Lua >= 5.3, `math.type(nbr) == 'integer'`)
67+
* `float` (works only with Lua >= 5.3, `math.type(nbr) == 'float'`)
6868
* `file` (`io.type(value) == 'file'`)
6969

7070
#### Example
@@ -102,6 +102,7 @@ Validation factory consist of different validators and filters used to validate
102102
* `unmatch(pattern[, init])`, validates that the value does not match (`string.match`) the pattern
103103
* `tostring()`, converts value to string
104104
* `tonumber([base])`, converts value to number
105+
* `tointeger()`, converts value to integer (works only with Lua >= 5.3, `math.tointeger'`)
105106
* `lower()`, converts value to lower case
106107
* `upper()`, converts value to upper case
107108
* `trim()`, trims whitespace from the left and the right

lib/resty/validation.lua

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,21 @@ local upper = string.upper
99
local pairs = pairs
1010
local gsub = string.gsub
1111
local type = type
12+
local iotype = io.type
13+
local mathtype = math.type
14+
local tointeger = math.tointeger
1215
local len = string.len
1316
if utf8 and utf8.len then
1417
len = utf8.len
1518
end
1619
local function istype(t)
1720
if t == "integer" or t == "float" then
1821
return function(value)
19-
return math.type(value) == t
22+
return mathtype(value) == t
2023
end
2124
elseif t == "file" then
2225
return function(value)
23-
return io.type(value) == t
26+
return iotype(value) == t
2427
end
2528
else
2629
return function(value)
@@ -140,6 +143,12 @@ function factory.tonumber(base)
140143
return nbr ~= nil, nbr
141144
end
142145
end
146+
function factory.tointeger()
147+
return function(value)
148+
local nbr = tointeger(value)
149+
return nbr ~= nil, nbr
150+
end
151+
end
143152
function factory.lower()
144153
return function(value)
145154
if type(value) == "string" or type(value) == "number" then

0 commit comments

Comments
 (0)