Skip to content

Commit

Permalink
Merge pull request #84 from Mashape/fix/schemas
Browse files Browse the repository at this point in the history
Fixing schema data types and database types
  • Loading branch information
subnetmarco committed Mar 20, 2015
2 parents d5ce9dd + 11e97ac commit 9410f3c
Show file tree
Hide file tree
Showing 11 changed files with 62 additions and 29 deletions.
10 changes: 10 additions & 0 deletions spec/unit/validations_spec.lua
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
local schemas = require "kong.dao.schemas"
local constants = require "kong.constants"
local validate = schemas.validate

describe("Validation #schema", function()

it("should return the right alias", function()
assert.are.same("number", schemas.get_type("number"))
assert.are.same("string", schemas.get_type("string"))
assert.are.same("boolean", schemas.get_type("boolean"))
assert.are.same("table", schemas.get_type("table"))
assert.are.same("string", schemas.get_type(constants.DATABASE_TYPES.ID))
assert.are.same("number", schemas.get_type(constants.DATABASE_TYPES.TIMESTAMP))
end)

describe("#validate()", function()
-- Ok kids, today we're gonna test a custom validation schema,
-- grab a pair of glasses, this stuff can literally explode.
Expand Down
4 changes: 4 additions & 0 deletions src/constants.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ return {
UNIQUE = "unique",
FOREIGN = "foreign"
},
DATABASE_TYPES = {
ID = "id",
TIMESTAMP = "timestamp"
},
HEADERS = {
SERVER = "Server",
PROXY_TIME = "X-Kong-Proxy-Time",
Expand Down
7 changes: 4 additions & 3 deletions src/dao/cassandra/accounts.lua
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
local BaseDao = require "kong.dao.cassandra.base_dao"
local constants = require "kong.constants"

local SCHEMA = {
id = { type = "id" },
provider_id = { unique = true, queryable = true },
created_at = { type = "timestamp" }
id = { type = constants.DATABASE_TYPES.ID },
provider_id = { type = "string", unique = true, queryable = true },
created_at = { type = constants.DATABASE_TYPES.TIMESTAMP }
}

local Accounts = BaseDao:extend()
Expand Down
11 changes: 6 additions & 5 deletions src/dao/cassandra/apis.lua
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
local BaseDao = require "kong.dao.cassandra.base_dao"
local constants = require "kong.constants"

local SCHEMA = {
id = { type = "id" },
name = { required = true, unique = true, queryable = true },
public_dns = { required = true, unique = true, queryable = true,
id = { type = constants.DATABASE_TYPES.ID },
name = { type = "string", required = true, unique = true, queryable = true },
public_dns = { type = "string", required = true, unique = true, queryable = true,
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 = { required = true },
created_at = { type = "timestamp" }
target_url = { type = "string", required = true },
created_at = { type = constants.DATABASE_TYPES.TIMESTAMP }
}

local Apis = BaseDao:extend()
Expand Down
11 changes: 6 additions & 5 deletions src/dao/cassandra/applications.lua
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
local BaseDao = require "kong.dao.cassandra.base_dao"
local constants = require "kong.constants"

local SCHEMA = {
id = { type = "id" },
account_id = { type = "id", required = true, foreign = true, queryable = true },
public_key = { required = true, unique = true, queryable = true },
secret_key = {},
created_at = { type = "timestamp" }
id = { type = constants.DATABASE_TYPES.ID },
account_id = { type = constants.DATABASE_TYPES.ID, required = true, foreign = true, queryable = true },
public_key = { type = "string", required = true, unique = true, queryable = true },
secret_key = { type = "string" },
created_at = { type = constants.DATABASE_TYPES.TIMESTAMP }
}

local Applications = BaseDao:extend()
Expand Down
4 changes: 2 additions & 2 deletions src/dao/cassandra/base_dao.lua
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,13 @@ local function encode_cassandra_values(schema, t, parameters)
local schema_field = schema[column]
local value = t[column]

if schema_field.type == "id" and value then
if schema_field.type == constants.DATABASE_TYPES.ID and value then
if is_valid_uuid(value) then
value = cassandra.uuid(value)
else
errors = utils.add_error(errors, column, value.." is an invalid uuid")
end
elseif schema_field.type == "timestamp" and value then
elseif schema_field.type == constants.DATABASE_TYPES.TIMESTAMP and value then
value = cassandra.timestamp(value)
elseif value == nil then
value = cassandra.null
Expand Down
10 changes: 5 additions & 5 deletions src/dao/cassandra/plugins.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ local function load_value_schema(plugin_t)
end

local SCHEMA = {
id = { type = "id" },
api_id = { type = "id", required = true, foreign = true, queryable = true },
application_id = { type = "id", foreign = true, queryable = true, default = constants.DATABASE_NULL_ID },
name = { required = true, queryable = true, immutable = true },
id = { type = constants.DATABASE_TYPES.ID },
api_id = { type = constants.DATABASE_TYPES.ID, required = true, foreign = true, queryable = true },
application_id = { type = constants.DATABASE_TYPES.ID, foreign = true, queryable = true, default = constants.DATABASE_NULL_ID },
name = { type = "string", required = true, queryable = true, immutable = true },
value = { type = "table", required = true, schema = load_value_schema },
enabled = { type = "boolean", default = true },
created_at = { type = "timestamp" }
created_at = { type = constants.DATABASE_TYPES.TIMESTAMP }
}

local Plugins = BaseDao:extend()
Expand Down
18 changes: 17 additions & 1 deletion src/dao/schemas.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
local rex = require "rex_pcre" -- Why? Lua has built in pattern which should do the job too
local utils = require "kong.tools.utils"
local constants = require "kong.constants"

local LUA_TYPES = {
boolean = true,
Expand All @@ -8,11 +9,26 @@ local LUA_TYPES = {
table = true
}

local LUA_TYPE_ALIASES = {
[constants.DATABASE_TYPES.ID] = "string",
[constants.DATABASE_TYPES.TIMESTAMP] = "number"
}

--
-- Schemas
--
local _M = {}


-- Returns the proper Lua type from a schema type, handling aliases
-- @param {string} type_val The type of the schema property
-- @return {string} A valid Lua type
function _M.get_type(type_val)
local alias = LUA_TYPE_ALIASES[type_val]
return alias and alias or type_val
end


-- Validate a table against a given schema
-- @param {table} t Table to validate
-- @param {table} schema Schema against which to validate the table
Expand All @@ -38,7 +54,7 @@ function _M.validate(t, schema, is_update)
errors = utils.add_error(errors, column, column.." is required")

-- Check type if valid
elseif v.type ~= nil and t[column] ~= nil and type(t[column]) ~= v.type and LUA_TYPES[v.type] then
elseif v.type ~= nil and t[column] ~= nil and type(t[column]) ~= _M.get_type(v.type) and LUA_TYPES[v.type] then
errors = utils.add_error(errors, column, column.." is not a "..v.type)

-- Check type if value is allowed in the enum
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/ratelimiting/schema.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ local constants = require "kong.constants"

return {
limit = { required = true, type = "number" },
period = { required = true, enum = constants.RATELIMIT.PERIODS }
period = { required = true, type = "string", enum = constants.RATELIMIT.PERIODS }
}
8 changes: 4 additions & 4 deletions src/plugins/tcplog/schema.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
return {
host = { required = true },
port = { required = true },
timeout = { required = false, default = 10000 },
keepalive = { required = false, default = 60000 }
host = { required = true, type = "string" },
port = { required = true, type = "number" },
timeout = { required = false, default = 10000, type = "number" },
keepalive = { required = false, default = 60000, type = "number" }
}
6 changes: 3 additions & 3 deletions src/plugins/udplog/schema.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
return {
host = { required = true },
port = { required = true },
timeout = { required = false, default = 10000 }
host = { required = true, type = "string" },
port = { required = true, type = "number" },
timeout = { required = false, default = 10000, type = "number" }
}

0 comments on commit 9410f3c

Please sign in to comment.