From d3a605e4f8427551b86eb39b3d2d7f3a833e2002 Mon Sep 17 00:00:00 2001 From: ypnos Date: Mon, 22 Feb 2021 16:23:41 +0100 Subject: [PATCH 1/2] Clarify numerical validation errors Current messages for `minimum` and `maximum` were misleading for a wide audience. Messages are now changed to "at least" for `minimum` and "at most" for `maximum`. Code simplified. --- lib/jsonschema.lua | 31 ++++++++++--------------------- 1 file changed, 10 insertions(+), 21 deletions(-) diff --git a/lib/jsonschema.lua b/lib/jsonschema.lua index f3dffb4..85ef175 100644 --- a/lib/jsonschema.lua +++ b/lib/jsonschema.lua @@ -895,34 +895,23 @@ generate_validator = function(ctx, schema) if schema.minimum or schema.maximum or schema.multipleOf or schema.exclusiveMinimum or schema.exclusiveMaximum then ctx:stmt(sformat('if %s == "number" then', datatype)) - if schema.minimum then - local op = '<' - local msg = 'greater' - ctx:stmt(sformat(' if %s %s %s then', ctx:param(1), op, schema.minimum)) - ctx:stmt(sformat(' return false, %s("expected %%s to be %s than %s", %s)', - ctx:libfunc('string.format'), msg, schema.minimum, ctx:param(1))) + local addRangeCheck = function (op, reference, msg) + ctx:stmt(sformat(' if %s %s %s then', ctx:param(1), op, reference)) + ctx:stmt(sformat(' return false, %s("expected %%s to be %s %s", %s)', + ctx:libfunc('string.format'), msg, reference, ctx:param(1))) ctx:stmt( ' end') end - + if schema.minimum then + addRangeCheck('<', schema.minimum, 'at least') + end if schema.exclusiveMinimum then - ctx:stmt(sformat(' if %s %s %s then', ctx:param(1), "<=", schema.exclusiveMinimum)) - ctx:stmt(sformat(' return false, %s("expected %%s to be %s than %s", %s)', - ctx:libfunc('string.format'), 'strictly greater', schema.exclusiveMinimum, ctx:param(1))) - ctx:stmt( ' end') + addRangeCheck('<=', schema.exclusiveMinimum, 'greater than') end - if schema.maximum then - ctx:stmt(sformat(' if %s %s %s then', ctx:param(1), ">", schema.maximum)) - ctx:stmt(sformat(' return false, %s("expected %%s to be %s than %s", %s)', - ctx:libfunc('string.format'), "smaller", schema.maximum, ctx:param(1))) - ctx:stmt( ' end') + addRangeCheck('>', schema.maximum, 'at most') end - if schema.exclusiveMaximum then - ctx:stmt(sformat(' if %s %s %s then', ctx:param(1), ">=", schema.exclusiveMaximum)) - ctx:stmt(sformat(' return false, %s("expected %%s to be %s than %s", %s)', - ctx:libfunc('string.format'), 'strictly smaller', schema.exclusiveMaximum, ctx:param(1))) - ctx:stmt( ' end') + addRangeCheck('>=', schema.exclusiveMaximum, 'smaller than') end local mof = schema.multipleOf From e8a71cf002e7b974156e7a2451cd0fa768594198 Mon Sep 17 00:00:00 2001 From: ypnos Date: Mon, 19 Apr 2021 12:11:45 +0200 Subject: [PATCH 2/2] Avoid closure for better performance --- lib/jsonschema.lua | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/lib/jsonschema.lua b/lib/jsonschema.lua index 85ef175..2ca9c29 100644 --- a/lib/jsonschema.lua +++ b/lib/jsonschema.lua @@ -524,6 +524,13 @@ local function to_lua_code(var) return code .. "}" end +local function addRangeCheck(ctx, op, reference, msg) + ctx:stmt(sformat(' if %s %s %s then', ctx:param(1), op, reference)) + ctx:stmt(sformat(' return false, %s("expected %%s to be %s %s", %s)', + ctx:libfunc('string.format'), msg, reference, ctx:param(1))) + ctx:stmt( ' end') +end + generate_validator = function(ctx, schema) -- get type informations as they will be necessary anyway local datatype = ctx:localvartab(sformat('%s(%s)', @@ -895,23 +902,17 @@ generate_validator = function(ctx, schema) if schema.minimum or schema.maximum or schema.multipleOf or schema.exclusiveMinimum or schema.exclusiveMaximum then ctx:stmt(sformat('if %s == "number" then', datatype)) - local addRangeCheck = function (op, reference, msg) - ctx:stmt(sformat(' if %s %s %s then', ctx:param(1), op, reference)) - ctx:stmt(sformat(' return false, %s("expected %%s to be %s %s", %s)', - ctx:libfunc('string.format'), msg, reference, ctx:param(1))) - ctx:stmt( ' end') - end if schema.minimum then - addRangeCheck('<', schema.minimum, 'at least') + addRangeCheck(ctx, '<', schema.minimum, 'at least') end if schema.exclusiveMinimum then - addRangeCheck('<=', schema.exclusiveMinimum, 'greater than') + addRangeCheck(ctx, '<=', schema.exclusiveMinimum, 'greater than') end if schema.maximum then - addRangeCheck('>', schema.maximum, 'at most') + addRangeCheck(ctx, '>', schema.maximum, 'at most') end if schema.exclusiveMaximum then - addRangeCheck('>=', schema.exclusiveMaximum, 'smaller than') + addRangeCheck(ctx, '>=', schema.exclusiveMaximum, 'smaller than') end local mof = schema.multipleOf