Skip to content

Commit 9495882

Browse files
committed
Merge pull request Kong#37 from Mashape/fix/schema-validation
Fixing schema validation"
2 parents 306acf5 + 6fdfa4d commit 9495882

File tree

3 files changed

+21
-17
lines changed

3 files changed

+21
-17
lines changed

spec/unit/validations_spec.lua

+7-4
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,14 @@ describe("Validation #schema", function()
1515
allowed = { enum = { "hello", "world" }},
1616
default = { default = function() return "default" end },
1717
custom = { func = function(v, t)
18-
if v and t.default == "default" then
19-
return true
18+
if v then
19+
if t.default == "default" then
20+
return true
21+
else
22+
return false, "Nah"
23+
end
2024
else
21-
return false, "Nah"
25+
return true
2226
end
2327
end },
2428
sub_schema = {
@@ -186,7 +190,6 @@ describe("Validation #schema", function()
186190
describe("Sub-schemas", function()
187191

188192
it("should validate a property with a sub-schema", function()
189-
pending("todo")
190193
-- Success
191194
local values = { string = "somestring", sub_schema = { sub_field_required = "sub value" }}
192195

src/kong/dao/schemas.lua

+7-2
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ function _M.validate(t, schema, is_update)
5555
end
5656

5757
-- Check field against a custom function
58-
elseif t[column] ~= nil and v.func and type(v.func) == "function" then
58+
elseif v.func and type(v.func) == "function" then
5959
local ok, err = v.func(t[column], t)
6060
if not ok or err then
6161
errors = utils.add_error(errors, column, err)
@@ -67,7 +67,12 @@ function _M.validate(t, schema, is_update)
6767

6868
-- validate a subschema
6969
elseif t[column] ~= nil and v.schema then
70-
70+
local s_ok, s_errors = _M.validate(t[column], v.schema, is_update)
71+
if not s_ok then
72+
for s_k, s_v in pairs(s_errors) do
73+
errors = utils.add_error(errors, column.."."..s_k, s_v)
74+
end
75+
end
7176
end
7277
end
7378

src/kong/plugins/authentication/schema.lua

+7-11
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,16 @@
11
local constants = require "kong.constants"
2+
local utils = require "kong.tools.utils"
23
local stringy = require "stringy"
34

45
local function check_authentication_key_names(names, plugin_value)
5-
if plugin_value.authentication_type == constants.AUTHENTICATION.BASIC then
6-
return false, "This field is not available for \""..BASIC.."\" authentication"
7-
elseif plugin_value.authentication_type ~= BASIC then
8-
if names then
9-
if type(names) == "table" then
10-
return true
11-
else
12-
return false, "You need to specify an array"
13-
end
14-
else
15-
return false, "This field is required for query and header authentication"
6+
if plugin_value.authentication_type == constants.AUTHENTICATION.BASIC and names then
7+
return false, "This field is not available for \""..constants.AUTHENTICATION.BASIC.."\" authentication"
8+
elseif plugin_value.authentication_type ~= constants.AUTHENTICATION.BASIC then
9+
if not names or type(names) ~= "table" or utils.table_size(names) == 0 then
10+
return false, "You need to specify an array with at least one value"
1611
end
1712
end
13+
return true
1814
end
1915

2016
return {

0 commit comments

Comments
 (0)