From f7affc74eff49e81bbad03134867b79892a198e6 Mon Sep 17 00:00:00 2001 From: d9k Date: Wed, 2 May 2018 22:27:19 +0300 Subject: numerical indexes for test data: better understanding --- valua-test.lua | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/valua-test.lua b/valua-test.lua index c12a1a4..fa7f433 100644 --- a/valua-test.lua +++ b/valua-test.lua @@ -17,25 +17,25 @@ local function check(val_test, test_value, expected, n) end 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" + [1] = "test string!", + [2] = "hey", + [3] = "", + [4] = nil, + [5] = true, + [6] = 42, + [7] = 1337, + [8] = '26/10/1980', + [9] = '10-26-1980', + [10] = '29.02.2014', + [11] = '29/02/2016', + [12] = 'a@a.com', + [13] = 'asd123', + [14] = 5.7, + [15] = {}, + [16] = {3,46}, + [17] = "", + [18] = "test-123_maria.2@newdomain.wow.movie", + [19] = "10/06/1980 10:32:10" } local tests = { -- cgit v1.2.3-70-g09d2 From 0d66254547c657eb0158d362d62bbaaa3456dda9 Mon Sep 17 00:00:00 2001 From: d9k Date: Wed, 2 May 2018 22:34:15 +0300 Subject: test cases are splitted to pairs by spaces for better readability --- valua-test.lua | 44 ++++++++++++++++++++++---------------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/valua-test.lua b/valua-test.lua index fa7f433..e358c77 100644 --- a/valua-test.lua +++ b/valua-test.lua @@ -39,29 +39,29 @@ local test_values = { } local tests = { - {v:new().type("string").len(3,5),{1,false}}, + {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().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().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}} } for n,t in ipairs(tests) do -- cgit v1.2.3-70-g09d2 From 9ffa698e15deed8acbd04507371e7c0e666f1bf1 Mon Sep 17 00:00:00 2001 From: d9k Date: Fri, 4 May 2018 14:52:13 +0300 Subject: Added stackable function `optional()`. ref https://github.com/sailorproject/valua/issues/10 --- valua-test.lua | 11 +++++++---- valua.lua | 14 ++++++++++++-- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/valua-test.lua b/valua-test.lua index e358c77..307cd1a 100644 --- a/valua-test.lua +++ b/valua-test.lua @@ -42,13 +42,14 @@ 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}}, +-- require('mobdebug').start('127.0.0.1') {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().min(45), {2,false, 6,false, 7,true, 4,false}}, {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}}, @@ -56,12 +57,14 @@ local tests = { {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().integer(), {14,false, 6,true,}}, + {v:new().string(), {14,false, 1,true, 4,false}}, {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().datetime(), {19,true, 9,false}}, + {v:new().number().min(45).optional(), {2,false, 6,false, 7,true, 4,true}}, + {v:new().string().optional(), {14,false, 1,true, 4,true}}, } for n,t in ipairs(tests) do diff --git a/valua.lua b/valua.lua index 6e1c6e4..11d4741 100644 --- a/valua.lua +++ b/valua.lua @@ -41,8 +41,12 @@ function valua:new(obj) --saves a function named _ 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 @@ -51,6 +55,11 @@ function valua:new(obj) self.__call = function(t,value) local res = true local fres, err + + 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 +73,7 @@ function valua:new(obj) return res,err end obj.funcs = {} + obj.allow_nil = false return obj end -- -- cgit v1.2.3-70-g09d2 From 8f23fce49593deaab5de6c79598389fe86cd6e84 Mon Sep 17 00:00:00 2001 From: d9k Date: Fri, 4 May 2018 15:04:50 +0300 Subject: Added stackable function `optional()`: trying to fix travis warning --- valua.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/valua.lua b/valua.lua index 11d4741..c9589f9 100644 --- a/valua.lua +++ b/valua.lua @@ -54,7 +54,7 @@ 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 fres, err = nil if value == nil and t.allow_nil then return res, err -- cgit v1.2.3-70-g09d2 From a5ebacddc49660f97bfe5e97e3bdfd264803372f Mon Sep 17 00:00:00 2001 From: d9k Date: Fri, 4 May 2018 15:14:14 +0300 Subject: optional() stackable function+. fixed warnings locally with `luacheck --codes --std=max *.lua --ignore 211/_ENV` --- valua.lua | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/valua.lua b/valua.lua index c9589f9..d4d62b9 100644 --- a/valua.lua +++ b/valua.lua @@ -54,7 +54,8 @@ function valua:new(obj) -- __call will run only when the value is validated self.__call = function(t,value) local res = true - local fres, err = nil + local err = nil + local fres if value == nil and t.allow_nil then return res, err -- cgit v1.2.3-70-g09d2 From 56b5189cb9f188a1064296fea3d239a1ae8b7f79 Mon Sep 17 00:00:00 2001 From: d9k Date: Fri, 4 May 2018 22:57:53 +0300 Subject: converted spaces to tabs with `find -name \*.lua -exec bash -c 'unexpand --first-only --tabs=2 "$0" | tee "$0"' {} \;` --- valua-test.lua | 4 ++-- valua.lua | 24 ++++++++++++------------ 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/valua-test.lua b/valua-test.lua index 307cd1a..fa0cfb9 100644 --- a/valua-test.lua +++ b/valua-test.lua @@ -63,8 +63,8 @@ local tests = { {v:new().contains(" "), {2,false, 1,true}}, {v:new().no_white(), {1,false, 2,true}}, {v:new().datetime(), {19,true, 9,false}}, - {v:new().number().min(45).optional(), {2,false, 6,false, 7,true, 4,true}}, - {v:new().string().optional(), {14,false, 1,true, 4,true}}, + {v:new().number().min(45).optional(), {2,false, 6,false, 7,true, 4,true}}, + {v:new().string().optional(), {14,false, 1,true, 4,true}}, } for n,t in ipairs(tests) do diff --git a/valua.lua b/valua.lua index d4d62b9..924b175 100644 --- a/valua.lua +++ b/valua.lua @@ -41,12 +41,12 @@ function valua:new(obj) --saves a function named _ with its args in a funcs table, to be used later when validating return function(...) local args = pack(...) - 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 + 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 @@ -54,12 +54,12 @@ function valua:new(obj) -- __call will run only when the value is validated self.__call = function(t,value) local res = true - local err = nil - local fres + local err = nil + local fres - if value == nil and t.allow_nil then - return res, err - end + 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 @@ -74,7 +74,7 @@ function valua:new(obj) return res,err end obj.funcs = {} - obj.allow_nil = false + obj.allow_nil = false return obj end -- -- cgit v1.2.3-70-g09d2 From f34865745e30cadf458d8a13600869c77bcc3839 Mon Sep 17 00:00:00 2001 From: d9k Date: Fri, 4 May 2018 23:09:24 +0300 Subject: README: documentation for optional() validator construction method. Added test when optional() is not last method in chain. --- README.md | 33 +++++++++++++++++---------------- valua-test.lua | 1 + 2 files changed, 18 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 25a44c6..578cd11 100644 --- a/README.md +++ b/README.md @@ -29,43 +29,44 @@ reusable_validation("test!") -- true #### Current validation functions - * alnum() - + * alnum() - Checks if string is alphanumeric. - * boolean() - + * boolean() - Checks if value is a boolean. - * compare(another_value) - + * compare(another_value) - Checks if value is equal to another value. - * contains(substr) - + * contains(substr) - Checks if a string contains a substring. - * date() or date(format) - + * date() or date(format) - Checks if a string is a valid date. Default format is UK (dd/mm/yyyy). Also checks for US and ISO formats. - * email() - + * email() - Checks if a string is a valid email address. - * empty() - + * empty() - Checks if a value is empty. - * integer() - + * integer() - Checks if a number is an integer; - * in_list(list) - + * in_list(list) - Checks if a value is inside an array. * len(min,max) - Checks if a string's length is between min and max. * match(pattern) - Checks if a string matches a given pattern. - * max(n) - + * max(n) - Checks if a number is equal or less than n. - * min(n) - + * min(n) - Checks if a number is equal or greater than n. - * not_empty() - + * not_empty() - Checks if a value is not empty. - * no_white() - + * no_white() - Checks if a string contains no white spaces. - * number() - + * number() - Checks if a value is a number. - * string() - + * string() - Checks if a value is a string. * type(t) - Checks if a value is of type t. - + * optional(t) - +If value is `nil` it would be accepted. If it's not `nil` it would be processed with other chained validation functions as usually done. Copyright (c) 2014 Etiene Dalcol diff --git a/valua-test.lua b/valua-test.lua index fa0cfb9..3d78740 100644 --- a/valua-test.lua +++ b/valua-test.lua @@ -64,6 +64,7 @@ local tests = { {v:new().no_white(), {1,false, 2,true}}, {v:new().datetime(), {19,true, 9,false}}, {v:new().number().min(45).optional(), {2,false, 6,false, 7,true, 4,true}}, + {v:new().number().optional().min(45), {2,false, 6,false, 7,true, 4,true}}, {v:new().string().optional(), {14,false, 1,true, 4,true}}, } -- cgit v1.2.3-70-g09d2