Skip to content

Commit

Permalink
fix(schema) handle conditional entity checks on false values
Browse files Browse the repository at this point in the history
  • Loading branch information
hishamhm committed Jun 13, 2019
1 parent fdc5906 commit 326bd9a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
5 changes: 4 additions & 1 deletion kong/db/schema/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -628,7 +628,10 @@ Schema.entity_checkers = {
required_fields = { ["if_field"] = true },
fn = function(entity, arg, schema, errors)
local if_value = get_field(entity, arg.if_field)
local then_value = get_field(entity, arg.then_field) or null
local then_value = get_field(entity, arg.then_field)
if then_value == nil then
then_value = null
end

setmetatable(arg.if_match, {
__index = get_schema_field(schema, arg.if_field)
Expand Down
24 changes: 24 additions & 0 deletions spec/01-unit/01-db/01-schema/01-schema_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1307,6 +1307,30 @@ describe("schema", function()
end)

describe("conditional", function()
it("can check on false", function()
local Test = Schema.new({
fields = {
{ a = { type = "boolean" }, },
{ b = { type = "boolean" }, },
},
entity_checks = {
{ conditional = { if_field = "a",
if_match = { eq = true },
then_field = "b",
then_match = { eq = false },
then_err = "can't have a and b at the same time", }
},
}
})

assert.truthy(Test:validate_insert({ a = true, b = false }))
local ok, errs = Test:validate_insert({ a = true, b = true })
assert.falsy(ok)
assert.same({
"can't have a and b at the same time"
}, errs["@entity"])
end)

it("supports a custom error message", function()
local Test = Schema.new({
fields = {
Expand Down

0 comments on commit 326bd9a

Please sign in to comment.