Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(schema): nested record shorthand type check #10449

Merged
merged 1 commit into from
Mar 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion kong/db/schema/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1704,7 +1704,11 @@ function Schema:process_auto_fields(data, context, nulls, opts)
end
end

value = adjust_field_for_context(field, value, context, nulls, opts)
local err
value, err = adjust_field_for_context(field, value, context, nulls, opts)
if err then
return nil, err
end

if is_select then
local vtype = type(value)
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 @@ -873,6 +873,30 @@ describe("schema", function()
assert.falsy(Test:validate({ f = { r = { a = 2, b = "foo" }}}))
end)

it("validates shorthands type check with nested records", function()
local Test = Schema.new({
fields = {
{ r = {
type = "record",
fields = {
{ a = { type = "string" } },
{ b = { type = "number" } } },
shorthand_fields = {
{
username = {
type = "string",
func = function(value)
return {
b = value
}
end,
}}}}}}})
local input = { r = { username = 123 }}
local ok, err = Test:process_auto_fields(input)
assert.falsy(ok)
assert.same({ username = "expected a string" }, err)
end)

it("validates an integer", function()
local Test = Schema.new({
fields = {
Expand Down