Skip to content

Commit f71d99d

Browse files
committed
Ensure required integer parameters.
1 parent d2186f9 commit f71d99d

File tree

2 files changed

+52
-6
lines changed

2 files changed

+52
-6
lines changed

jmespath/functions.py

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,8 @@ def _func_keys(self, arg):
314314
{'type': 'number', 'optional': True},
315315
{'type': 'number', 'optional': True})
316316
def _func_find_first(self, text, search, start = 0, end = None):
317+
self._ensure_integer('find_first', start, start)
318+
self._ensure_integer('find_first', end, end)
317319
return self._find_impl(
318320
text,
319321
search,
@@ -328,6 +330,8 @@ def _func_find_first(self, text, search, start = 0, end = None):
328330
{'type': 'number', 'optional': True},
329331
{'type': 'number', 'optional': True})
330332
def _func_find_last(self, text, search, start = 0, end = None):
333+
self._ensure_integer('find_last', start, start)
334+
self._ensure_integer('find_last', end, end)
331335
return self._find_impl(
332336
text,
333337
search,
@@ -355,13 +359,15 @@ def _find_impl(self, text, search, func, start, end):
355359
{'type': 'number'},
356360
{'type': 'string', 'optional': True})
357361
def _func_pad_left(self, text, width, padding = ' '):
362+
self._ensure_non_negative_integer('pad_left', 'width', width)
358363
return self._pad_impl(lambda : text.rjust(width, padding), padding)
359364

360365
@signature(
361366
{'type': 'string'},
362367
{'type': 'number'},
363368
{'type': 'string', 'optional': True})
364369
def _func_pad_right(self, text, width, padding = ' '):
370+
self._ensure_non_negative_integer('pad_left', 'width', width)
365371
return self._pad_impl(lambda : text.ljust(width, padding), padding)
366372

367373
def _pad_impl(self, func, padding):
@@ -378,7 +384,7 @@ def _pad_impl(self, func, padding):
378384
{'type': 'string'},
379385
{'type': 'number', 'optional': True})
380386
def _func_replace(self, text, search, replacement, count = None):
381-
self._ensure_non_negative_optional_integer(
387+
self._ensure_non_negative_integer(
382388
'replace',
383389
'count',
384390
count)
@@ -392,7 +398,7 @@ def _func_replace(self, text, search, replacement, count = None):
392398
{'type': 'string'},
393399
{'type': 'number', 'optional': True})
394400
def _func_split(self, text, search, count = None):
395-
self._ensure_non_negative_optional_integer(
401+
self._ensure_non_negative_integer(
396402
'split',
397403
'count',
398404
count)
@@ -410,7 +416,24 @@ def _func_split(self, text, search, count = None):
410416
return text.split(search, count)
411417
return text.split(search)
412418

413-
def _ensure_non_negative_optional_integer(
419+
def _ensure_integer(
420+
self,
421+
func_name,
422+
param_name,
423+
param_value):
424+
425+
if param_value != None:
426+
if int(param_value) != param_value:
427+
raise exceptions.JMESPathError(
428+
'invalid-type: {}() expects ${} to be a '
429+
'integer, but received {} instead.'
430+
.format(
431+
func_name,
432+
param_name,
433+
param_value
434+
))
435+
436+
def _ensure_non_negative_integer(
414437
self,
415438
func_name,
416439
param_name,
@@ -419,12 +442,12 @@ def _ensure_non_negative_optional_integer(
419442
if param_value != None:
420443
if int(param_value) != param_value or int(param_value) < 0:
421444
raise exceptions.JMESPathError(
422-
'syntax-error: replace() expects ${} to be a '
445+
'invalid-type: {}() expects ${} to be a '
423446
'non-negative integer, but received {} instead.'
424447
.format(
425448
func_name,
426449
param_name,
427-
param_name
450+
param_value
428451
))
429452

430453
@signature({'type': 'string'}, {'type': 'string', 'optional': True})

tests/compliance/string_functions.json

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,14 @@
2222
"expression": "find_first(string, 'string', `1`, '2')",
2323
"error": "invalid-type"
2424
},
25+
{
26+
"expression": "find_first(string, 'string', `1.3`, '2')",
27+
"error": "invalid-type"
28+
},
29+
{
30+
"expression": "find_first(string, 'string', `1`, '2.4')",
31+
"error": "invalid-type"
32+
},
2533

2634
{ "expression": "find_first(string, 'string')", "result": 8 },
2735
{ "expression": "find_first(string, 'string', `0`)", "result": 8 },
@@ -52,7 +60,12 @@
5260

5361
{
5462
"expression": "replace(abab, 'aa', '-', `0.333333`)",
55-
"error": "syntax"
63+
"error": "invalid-type"
64+
},
65+
66+
{
67+
"expression": "replace(abab, 'aa', '-', `0.001`)",
68+
"error": "invalid-type"
5669
},
5770

5871
{ "expression": "replace(abab, 'aa', '-', `0`)", "result": "aabaaabaaaab" },
@@ -95,6 +108,11 @@
95108
"expression": "pad_left('string', `1`, '--')",
96109
"error": "syntax"
97110

111+
},
112+
{
113+
"expression": "pad_left('string', '1.4')",
114+
"error": "invalid-type"
115+
98116
},
99117

100118
{ "expression": "pad_left('string', `0`)", "result": "string" },
@@ -107,6 +125,11 @@
107125
{ "expression": "pad_right('string', `10`)", "result": "string " },
108126
{ "expression": "pad_right('string', `10`, '-')", "result": "string----" },
109127

128+
{
129+
"expression": "split('/', '/', `3.7`)",
130+
"error": "invalid-type"
131+
},
132+
110133
{ "expression": "split('/', '/')", "result": [ "", "" ] },
111134
{ "expression": "split('', '')", "result": [ ] },
112135
{ "expression": "split('all chars', '')", "result": [ "a", "l", "l", " ", "c", "h", "a", "r", "s" ] },

0 commit comments

Comments
 (0)