diff --git a/kong/db/schema/init.lua b/kong/db/schema/init.lua index eb8b696136c2..aea56c06a560 100644 --- a/kong/db/schema/init.lua +++ b/kong/db/schema/init.lua @@ -495,7 +495,8 @@ Schema.entity_checkers = { -- then_match = { required = true } } -- ``` conditional = { - field_sources = { "if_field" }, + field_sources = { "if_field", "then_field" }, + required_fields = { ["if_field"] = true }, fn = function(entity, arg, schema) local if_value = get_field(entity, arg.if_field) local then_value = get_field(entity, arg.then_field) or null @@ -909,10 +910,12 @@ local function run_entity_check(self, name, input, arg, full_check) end local all_nil = true + local required_fields = checker.required_fields for _, fname in ipairs(fields_to_check) do local value = get_field(input, fname) if value == nil then - if not checker.run_with_missing_fields then + if not (checker.run_with_missing_fields or + (required_fields and not required_fields[fname])) then local err = validation_errors.REQUIRED_FOR_ENTITY_CHECK set_field(field_errors, fname, err) ok = false diff --git a/spec/01-unit/000-new-dao/01-schema/01-schema_spec.lua b/spec/01-unit/000-new-dao/01-schema/01-schema_spec.lua index 0b73ca1766ea..5060dd211ea9 100644 --- a/spec/01-unit/000-new-dao/01-schema/01-schema_spec.lua +++ b/spec/01-unit/000-new-dao/01-schema/01-schema_spec.lua @@ -1430,9 +1430,12 @@ describe("schema", function() redis_host = "example.com", redis_port = 80 })) - assert.falsy(Test:validate_update({ + assert.truthy(Test:validate_update({ policy = "bla", })) + assert.falsy(Test:validate_update({ + policy = "redis", + })) end) it("supports entity checks on nested fields", function() @@ -1476,6 +1479,11 @@ describe("schema", function() } })) assert.falsy(Test:validate_update({ + config = { + policy = "redis", + } + })) + assert.truthy(Test:validate_update({ config = { policy = "bla", } @@ -1536,7 +1544,7 @@ describe("schema", function() { d = { type = "integer" }, }, { e = { type = "boolean" }, }, { f = { type = "string" }, }, - { g = { type = "record", fields = {} }, }, + { g = { type = "record", fields = {} }, }, { h = { type = "map", keys = {}, values = {} }, }, } })