Skip to content

Commit

Permalink
Merge pull request #253 from Mashape/feature/plugins/default-api-key-…
Browse files Browse the repository at this point in the history
…name

[feature] default key_names to "apikey"
  • Loading branch information
thibaultcha committed May 20, 2015
2 parents 532c91e + 60a72f5 commit 0dada87
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 36 deletions.
9 changes: 5 additions & 4 deletions kong/dao/cassandra/plugins_configurations.lua
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
local constants = require "kong.constants"
local BaseDao = require "kong.dao.cassandra.base_dao"
local cjson = require "cjson"
local utils = require "kong.tools.utils"

local function load_value_schema(plugin_t)
if plugin_t.name then
local status, plugin_schema = pcall(require, "kong.plugins."..plugin_t.name..".schema")
if status then
local loaded, plugin_schema = utils.load_module_if_exists("kong.plugins."..plugin_t.name..".schema")
if loaded then
return plugin_schema
else
return nil, "Plugin \""..(plugin_t.name and plugin_t.name or "").."\" not found"
end
end

return nil, "Plugin \""..(plugin_t.name and plugin_t.name or "").."\" not found"
end

local SCHEMA = {
Expand Down
51 changes: 31 additions & 20 deletions kong/dao/schemas.lua
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,6 @@ function _M.validate(t, schema, is_update)
errors = utils.add_error(errors, column, column.." cannot be updated")
end

-- Check required fields are set
if v.required and (t[column] == nil or t[column] == "") then
errors = utils.add_error(errors, column, column.." is required")
end

-- Check if type is valid boolean and numbers as strings are accepted and converted
if v.type ~= nil and t[column] ~= nil then
local valid
Expand Down Expand Up @@ -93,16 +88,8 @@ function _M.validate(t, schema, is_update)
end
end

-- Check field against a custom function
if v.func and type(v.func) == "function" then
local ok, err = v.func(t[column], t)
if not ok or err then
errors = utils.add_error(errors, column, err)
end
end

-- validate a subschema
if v.schema and t[column] ~= nil then
if v.schema then
local sub_schema, err
if type(v.schema) == "function" then
sub_schema, err = v.schema(t)
Expand All @@ -113,14 +100,38 @@ function _M.validate(t, schema, is_update)
if err then
-- could not retrieve sub schema
errors = utils.add_error(errors, column, err)
else
-- validating subschema
local s_ok, s_errors = _M.validate(t[column], sub_schema, is_update)
if not s_ok then
for s_k, s_v in pairs(s_errors) do
errors = utils.add_error(errors, column.."."..s_k, s_v)
end

if sub_schema then
for k, v in pairs(sub_schema) do
if v.default and t[column] == nil then
t[column] = {}
break
end
end

if t[column] then
-- validating subschema
local s_ok, s_errors = _M.validate(t[column], sub_schema, is_update)
if not s_ok then
for s_k, s_v in pairs(s_errors) do
errors = utils.add_error(errors, column.."."..s_k, s_v)
end
end
end
end
end

-- Check required fields are set
if v.required and (t[column] == nil or t[column] == "") then
errors = utils.add_error(errors, column, column.." is required")
end

-- Check field against a custom function
if v.func and type(v.func) == "function" then
local ok, err = v.func(t[column], t)
if not ok or err then
errors = utils.add_error(errors, column, err)
end
end
end
Expand Down
20 changes: 19 additions & 1 deletion kong/plugins/keyauth/schema.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,22 @@
local utils = require "kong.tools.utils"

local function default_key_names(t)
if not t.key_names then
return {"apikey"}
end
end

local function validate_key_names(t)
if type(t) == "table" and not utils.is_array(t) then
local printable_mt = require "kong.tools.printable"
setmetatable(t, printable_mt)
return false, "key_names must be an array. '"..t.."' is a table. Lua tables must have integer indexes starting at 1."
end

return true
end

return {
key_names = { required = true, type = "table" },
key_names = { required = true, type = "table", default = default_key_names, func = validate_key_names },
hide_credentials = { type = "boolean", default = false }
}
16 changes: 8 additions & 8 deletions kong/plugins/request_transformer/schema.lua
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
return {
add = { require = "false", type = "table", schema = {
form = { required = false, type = "table" },
headers = { required = false, type = "table" },
querystring = { required = false, type = "table" }
add = { type = "table", schema = {
form = { type = "table" },
headers = { type = "table" },
querystring = { type = "table" }
}
},
remove = { require = "false", type = "table", schema = {
form = { required = false, type = "table" },
headers = { required = false, type = "table" },
querystring = { required = false, type = "table" }
remove = { type = "table", schema = {
form = { type = "table" },
headers = { type = "table" },
querystring = { type = "table" }
}
}
}
4 changes: 2 additions & 2 deletions kong/plugins/tcplog/schema.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
return {
host = { required = true, type = "string" },
port = { required = true, type = "number" },
timeout = { required = false, default = 10000, type = "number" },
keepalive = { required = false, default = 60000, type = "number" }
timeout = { default = 10000, type = "number" },
keepalive = { default = 60000, type = "number" }
}
2 changes: 1 addition & 1 deletion kong/plugins/udplog/schema.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
return {
host = { required = true, type = "string" },
port = { required = true, type = "number" },
timeout = { required = false, default = 10000, type = "number" }
timeout = { default = 10000, type = "number" }
}
19 changes: 19 additions & 0 deletions spec/unit/schemas_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,25 @@ describe("Schemas", function()
assert.are.same("number", type(values.sub_schema.sub_field_number))
end)

it("should instanciate a sub-value if sub-schema has a `default` value and do that before `required`", function()
local function validate_value(value)
if not value.some_property then
return false, "value.some_property must not be empty"
end
return true
end

local schema = {
value = { type = "table", schema = {some_property={default="hello"}}, func = validate_value, required = true }
}

local obj = {}
local valid, err = validate(obj, schema)
assert.falsy(err)
assert.True(valid)
assert.are.same("hello", obj.value.some_property)
end)

end)
end)
end)

0 comments on commit 0dada87

Please sign in to comment.