aboutsummaryrefslogtreecommitdiff
path: root/valua.lua
diff options
context:
space:
mode:
authorEtiene Dalcol <Etiene@users.noreply.github.com>2018-05-14 11:05:32 +0100
committerGitHub <noreply@github.com>2018-05-14 11:05:32 +0100
commita743818e63fad4d1e2578d54422f491bccfdc66e (patch)
tree225e9ccc2d881995e8d299093e941ec34a13b970 /valua.lua
parentb48176ea59c485056b07bd6f01aad5c6407c27d9 (diff)
parentf34865745e30cadf458d8a13600869c77bcc3839 (diff)
downloadvalua-master.tar.gz
valua-master.tar.bz2
valua-master.zip
Merge pull request #19 from d9k/masterHEADmaster
optional() stackable function; changes in tests for better readability
Diffstat (limited to 'valua.lua')
-rw-r--r--valua.lua17
1 files changed, 14 insertions, 3 deletions
diff --git a/valua.lua b/valua.lua
index 6e1c6e4..924b175 100644
--- a/valua.lua
+++ b/valua.lua
@@ -41,8 +41,12 @@ function valua:new(obj)
--saves a function named _<index> with its args in a funcs table, to be used later when validating
return function(...)
local args = pack(...)
- local f = function(value) return valua['_'..k](value, unpack(args, 1, args.n)) end
- tinsert(t.funcs,f)
+ if k == 'optional' then
+ obj.allow_nil = true
+ else
+ local f = function(value) return valua['_'..k](value, unpack(args, 1, args.n)) end
+ tinsert(t.funcs,f)
+ end
return t
end
end
@@ -50,7 +54,13 @@ function valua:new(obj)
-- __call will run only when the value is validated
self.__call = function(t,value)
local res = true
- local fres, err
+ local err = nil
+ local fres
+
+ if value == nil and t.allow_nil then
+ return res, err
+ end
+
-- iterates through all chained validations funcs that were packed, passing the value to be validated
for _,f in ipairs(t.funcs) do
fres,err = f(value)
@@ -64,6 +74,7 @@ function valua:new(obj)
return res,err
end
obj.funcs = {}
+ obj.allow_nil = false
return obj
end
--