Skip to content

Commit

Permalink
refactor: back to supporting pcre (only for regex field)
Browse files Browse the repository at this point in the history
  • Loading branch information
thibaultcha committed May 9, 2015
1 parent f26c24a commit 46525b5
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 26 deletions.
9 changes: 6 additions & 3 deletions kong-0.2.1-1.rockspec
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,22 @@ description = {
}
dependencies = {
"lua ~> 5.1",
"luasec ~> 0.5-2",

"uuid ~> 0.2-1",
"luatz ~> 0.3-1",
"yaml ~> 1.1.1-1",
"luasec ~> 0.5-2",
"lapis ~> 1.1.0-1",
"stringy ~> 0.4-1",
"cassandra ~> 0.5-7",
"multipart ~> 0.1-2",
"lua-path ~> 0.2.3-1",
"lua-cjson ~> 2.1.0-1",
"luasocket ~> 2.0.2-5",
"ansicolors ~> 1.0.2-3",
"multipart ~> 0.1-2"

"luasocket ~> 2.0.2-5",
"lrexlib-pcre ~> 2.8.0-1",
"lua-llthreads2 ~> 0.1.3-1"
}
build = {
type = "builtin",
Expand Down
11 changes: 1 addition & 10 deletions kong/dao/cassandra/apis.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,11 @@ local BaseDao = require "kong.dao.cassandra.base_dao"
local constants = require "kong.constants"
local PluginsConfigurations = require "kong.dao.cassandra.plugins_configurations"

local function is_valid_public_dns(str)
-- check the format
local pattern1 = "[A-Za-z0-9%-]*%.*[A-Za-z0-9%-]*%.[A-Za-z0-9][A-Za-z0-9]+$"
-- check all characters are alphanumeric
local pattern2 = "^[%w%.%-]*$"
-- both checks need to be true to be a valid public_dns
return str:match(pattern1) and str:match(pattern2)
end

local SCHEMA = {
id = { type = constants.DATABASE_TYPES.ID },
name = { type = "string", unique = true, queryable = true, default = function(api_t) return api_t.public_dns end },
public_dns = { type = "string", required = true, unique = true, queryable = true,
func = is_valid_public_dns },
regex = "(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\\-]*[a-zA-Z0-9])\\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\\-]*[A-Za-z0-9])" },
target_url = { type = "string", required = true },
created_at = { type = constants.DATABASE_TYPES.TIMESTAMP }
}
Expand Down
4 changes: 2 additions & 2 deletions kong/dao/cassandra/base_dao.lua
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,8 @@ function BaseDao:_close_session(session)
end
end

local x = "%x"
local uuid_pattern = "^"..table.concat({ x:rep(8), x:rep(4), x:rep(4), x:rep(4), x:rep(12) }, '%-').."$"
local digit = "[0-9a-f]"
local uuid_pattern = "^"..table.concat({ digit:rep(8), digit:rep(4), digit:rep(4), digit:rep(4), digit:rep(12) }, '%-').."$"
local function is_valid_uuid(uuid)
return uuid and uuid:match(uuid_pattern) ~= nil
end
Expand Down
8 changes: 7 additions & 1 deletion kong/dao/schemas.lua
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ function _M.validate(t, schema, is_update)
errors = utils.add_error(errors, column, string.format("\"%s\" is not allowed. Allowed values are: \"%s\"", t[column], table.concat(v.enum, "\", \"")))
end

-- Check field against a regex if specified
elseif t[column] ~= nil and v.regex then
if not ngx.re.match(t[column], v.regex) then
errors = utils.add_error(errors, column, column.." has an invalid value")
end

-- Check field against a custom function
elseif v.func and type(v.func) == "function" then
local ok, err = v.func(t[column], t)
Expand Down Expand Up @@ -102,7 +108,7 @@ function _M.validate(t, schema, is_update)
end

