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 cf94bce
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 23 deletions.
5 changes: 4 additions & 1 deletion kong-0.2.1-1.rockspec
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ dependencies = {
"lua-cjson ~> 2.1.0-1",
"luasocket ~> 2.0.2-5",
"ansicolors ~> 1.0.2-3",
"multipart ~> 0.1-2"
"multipart ~> 0.1-2",

"lua-llthreads2 ~> 0.1.3-1",
"lrexlib-pcre ~> 2.8.0-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
7 changes: 7 additions & 0 deletions kong/dao/schemas.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
local reg = require "rex_pcre"
local utils = require "kong.tools.utils"
local constants = require "kong.constants"

Expand Down Expand Up @@ -66,6 +67,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 reg.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
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
26 changes: 18 additions & 8 deletions spec/unit/schemas_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,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 +41,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 +94,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 +112,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 cf94bce

Please sign in to comment.