From 56809323fa51316b61c33f08cf0a17b601c849cb Mon Sep 17 00:00:00 2001 From: Francois Perrad Date: Wed, 20 Sep 2017 20:37:44 +0200 Subject: fix indentation & trailing spaces --- valua-test.lua | 90 ++++++++++++++++++++--------------------- valua.lua | 124 ++++++++++++++++++++++++++++----------------------------- 2 files changed, 107 insertions(+), 107 deletions(-) diff --git a/valua-test.lua b/valua-test.lua index 5f75fdd..a4cb0dd 100644 --- a/valua-test.lua +++ b/valua-test.lua @@ -1,67 +1,67 @@ local v = require "valua" local passing = true -local function check(val_test, test_value, expected, n) +local function check(val_test, test_value, expected, n) local res,err = val_test(test_value) local msg = "Validation "..n.." " - if res == expected then + if res == expected then msg = msg.. "succeeded" - else - passing = false - msg = msg.. " \27[31m FAILED \27[0m" + else + passing = false + msg = msg.. " \27[31m FAILED \27[0m" end - msg = msg.." on '"..(tostring(test_value)).."'. Expected: "..tostring(expected)..", result: "..tostring(res)..". " + msg = msg.." on '"..(tostring(test_value)).."'. Expected: "..tostring(expected)..", result: "..tostring(res)..". " print(msg) - if err then print("\tTest Msg: value "..(err or "")) end -end + if err then print("\tTest Msg: value "..(err or "")) end +end -local test_values = { - "test string!", +local test_values = { + "test string!", "hey", "", - nil, - true, - 42, - 1337, - '26/10/1980', - '10-26-1980', - '29.02.2014', - '29/02/2016', - 'a@a.com', - 'asd123', - 5.7, - {}, - {3,46}, - "", - "test-123_maria.2@newdomain.wow.movie", - "10/06/1980 10:32:10" + nil, + true, + 42, + 1337, + '26/10/1980', + '10-26-1980', + '29.02.2014', + '29/02/2016', + 'a@a.com', + 'asd123', + 5.7, + {}, + {3,46}, + "", + "test-123_maria.2@newdomain.wow.movie", + "10/06/1980 10:32:10" } local tests = { {v:new().type("string").len(3,5),{1,false}}, {v:new().type("number").len(3,5), {1,false}}, - {v:new().type("table").empty(),{15,true,16,false,1,false}}, + {v:new().type("table").empty(),{15,true,16,false,1,false}}, {v:new().not_empty(),{2,true,3,false,4,false,16,true,5,true,6,true}}, {v:new().len(2,10),{2,true}}, {v:new().type("number"),{2,false}}, {v:new().empty(),{3,true,4,true,5,false,6,false}}, - {v:new().boolean(),{1,false,5,true}}, - {v:new().compare("hey"),{1,false,2,true}}, - {v:new().number().min(45),{2,false,6,false,7,true}}, - {v:new().number().max(1009),{7,false,6,true}}, - {v:new().date(),{9,false,10,false,11,true,8,true}}, - {v:new().date('us'),{8,false,9,true}}, - {v:new().email(),{13,false,12,true,17,false,18,true}}, - {v:new().in_list({"hey",42}),{12,false,6,true,2,true}}, - {v:new().match("^%d+%p%d+%p%d%d%d%d$"),{1,false,8,true}}, - {v:new().alnum(),{8,false,13,true}}, - {v:new().integer(),{14,false,6,true}}, - {v:new().string(),{14,false,1,true}}, - {v:new().string().alnum(),{6,false}}, - {v:new().contains(" "),{2,false,1,true}}, - {v:new().no_white(),{1,false,2,true}}, - {v:new().datetime(),{19,true,9,false}} + {v:new().boolean(),{1,false,5,true}}, + {v:new().compare("hey"),{1,false,2,true}}, + {v:new().number().min(45),{2,false,6,false,7,true}}, + {v:new().number().max(1009),{7,false,6,true}}, + {v:new().date(),{9,false,10,false,11,true,8,true}}, + {v:new().date('us'),{8,false,9,true}}, + {v:new().email(),{13,false,12,true,17,false,18,true}}, + {v:new().in_list({"hey",42}),{12,false,6,true,2,true}}, + {v:new().match("^%d+%p%d+%p%d%d%d%d$"),{1,false,8,true}}, + {v:new().alnum(),{8,false,13,true}}, + {v:new().integer(),{14,false,6,true}}, + {v:new().string(),{14,false,1,true}}, + {v:new().string().alnum(),{6,false}}, + {v:new().contains(" "),{2,false,1,true}}, + {v:new().no_white(),{1,false,2,true}}, + {v:new().datetime(),{19,true,9,false}} } for n,t in ipairs(tests) do @@ -71,5 +71,5 @@ for n,t in ipairs(tests) do end if not passing then - error('Tests are failing') -end \ No newline at end of file + error('Tests are failing') +end diff --git a/valua.lua b/valua.lua index cec04fb..c60dc3d 100644 --- a/valua.lua +++ b/valua.lua @@ -36,12 +36,12 @@ function valua:new(obj) -- __index will be called always when chaining validation functions self.__index = function(t,k) --saves a function named _ with its args in a funcs table, to be used later when validating - return function(...) + return function(...) local args = pack(...) local f = function(value) return valua['_'..k](value, unpack(args, 1, args.n)) end tinsert(t.funcs,f) - return t - end + return t + end end -- __call will run only when the value is validated @@ -66,12 +66,12 @@ end -- -- VALIDATION FUNCS --- Add new funcs at will, they all should have the value to be validated as first parameter +-- Add new funcs at will, they all should have the value to be validated as first parameter -- and their names must be preceded by '_'. --- For example, if you want to use .custom_val(42) on your validation chain, you need to create a +-- For example, if you want to use .custom_val(42) on your validation chain, you need to create a -- function valua._custom_val(,). Just remember the value var will be known -- at the end of the chain and the other var, in this case, will receive '42'. You can add multiple other vars. --- These functions can be called directly (valua._len("test",2,5))in a non-chained and isolated way of life +-- These functions can be called directly (valua._len("test",2,5))in a non-chained and isolated way of life -- for quick stuff, but chaining is much cooler! -- Return false,'' if the value fails the test and simply true if it succeeds. @@ -95,9 +95,9 @@ end function valua._email(value) if not empty(value) and not value:match("^[%w+%.%-_]+@[%w+%.%-_]+%.%a%a+$") then - return false, "is not a valid email address" - end - return true + return false, "is not a valid email address" + end + return true end function valua._match(value,pattern) @@ -142,33 +142,33 @@ end -- Check for a UK date pattern dd/mm/yyyy , dd-mm-yyyy, dd.mm.yyyy -- or US pattern mm/dd/yyyy, mm-dd-yyyy, mm.dd.yyyy --- or ISO pattern yyyy/mm/dd, yyyy-mm-dd, yyyy.mm.dd +-- or ISO pattern yyyy/mm/dd, yyyy-mm-dd, yyyy.mm.dd -- Default is UK function valua._date(value,format) - local valid = true - if (match(value, "^%d+%p%d+%p%d%d%d%d$")) then - local d, m, y - if format and format:lower() == 'us' then - m, d, y = match(value, "(%d+)%p(%d+)%p(%d+)") - elseif format and format:lower() == 'iso' then - y, m, d = match(value, "(%d+)%p(%d+)%p(%d+)") - else - d, m, y = match(value, "(%d+)%p(%d+)%p(%d+)") - end - d, m, y = tonumber(d), tonumber(m), tonumber(y) - - local dm2 = d*m*m - if d>31 or m>12 or dm2==116 or dm2==120 or dm2==124 or dm2==496 or dm2==1116 or dm2==2511 or dm2==3751 then - -- invalid unless leap year - if not (dm2==116 and (y%400 == 0 or (y%100 ~= 0 and y%4 == 0))) then - valid = false - end - end - else - valid = false - end - if not valid then return false, "is not a valid date" end - return true + local valid = true + if (match(value, "^%d+%p%d+%p%d%d%d%d$")) then + local d, m, y + if format and format:lower() == 'us' then + m, d, y = match(value, "(%d+)%p(%d+)%p(%d+)") + elseif format and format:lower() == 'iso' then + y, m, d = match(value, "(%d+)%p(%d+)%p(%d+)") + else + d, m, y = match(value, "(%d+)%p(%d+)%p(%d+)") + end + d, m, y = tonumber(d), tonumber(m), tonumber(y) + + local dm2 = d*m*m + if d>31 or m>12 or dm2==116 or dm2==120 or dm2==124 or dm2==496 or dm2==1116 or dm2==2511 or dm2==3751 then + -- invalid unless leap year + if not (dm2==116 and (y%400 == 0 or (y%100 ~= 0 and y%4 == 0))) then + valid = false + end + end + else + valid = false + end + if not valid then return false, "is not a valid date" end + return true end -- @@ -176,47 +176,47 @@ end -- Check for a UK date pattern dd/mm/yyyy hh:mi:ss, dd-mm-yyyy, dd.mm.yyyy -- or US pattern mm/dd/yyyy, mm-dd-yyyy, mm.dd.yyyy --- or ISO pattern yyyy/mm/dd, yyyy-mm-dd, yyyy.mm.dd +-- or ISO pattern yyyy/mm/dd, yyyy-mm-dd, yyyy.mm.dd -- Default is UK function valua._datetime(value,format) - local valid = true - if (match(value, "^%d+%p%d+%p%d%d%d%d %d%d%p%d%d%p%d%d$")) then - local d, m, y, hh, mm, ss - if format and format:lower() == 'us' then - m, d, y, hh, mm, ss = match(value, "(%d+)%p(%d+)%p(%d+) (%d%d)%p(%d%d)%p(%d%d)") - elseif format and format:lower() == 'iso' then - y, m, d, hh, mm, ss = match(value, "(%d+)%p(%d+)%p(%d+) (%d%d)%p(%d%d)%p(%d%d)") - else - d, m, y, hh, mm, ss = match(value, "(%d+)%p(%d+)%p(%d+) (%d%d)%p(%d%d)%p(%d%d)") - end - d, m, y, hh, mm, ss = tonumber(d), tonumber(m), tonumber(y), tonumber(hh), tonumber(mm), tonumber(ss) - - local dm2 = d*m*m - if d>31 or m>12 or dm2==116 or dm2==120 or dm2==124 or dm2==496 or dm2==1116 or dm2==2511 or dm2==3751 then - -- invalid unless leap year - if not (dm2==116 and (y%400 == 0 or (y%100 ~= 0 and y%4 == 0))) then - valid = false - end - end + local valid = true + if (match(value, "^%d+%p%d+%p%d%d%d%d %d%d%p%d%d%p%d%d$")) then + local d, m, y, hh, mm, ss + if format and format:lower() == 'us' then + m, d, y, hh, mm, ss = match(value, "(%d+)%p(%d+)%p(%d+) (%d%d)%p(%d%d)%p(%d%d)") + elseif format and format:lower() == 'iso' then + y, m, d, hh, mm, ss = match(value, "(%d+)%p(%d+)%p(%d+) (%d%d)%p(%d%d)%p(%d%d)") + else + d, m, y, hh, mm, ss = match(value, "(%d+)%p(%d+)%p(%d+) (%d%d)%p(%d%d)%p(%d%d)") + end + d, m, y, hh, mm, ss = tonumber(d), tonumber(m), tonumber(y), tonumber(hh), tonumber(mm), tonumber(ss) + + local dm2 = d*m*m + if d>31 or m>12 or dm2==116 or dm2==120 or dm2==124 or dm2==496 or dm2==1116 or dm2==2511 or dm2==3751 then + -- invalid unless leap year + if not (dm2==116 and (y%400 == 0 or (y%100 ~= 0 and y%4 == 0))) then + valid = false + end + end -- time validation if not (hh >= 0 and hh <= 24) then valid = false - end + end if not (mm >= 0 and mm <= 60) then valid = false - end + end if not (ss >= 0 and ss <= 60) then valid = false end - - else - valid = false - end - if not valid then return false, "is not a valid datetime" end - return true + + else + valid = false + end + if not valid then return false, "is not a valid datetime" end + return true end -- -- cgit v1.2.3-70-g09d2