Skip to content

Commit

Permalink
Merge pull request Kong#296 from Mashape/fix/schema-strip-strings
Browse files Browse the repository at this point in the history
[fix][schemas] strip string values

Former-commit-id: 0e5b1b6506abd530b5a3ded139cf82eaae19b4a3
  • Loading branch information
thibaultcha committed Jun 4, 2015
2 parents 44f2406 + 0aa50e0 commit 06296d1
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
4 changes: 4 additions & 0 deletions kong/dao/schemas_validation.lua
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ function _M.validate(t, schema, is_update)

-- ALIASES: number, timestamp, boolean and array can be passed as strings and will be converted
if type(t[column]) == "string" then
t[column] = stringy.strip(t[column])
if v.type == "number" or v .type == constants.DATABASE_TYPES.TIMESTAMP then
t[column] = tonumber(t[column])
is_valid_type = t[column] ~= nil
Expand All @@ -66,6 +67,9 @@ function _M.validate(t, schema, is_update)
t[column] = bool == "true"
elseif v.type == "array" then
t[column] = stringy.split(t[column], ",")
for arr_k, arr_v in ipairs(t[column]) do
t[column][arr_k] = stringy.strip(arr_v)
end
is_valid_type = validate_type(v.type, t[column])
else -- if string
is_valid_type = validate_type(v.type, t[column])
Expand Down
3 changes: 2 additions & 1 deletion spec/unit/schemas_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,8 @@ describe("Schemas", function()
it("should alias a string to `array`", function()
local s = { array = { type = "array" } }

local values = { array = "hello,world" }
-- It should also strip the resulting strings
local values = { array = "hello, world" }

local valid, err = validate(values, s)
assert.True(valid)
Expand Down

0 comments on commit 06296d1

Please sign in to comment.