Skip to content

Commit

Permalink
feat(schemas) auto-required for sub-schemas
Browse files Browse the repository at this point in the history
This allows for ex the plugins_configurations `value` property to only
be required if a plugin really has a configuration (schema.lua).
  • Loading branch information
thibaultcha committed May 26, 2015
1 parent 88c7a51 commit cd59b34
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 21 deletions.
2 changes: 1 addition & 1 deletion kong/dao/cassandra/plugins_configurations.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ local SCHEMA = {
api_id = { type = constants.DATABASE_TYPES.ID, required = true, foreign = true, queryable = true },
consumer_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 },
value = { type = "table", schema = load_value_schema },
enabled = { type = "boolean", default = true },
created_at = { type = constants.DATABASE_TYPES.TIMESTAMP }
}
Expand Down
12 changes: 8 additions & 4 deletions kong/dao/schemas.lua
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,14 @@ function _M.validate(t, schema, is_update)
end

if sub_schema then
for k, v in pairs(sub_schema) do
if v.default and t[column] == nil then
t[column] = {}
break
-- Check for sub-schema defaults and required properties
for _, sub_field in pairs(sub_schema) do
if t[column] == nil then
if sub_field.default then
t[column] = {}
elseif sub_field.required then -- only check required if field doesn't have a default
v.required = true
end
end
end

Expand Down
51 changes: 35 additions & 16 deletions spec/unit/schemas_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,26 @@ describe("Schemas", function()
assert.truthy(valid)
end)

it("should not return an error when a number is passed as a string", function()
-- Success
local values = { string = "test", number = "10" }

local valid, err = validate(values, schema)
assert.falsy(err)
assert.truthy(valid)
assert.are.same("number", type(values.number))
end)

it("should not return an error when a boolean is passed as a string", function()
-- Success
local values = { string = "test", boolean_val = "false" }

local valid, err = validate(values, schema)
assert.falsy(err)
assert.truthy(valid)
assert.are.same("boolean", type(values.boolean_val))
end)

it("should consider id and timestampd as valid types", function()
local s = { id = { type = "id" } }

Expand Down Expand Up @@ -293,8 +313,7 @@ describe("Schemas", function()
sub_field_required = { required = true },
sub_field_default = { default = "abcd" },
sub_field_number = { type = "number" },
error_loading_sub_sub_schema = {},
sub_sub_schema = { schema = schema_from_function }
error_loading_sub_sub_schema = {}
}
}
}
Expand All @@ -318,6 +337,8 @@ describe("Schemas", function()
end)

it("should validate a property with a sub-schema from a function", function()
nested_schema.sub_schema.schema.sub_sub_schema = { schema = schema_from_function }

-- Success
local values = { some_required = "somestring", sub_schema = {
sub_field_required = "sub value",
Expand Down Expand Up @@ -368,20 +389,6 @@ describe("Schemas", function()
assert.are.same("Error loading the sub-sub-schema", err["sub_schema.sub_sub_schema"])
end)

it("should not return an error when a number is passed as a string", function()
-- Success
local values = { some_required = "somestring",
sub_schema = {
sub_field_required = "sub value",
sub_field_number = "10"
}}

local valid, err = validate(values, nested_schema)
assert.falsy(err)
assert.truthy(valid)
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
Expand All @@ -401,6 +408,18 @@ describe("Schemas", function()
assert.are.same("hello", obj.value.some_property)
end)

it("should mark a value required if sub-schema has a `required`", function()
local schema = {
value = { type = "table", schema = {some_property={required=true}}, func = validate_value }
}

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

end)
end)
end)

0 comments on commit cd59b34

Please sign in to comment.