Skip to content
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
50 changes: 49 additions & 1 deletion cv/cv.c
Original file line number Diff line number Diff line change
Expand Up @@ -2528,6 +2528,30 @@ cv_check_array(lua_State *L, struct cv_ctx *ctx,
}
}

/* constraint on the array itself */
if (ok && n->constraint_ref != LUA_NOREF) {
lua_rawgeti(L, LUA_REGISTRYINDEX,
n->constraint_ref);
lua_pushvalue(L, data_idx);
if (lua_pcall(L, 1, 0, 0) != 0) {
int errmsg = lua_gettop(L);
int det = cv_ctx_push_error(L, ctx,
n, "CONSTRAINT_ERROR",
"Field constraint detected"
" error");
if (det != 0) {
lua_pushvalue(L, data_idx);
lua_setfield(L, det, "value");
lua_pushvalue(L, errmsg);
lua_setfield(L, det,
"constraint_error");
lua_pop(L, 1);
}
lua_pop(L, 1); /* errmsg */
return false;
}
}

/* transform on the array itself */
if (ok && !ctx->validate_only &&
n->transform_ref != LUA_NOREF) {
Expand Down Expand Up @@ -2941,7 +2965,31 @@ cv_check_map(lua_State *L, struct cv_ctx *ctx,
}
/* return_unexpected: do nothing extra */

/* --- step 3: transform via pcall --- */
/* --- step 3: constraint via pcall --- */
if (ok && n->constraint_ref != LUA_NOREF) {
lua_rawgeti(L, LUA_REGISTRYINDEX,
n->constraint_ref);
lua_pushvalue(L, data_idx);
if (lua_pcall(L, 1, 0, 0) != 0) {
int errmsg = lua_gettop(L);
int det = cv_ctx_push_error(L, ctx,
n, "CONSTRAINT_ERROR",
"Field constraint detected"
" error");
if (det != 0) {
lua_pushvalue(L, data_idx);
lua_setfield(L, det, "value");
lua_pushvalue(L, errmsg);
lua_setfield(L, det,
"constraint_error");
lua_pop(L, 1);
}
lua_pop(L, 1); /* errmsg */
return false;
}
}

/* --- step 4: transform via pcall --- */
if (ok && !ctx->validate_only &&
n->transform_ref != LUA_NOREF) {
lua_rawgeti(L, LUA_REGISTRYINDEX,
Expand Down
102 changes: 102 additions & 0 deletions test/compat_test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -680,6 +680,108 @@ function g.test_func_constraint()
)
end

function g.test_map_constraint()
-- constraint ok
t.assert_equals(
{
cv.check(
{asd = 123},
{
type = 'map',
properties = { asd = 'number' },
constraint = function(value)
t.assert_equals(
value, {asd = 123})
end
}
)
},
{ {asd = 123}, {} }
)

-- constraint error (false)
t.assert_equals(
{
cv.check(
{asd = 123},
{
type = 'map',
properties = { asd = 'number' },
constraint = function(_value)
error(false, 0)
end
}
)
},
{
nil,
{
{
details = {
constraint_error = false,
value = {asd = 123},
},
message =
"Field constraint detected"
.. " error",
path = "$",
type = "CONSTRAINT_ERROR",
},
}
}
)
end

function g.test_array_constraint()
-- constraint ok
t.assert_equals(
{
cv.check(
{1, 2, 3},
{
type = 'array',
constraint = function(value)
t.assert_equals(
value, {1, 2, 3})
end
}
)
},
{ {1, 2, 3}, {} }
)

-- constraint error (string)
t.assert_equals(
{
cv.check(
{1, 2, 3},
{
type = 'array',
constraint = function(_value)
error("bad array", 0)
end
}
)
},
{
nil,
{
{
details = {
constraint_error = "bad array",
value = {1, 2, 3},
},
message =
"Field constraint detected"
.. " error",
path = "$",
type = "CONSTRAINT_ERROR",
},
}
}
)
end

function g.test_oneof()
t.assert_equals(
{
Expand Down
Loading