Skip to content

Commit f5888c6

Browse files
committed
Supports invalid-value error type
1 parent f27b82b commit f5888c6

File tree

3 files changed

+23
-33
lines changed

3 files changed

+23
-33
lines changed

jmespath/exceptions.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -114,18 +114,16 @@ def __str__(self):
114114

115115
@with_str_method
116116
class JMESPathValueError(JMESPathError):
117-
def __init__(self, function_name, current_value, actual_value,
118-
expected_types):
117+
def __init__(self, function_name, current_value, expected_types):
119118
self.function_name = function_name
120119
self.current_value = current_value
121-
self.actual_value = actual_value
122120
self.expected_types = expected_types
123121

124122
def __str__(self):
125-
return ('In function %s(), invalid value: %s, '
126-
'expected: %s, received: "%s"' % (
123+
return ('In function %s(), invalid value: "%s", '
124+
'expected: %s"%s"' % (
127125
self.function_name, self.current_value,
128-
self.expected_types, self.actual_value))
126+
self.expected_types))
129127

130128
class EmptyExpressionError(JMESPathError):
131129
def __init__(self):

jmespath/functions.py

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -314,8 +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)
317+
self._ensure_integer('find_first', 'start', start)
318+
self._ensure_integer('find_first', 'end', end)
319319
return self._find_impl(
320320
text,
321321
search,
@@ -330,8 +330,8 @@ def _func_find_first(self, text, search, start = 0, end = None):
330330
{'type': 'number', 'optional': True},
331331
{'type': 'number', 'optional': True})
332332
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)
333+
self._ensure_integer('find_last', 'start', start)
334+
self._ensure_integer('find_last', 'end', end)
335335
return self._find_impl(
336336
text,
337337
search,
@@ -367,7 +367,7 @@ def _func_pad_left(self, text, width, padding = ' '):
367367
{'type': 'number'},
368368
{'type': 'string', 'optional': True})
369369
def _func_pad_right(self, text, width, padding = ' '):
370-
self._ensure_non_negative_integer('pad_left', 'width', width)
370+
self._ensure_non_negative_integer('pad_right', 'width', width)
371371
return self._pad_impl(lambda : text.ljust(width, padding), padding)
372372

373373
def _pad_impl(self, func, padding):
@@ -424,14 +424,10 @@ def _ensure_integer(
424424

425425
if param_value != None:
426426
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-
))
427+
raise exceptions.JMESPathValueError(
428+
func_name,
429+
param_value,
430+
"integer")
435431

436432
def _ensure_non_negative_integer(
437433
self,
@@ -441,14 +437,10 @@ def _ensure_non_negative_integer(
441437

442438
if param_value != None:
443439
if int(param_value) != param_value or int(param_value) < 0:
444-
raise exceptions.JMESPathError(
445-
'invalid-type: {}() expects ${} to be a '
446-
'non-negative integer, but received {} instead.'
447-
.format(
448-
func_name,
449-
param_name,
450-
param_value
451-
))
440+
raise exceptions.JMESPathValueError(
441+
func_name,
442+
param_name,
443+
"non-negative integer")
452444

453445
@signature({'type': 'string'}, {'type': 'string', 'optional': True})
454446
def _func_trim(self, text, chars = None):

tests/compliance/string_functions.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
},
2929
{
3030
"expression": "find_first(string, 'string', `1`, '2.4')",
31-
"error": "invalid-type"
31+
"error": "invalid-value"
3232
},
3333

3434
{ "expression": "find_first(string, 'string')", "result": 8 },
@@ -60,12 +60,12 @@
6060

6161
{
6262
"expression": "replace(abab, 'aa', '-', `0.333333`)",
63-
"error": "invalid-type"
63+
"error": "invalid-value"
6464
},
6565

6666
{
6767
"expression": "replace(abab, 'aa', '-', `0.001`)",
68-
"error": "invalid-type"
68+
"error": "invalid-value"
6969
},
7070

7171
{ "expression": "replace(abab, 'aa', '-', `0`)", "result": "aabaaabaaaab" },
@@ -110,8 +110,8 @@
110110

111111
},
112112
{
113-
"expression": "pad_left('string', '1.4')",
114-
"error": "invalid-type"
113+
"expression": "pad_left('string', `1.4`)",
114+
"error": "invalid-value"
115115

116116
},
117117

@@ -127,7 +127,7 @@
127127

128128
{
129129
"expression": "split('/', '/', `3.7`)",
130-
"error": "invalid-type"
130+
"error": "invalid-value"
131131
},
132132

133133
{ "expression": "split('/', '/')", "result": [ "", "" ] },

0 commit comments

Comments
 (0)