Skip to content
Open
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
10 changes: 8 additions & 2 deletions stdlib/std.jsonnet
Original file line number Diff line number Diff line change
Expand Up @@ -720,6 +720,12 @@ limitations under the License.
else
error 'Unknown code: ' + code.ctype;

local validate_dynamic_format_arg(name, value) =
if std.isInteger(value) then
value
else
error 'Format ' + name + ' must be an integer, got ' + std.type(value);

// Render a parsed format string with an array of values.
local format_codes_arr(codes, arr, i, j, v) =
if i >= std.length(codes) then
Expand All @@ -737,7 +743,7 @@ limitations under the License.
fw: if j >= std.length(arr) then
error ('Not enough values to format: ' + std.length(arr) + ', expected at least ' + j)
else
arr[j],
validate_dynamic_format_arg('width', arr[j]),
} else {
j: j,
fw: code.fw,
Expand All @@ -747,7 +753,7 @@ limitations under the License.
prec: if tmp.j >= std.length(arr) then
error ('Not enough values to format: ' + std.length(arr) + ', expected at least ' + tmp.j)
else
arr[tmp.j],
validate_dynamic_format_arg('precision', arr[tmp.j]),
} else {
j: tmp.j,
prec: code.prec,
Expand Down
1 change: 1 addition & 0 deletions test_suite/error.format.non_integer.jsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
std.format('%*d', [2.5, 42])
1 change: 1 addition & 0 deletions test_suite/error.format.non_integer.jsonnet.golden_regex
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
RUNTIME ERROR: Format width must be an integer, got number
1 change: 1 addition & 0 deletions test_suite/error.format.non_integer_precision.jsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
std.format('%.*f', [2.5, 1.234])
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
RUNTIME ERROR: Format precision must be an integer, got number