-- Check for unexpected fields in the entity
for k,v in pairs(t) do
for k, v in pairs(t) do
if schema[k] == nil then
errors = utils.add_error(errors, k, k.." is an unknown field")
end
Expand Down
4 changes: 2 additions & 2 deletions kong/tools/io.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
local constants = require "kong.constants"
local path = require("path").new("/")
local yaml = require "yaml"
local path = require("path").new("/")
local stringy = require "stringy"
local constants = require "kong.constants"

local _M = {}

Expand Down
7 changes: 7 additions & 0 deletions spec/ngx_stub.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
local reg = require "rex_pcre"

_G.ngx = {
re = {
match = reg.match
}
}
28 changes: 20 additions & 8 deletions spec/unit/schemas_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ local schemas = require "kong.dao.schemas"
local constants = require "kong.constants"
local validate = schemas.validate

require "spec.ngx_stub"

describe("Schemas", function()

it("should alias lua types to database types", function()
Expand All @@ -19,6 +21,7 @@ describe("Schemas", function()
local schema = {
string = { type = "string", required = true, immutable = true },
table = { type = "table" },
url = { regex = "(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\\-]*[a-zA-Z0-9])\\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\\-]*[A-Za-z0-9])" },
date = { default = 123456, immutable = true },
allowed = { enum = { "hello", "world" }},
boolean_val = { type = "boolean" },
Expand All @@ -40,15 +43,15 @@ describe("Schemas", function()
}

it("should confirm a valid entity is valid", function()
local values = { string = "mockbin entity" }
local values = { string = "mockbin entity", url = "mockbin.com" }

local valid, err = validate(values, schema)
assert.falsy(err)
assert.truthy(valid)
end)

it("should invalidate entity if required property is missing", function()
local values = { table = {"mockbin.com"} }
local values = { url = "mockbin.com" }

local valid, err = validate(values, schema)
assert.falsy(valid)
Expand Down Expand Up @@ -93,15 +96,15 @@ describe("Schemas", function()

it("should set default values if those are variables or functions specified in the validator", function()
-- Variables
local values = { string = "mockbin entity" }
local values = { string = "mockbin entity", url = "mockbin.com" }

local valid, err = validate(values, schema)
assert.falsy(err)
assert.truthy(valid)
assert.are.same(123456, values.date)

-- Functions
local values = { string = "mockbin entity" }
local values = { string = "mockbin entity", url = "mockbin.com" }

local valid, err = validate(values, schema)
assert.falsy(err)
Expand All @@ -111,32 +114,41 @@ describe("Schemas", function()

it("should override default values if specified", function()
-- Variables
local values = { string = "mockbin entity", date = 654321 }
local values = { string = "mockbin entity", url = "mockbin.com", date = 654321 }

local valid, err = validate(values, schema)
assert.falsy(err)
assert.truthy(valid)
assert.are.same(654321, values.date)

-- Functions
local values = { string = "mockbin entity", default = "abcdef" }
local values = { string = "mockbin entity", url = "mockbin.com", default = "abcdef" }

local valid, err = validate(values, schema)
assert.falsy(err)
assert.truthy(valid)
assert.are.same("abcdef", values.default)
end)

it("should validate a field against a regex", function()
local values = { string = "mockbin entity", url = "mockbin_!" }

local valid, err = validate(values, schema)
assert.falsy(valid)
assert.truthy(err)
assert.are.same("url has an invalid value", err.url)
end)

it("should return error when unexpected values are included in the schema", function()
local values = { string = "mockbin entity", unexpected = "abcdef" }
local values = { string = "mockbin entity", url = "mockbin.com", unexpected = "abcdef" }

local valid, err = validate(values, schema)
assert.falsy(valid)
assert.truthy(err)
end)

it("should be able to return multiple errors at once", function()
local values = { unexpected = "abcdef" }
local values = { url = "mockbin.com", unexpected = "abcdef" }

local valid, err = validate(values, schema)
assert.falsy(valid)
Expand Down

0 comments on commit 46525b5

Please sign in to comment.