aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.travis.yml30
-rw-r--r--.travis/platform.sh15
-rw-r--r--.travis/setenv_lua.sh3
-rw-r--r--.travis/setup_lua.sh122
-rw-r--r--valua-test.lua90
-rw-r--r--valua.lua130
6 files changed, 128 insertions, 262 deletions
diff --git a/.travis.yml b/.travis.yml
index 27fe9c5..5628fd1 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -1,22 +1,28 @@
-language: c
-
+language: python
sudo: false
env:
- global:
- - LUAROCKS=2.2.1
- matrix:
- - LUA=lua5.1
- - LUA=lua5.2
- - LUA=lua5.3
- - LUA=luajit # latest stable version (2.0.3)
- - LUA=luajit2.0 # current head of 2.0 branch
- #- LUA=luajit2.1 # current head of 2.1 branch
+ - LUA="lua 5.1" COMPAT=default
+ - LUA="lua 5.2" COMPAT=default
+ - LUA="lua 5.2" COMPAT=none
+ - LUA="lua 5.3" COMPAT=default
+ - LUA="lua 5.3" COMPAT=none
+ - LUA="luajit 2.0" COMPAT=none
+ - LUA="luajit 2.0" COMPAT=all
+ - LUA="luajit 2.1" COMPAT=none
+ - LUA="luajit 2.1" COMPAT=all
before_install:
- - source .travis/setenv_lua.sh
+ - pip install hererocks
+ - hererocks HERE --$LUA --compat $COMPAT --no-readline --luarocks latest --verbose
+ - hererocks HERE --show
+ - source HERE/bin/activate
+
+install:
+ - luarocks install luacheck
script:
+ - luacheck --codes --std=max *.lua
- lua valua-test.lua
after_success:
diff --git a/.travis/platform.sh b/.travis/platform.sh
deleted file mode 100644
index 7259a7d..0000000
--- a/.travis/platform.sh
+++ /dev/null
@@ -1,15 +0,0 @@
-if [ -z "${PLATFORM:-}" ]; then
- PLATFORM=$TRAVIS_OS_NAME;
-fi
-
-if [ "$PLATFORM" == "osx" ]; then
- PLATFORM="macosx";
-fi
-
-if [ -z "$PLATFORM" ]; then
- if [ "$(uname)" == "Linux" ]; then
- PLATFORM="linux";
- else
- PLATFORM="macosx";
- fi;
-fi
diff --git a/.travis/setenv_lua.sh b/.travis/setenv_lua.sh
deleted file mode 100644
index 8d8c825..0000000
--- a/.travis/setenv_lua.sh
+++ /dev/null
@@ -1,3 +0,0 @@
-export PATH=${PATH}:$HOME/.lua:$HOME/.local/bin:${TRAVIS_BUILD_DIR}/install/luarocks/bin
-bash .travis/setup_lua.sh
-eval `$HOME/.lua/luarocks path`
diff --git a/.travis/setup_lua.sh b/.travis/setup_lua.sh
deleted file mode 100644
index 8a07c16..0000000
--- a/.travis/setup_lua.sh
+++ /dev/null
@@ -1,122 +0,0 @@
-#! /bin/bash
-
-# A script for setting up environment for travis-ci testing.
-# Sets up Lua and Luarocks.
-# LUA must be "lua5.1", "lua5.2" or "luajit".
-# luajit2.0 - master v2.0
-# luajit2.1 - master v2.1
-
-set -eufo pipefail
-
-LUAJIT_BASE="LuaJIT-2.0.3"
-
-source .travis/platform.sh
-
-LUA_HOME_DIR=$TRAVIS_BUILD_DIR/install/lua
-
-LR_HOME_DIR=$TRAVIS_BUILD_DIR/install/luarocks
-
-mkdir $HOME/.lua
-
-LUAJIT="no"
-
-if [ "$PLATFORM" == "macosx" ]; then
- if [ "$LUA" == "luajit" ]; then
- LUAJIT="yes";
- fi
- if [ "$LUA" == "luajit2.0" ]; then
- LUAJIT="yes";
- fi
- if [ "$LUA" == "luajit2.1" ]; then
- LUAJIT="yes";
- fi;
-elif [ "$(expr substr $LUA 1 6)" == "luajit" ]; then
- LUAJIT="yes";
-fi
-
-mkdir -p "$LUA_HOME_DIR"
-
-if [ "$LUAJIT" == "yes" ]; then
-
- git clone https://github.com/LuaJIT/LuaJIT $LUAJIT_BASE;
-
- cd $LUAJIT_BASE
-
- if [ "$LUA" == "luajit2.1" ]; then
- git checkout v2.1;
- fi
-
- if [ "$LUA" == "luajit" ]; then
- git checkout tags/v2.0.3;
- fi
-
- make && make install PREFIX="$LUA_HOME_DIR"
-
- if [ "$LUA" == "luajit2.1" ]; then
- ln -s $LUA_HOME_DIR/bin/luajit-2.1.0-alpha $HOME/.lua/luajit
- ln -s $LUA_HOME_DIR/bin/luajit-2.1.0-alpha $HOME/.lua/lua;
- else
- ln -s $LUA_HOME_DIR/bin/luajit $HOME/.lua/luajit
- ln -s $LUA_HOME_DIR/bin/luajit $HOME/.lua/lua;
- fi;
-
-else
-
- if [ "$LUA" == "lua5.1" ]; then
- curl http://www.lua.org/ftp/lua-5.1.5.tar.gz | tar xz
- cd lua-5.1.5;
- elif [ "$LUA" == "lua5.2" ]; then
- curl http://www.lua.org/ftp/lua-5.2.4.tar.gz | tar xz
- cd lua-5.2.4;
- elif [ "$LUA" == "lua5.3" ]; then
- curl http://www.lua.org/ftp/lua-5.3.0.tar.gz | tar xz
- cd lua-5.3.0;
- fi
-
- make $PLATFORM
- make INSTALL_TOP="$LUA_HOME_DIR" install;
-
- ln -s $LUA_HOME_DIR/bin/lua $HOME/.lua/lua
- ln -s $LUA_HOME_DIR/bin/luac $HOME/.lua/luac;
-
-fi
-
-cd $TRAVIS_BUILD_DIR
-
-lua -v
-
-LUAROCKS_BASE=luarocks-$LUAROCKS
-
-curl --location http://luarocks.org/releases/$LUAROCKS_BASE.tar.gz | tar xz
-
-cd $LUAROCKS_BASE
-
-if [ "$LUA" == "luajit" ]; then
- ./configure --lua-suffix=jit --with-lua-include="$LUA_HOME_DIR/include/luajit-2.0" --prefix="$LR_HOME_DIR";
-elif [ "$LUA" == "luajit2.0" ]; then
- ./configure --lua-suffix=jit --with-lua-include="$LUA_HOME_DIR/include/luajit-2.0" --prefix="$LR_HOME_DIR";
-elif [ "$LUA" == "luajit2.1" ]; then
- ./configure --lua-suffix=jit --with-lua-include="$LUA_HOME_DIR/include/luajit-2.1" --prefix="$LR_HOME_DIR";
-else
- ./configure --with-lua="$LUA_HOME_DIR" --prefix="$LR_HOME_DIR"
-fi
-
-make build && make install
-
-ln -s $LR_HOME_DIR/bin/luarocks $HOME/.lua/luarocks
-
-cd $TRAVIS_BUILD_DIR
-
-luarocks --version
-
-rm -rf $LUAROCKS_BASE
-
-if [ "$LUAJIT" == "yes" ]; then
- rm -rf $LUAJIT_BASE;
-elif [ "$LUA" == "lua5.1" ]; then
- rm -rf lua-5.1.5;
-elif [ "$LUA" == "lua5.2" ]; then
- rm -rf lua-5.2.4;
-elif [ "$LUA" == "lua5.3" ]; then
- rm -rf lua-5.3.0;
-fi
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},
- "<script>alert('boohoo@email.com XSS');</script>",
- "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},
+ "<script>alert('boohoo@email.com XSS');</script>",
+ "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..4de673b 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 _<index> 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
@@ -49,7 +49,7 @@ function valua:new(obj)
local res = true
local fres, err
-- iterates through all chained validations funcs that were packed, passing the value to be validated
- for i,f in ipairs(t.funcs) do
+ for _,f in ipairs(t.funcs) do
fres,err = f(value)
res = res and fres
-- breaks the chain if a test fails
@@ -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(<value var>,<other var>). 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,'<error message>' if the value fails the test and simply true if it succeeds.
@@ -83,8 +83,8 @@ end
-- String
function valua._len(value,min,max)
- local len = len(value or '')
- if len < min or len >max then return false,"should have "..min.."-"..max.." characters" end
+ local l = len(value or '')
+ if l < min or l > max then return false,"should have "..min.."-"..max.." characters" end
return true
end
@@ -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
--