@@ -531,6 +531,13 @@ local function to_lua_code(var)
531531 return code .. " }"
532532end
533533
534+ local function addRangeCheck (ctx , op , reference , msg )
535+ ctx :stmt (sformat (' if %s %s %s then' , ctx :param (1 ), op , reference ))
536+ ctx :stmt (sformat (' return false, %s("expected %%s to be %s %s", %s)' ,
537+ ctx :libfunc (' string.format' ), msg , reference , ctx :param (1 )))
538+ ctx :stmt ( ' end' )
539+ end
540+
534541generate_validator = function (ctx , schema )
535542 -- get type informations as they will be necessary anyway
536543 local datatype = ctx :localvartab (sformat (' %s(%s)' ,
@@ -903,33 +910,16 @@ generate_validator = function(ctx, schema)
903910 ctx :stmt (sformat (' if %s == "number" then' , datatype ))
904911
905912 if schema .minimum then
906- local op = ' <'
907- local msg = ' greater'
908- ctx :stmt (sformat (' if %s %s %s then' , ctx :param (1 ), op , schema .minimum ))
909- ctx :stmt (sformat (' return false, %s("expected %%s to be %s than %s", %s)' ,
910- ctx :libfunc (' string.format' ), msg , schema .minimum , ctx :param (1 )))
911- ctx :stmt ( ' end' )
913+ addRangeCheck (ctx , ' <' , schema .minimum , ' at least' )
912914 end
913-
914915 if schema .exclusiveMinimum then
915- ctx :stmt (sformat (' if %s %s %s then' , ctx :param (1 ), " <=" , schema .exclusiveMinimum ))
916- ctx :stmt (sformat (' return false, %s("expected %%s to be %s than %s", %s)' ,
917- ctx :libfunc (' string.format' ), ' strictly greater' , schema .exclusiveMinimum , ctx :param (1 )))
918- ctx :stmt ( ' end' )
916+ addRangeCheck (ctx , ' <=' , schema .exclusiveMinimum , ' greater than' )
919917 end
920-
921918 if schema .maximum then
922- ctx :stmt (sformat (' if %s %s %s then' , ctx :param (1 ), " >" , schema .maximum ))
923- ctx :stmt (sformat (' return false, %s("expected %%s to be %s than %s", %s)' ,
924- ctx :libfunc (' string.format' ), " smaller" , schema .maximum , ctx :param (1 )))
925- ctx :stmt ( ' end' )
919+ addRangeCheck (ctx , ' >' , schema .maximum , ' at most' )
926920 end
927-
928921 if schema .exclusiveMaximum then
929- ctx :stmt (sformat (' if %s %s %s then' , ctx :param (1 ), " >=" , schema .exclusiveMaximum ))
930- ctx :stmt (sformat (' return false, %s("expected %%s to be %s than %s", %s)' ,
931- ctx :libfunc (' string.format' ), ' strictly smaller' , schema .exclusiveMaximum , ctx :param (1 )))
932- ctx :stmt ( ' end' )
922+ addRangeCheck (ctx , ' >=' , schema .exclusiveMaximum , ' smaller than' )
933923 end
934924
935925 local mof = schema .multipleOf
0 commit comments