Skip to content
Merged
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
67 changes: 13 additions & 54 deletions packages/jmespath/src/Functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,12 @@ import {
} from '@aws-lambda-powertools/commons/typeutils';
import { JSON_NUMBER_REGEX } from './constants.js';
import type { Expression } from './Expression.js';
import { JMESPathTypeError } from './errors.js';
import type {
FunctionSignatureDecorator,
FunctionSignatureOptions,
JSONObject as JSONObjectType,
} from './types.js';
import { arityCheck, typeCheck } from './utils.js';
import { arityCheck, typeCheck, typeCheckSortKeys } from './utils.js';

/**
* A class that contains the built-in JMESPath functions.
Expand Down Expand Up @@ -238,17 +237,9 @@ class Functions {
arg,
visited: expression.visit(arg),
}));
typeCheckSortKeys(visitedArgs);

const max = visitedArgs.reduce((max, current) => {
const type = getType(current.visited);
if (type !== 'string' && type !== 'number') {
throw new JMESPathTypeError({
currentValue: current.visited,
expectedTypes: ['string'],
actualType: type,
});
}

if (max.visited === current.visited) {
return max;
}
Expand All @@ -260,12 +251,7 @@ class Functions {
? max
: current;
}
// We can safely cast visited to number | string here because we've already
// checked the type at runtime above and we know that it's either a number or a string
return (max.visited as number | string) >
(current.visited as number | string)
? max
: current;
return max.visited > current.visited ? max : current;
}, visitedArgs[0]);

return max.arg;
Expand Down Expand Up @@ -330,17 +316,9 @@ class Functions {
arg,
visited: expression.visit(arg),
}));
typeCheckSortKeys(visitedArgs);

const min = visitedArgs.reduce((min, current) => {
const type = getType(current.visited);
if (type !== 'string' && type !== 'number') {
throw new JMESPathTypeError({
currentValue: current.visited,
expectedTypes: ['string'],
actualType: type,
});
}

if (min.visited === current.visited) {
return min;
}
Expand All @@ -352,12 +330,7 @@ class Functions {
? min
: current;
}
// We can safely cast visited to number | string here because we've already
// checked the type at runtime above and we know that it's either a number or a string
return (min.visited as string | number) <
(current.visited as string | number)
? min
: current;
return min.visited < current.visited ? min : current;
}, visitedArgs[0]);

return min.arg;
Expand Down Expand Up @@ -425,36 +398,22 @@ class Functions {
args: Array<JSONValue>,
expression: Expression
): Array<unknown> {
return args
.map((value, index) => {
const visited = expression.visit(value);
const type = getType(visited);
if (type !== 'string' && type !== 'number') {
throw new JMESPathTypeError({
currentValue: visited,
expectedTypes: ['string'],
actualType: getType(visited),
});
}
const visitedArgs = args.map((value, index) => ({
value,
index,
visited: expression.visit(value),
}));
typeCheckSortKeys(visitedArgs);

return {
value,
index,
visited,
};
})
return visitedArgs
.sort((a, b) => {
if (a.visited === b.visited) {
return a.index - b.index; // Make the sort stable
}
if (typeof a.visited === 'string' && typeof b.visited === 'string') {
return compareStringsByCodePoint(a.visited, b.visited);
}
// We can safely cast visited to number | string here because we've already
// checked the type at runtime above and we know that it's either a number or a string
return (a.visited as string | number) > (b.visited as string | number)
? 1
: -1;
return a.visited > b.visited ? 1 : -1;
})
.map(({ value }) => value); // Extract the original values
}
Expand Down
9 changes: 4 additions & 5 deletions packages/jmespath/src/TreeInterpreter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,7 @@ class TreeInterpreter {
if (comparator === 'ne') {
return !isStrictEqual(left, right);
}
/* v8 ignore else -- @preserve */
if (typeof left === 'number' && typeof right === 'number') {
// Ordering operators only work on numbers. Evaluating them on other
// types will return null.
if (comparator === 'lt') {
return left < right;
}
Expand All @@ -160,9 +157,11 @@ class TreeInterpreter {
}
return left >= right;
}
} else {
throw new JMESPathError(`Invalid comparator: ${comparator}`);
// Ordering operators only work on numbers. Evaluating them on other
// types will return null.
return null;
}
throw new JMESPathError(`Invalid comparator: ${comparator}`);
}

/**
Expand Down
70 changes: 60 additions & 10 deletions packages/jmespath/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,9 +219,7 @@ const checkIfArgumentTypeIsValid = (
actualType: getType(arg),
});
}
checkComplexArrayType(arg, type, hasMoreTypesToCheck);

return true;
return checkComplexArrayType(arg, type, argumentSpec, hasMoreTypesToCheck);
}
if (type === 'expression') {
checkExpressionType(arg, argumentSpec, hasMoreTypesToCheck);
Expand Down Expand Up @@ -265,32 +263,77 @@ const typeCheckType = (
};

/**
* Check if the argument is an array of complex types.
* Check if every element of the array is of the type of a typed array, like `array-number`.
*
* When an element doesn't match and there are more types to check, it returns `false`
* so the next type can be tried. Otherwise it throws, listing every accepted type when
* there is more than one, like `array-number` and `array-string`.
*
* @param arg - The argument to check
* @param type - The type to check against
* @param argumentSpec - The list of types to check against
* @param hasMoreTypesToCheck - Whether there are more types to check
*/
const checkComplexArrayType = (
arg: unknown[],
type: string,
argumentSpec: string[],
hasMoreTypesToCheck: boolean
): void => {
if (!type.includes('-')) return;
): boolean => {
if (!type.includes('-')) return true;
const arrayItemsType = type.slice(6);
let actualType: string | undefined;
for (const element of arg) {
try {
typeCheckArgument(element, [arrayItemsType]);
actualType = arrayItemsType;
} catch (error) {
if (!hasMoreTypesToCheck || actualType !== undefined) {
if (hasMoreTypesToCheck) {
return false;
}
if (argumentSpec.length === 1) {
throw error;
}
throw new JMESPathTypeError({
currentValue: arg,
expectedTypes: argumentSpec,
actualType: getType(arg),
});
}
}

return true;
};

/**
* Check that the keys computed by the expression of `sort_by()`, `max_by()`, or `min_by()`
* are either all numbers or all strings.
*
* @param items - The elements of the array, each with the result of evaluating the expression against it as `visited`
*/
function typeCheckSortKeys<T extends { visited: unknown }>(
items: T[]
): asserts items is Array<T & { visited: number | string }> {
let expectedType: string | undefined;
for (const { visited } of items) {
const actualType = getType(visited);
if (expectedType === undefined) {
if (actualType !== 'string' && actualType !== 'number') {
throw new JMESPathTypeError({
currentValue: visited,
expectedTypes: ['string'],
actualType,
});
}
expectedType = actualType;
} else if (actualType !== expectedType) {
throw new JMESPathTypeError({
currentValue: visited,
expectedTypes: [expectedType],
actualType,
});
}
}
}

/**
* Check if the argument is an expression.
*
Expand Down Expand Up @@ -333,4 +376,11 @@ const checkObjectType = (
}
};

export { arityCheck, isTruthy, sliceArray, typeCheck, typeCheckArgument };
export {
arityCheck,
isTruthy,
sliceArray,
typeCheck,
typeCheckArgument,
typeCheckSortKeys,
};
21 changes: 8 additions & 13 deletions packages/jmespath/tests/unit/compliance/functions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -753,7 +753,7 @@ describe('Functions tests', () => {
{
expression: 'max(array)',
error:
'Invalid argument type for function max(), expected "number" but found "string" in expression: max(array)',
'Invalid argument type for function max(), expected one of "array-number", "array-string" but found "array" in expression: max(array)',
},
])('max() function errors: $expression', ({ expression, error }) => {
// Prepare
Expand Down Expand Up @@ -900,7 +900,7 @@ describe('Functions tests', () => {
{
expression: 'min(array)',
error:
'Invalid argument type for function min(), expected "number" but found "string" in expression: min(array)',
'Invalid argument type for function min(), expected one of "array-number", "array-string" but found "array" in expression: min(array)',
},
])('min() function errors: $expression', ({ expression, error }) => {
// Prepare
Expand Down Expand Up @@ -1081,7 +1081,7 @@ describe('Functions tests', () => {
{
expression: 'sort(array)',
error:
'Invalid argument type for function sort(), expected "number" but found "string" in expression: sort(array)',
'Invalid argument type for function sort(), expected one of "array-number", "array-string" but found "array" in expression: sort(array)',
},
{
expression: 'sort(abc)',
Expand Down Expand Up @@ -1825,16 +1825,6 @@ describe('Functions tests', () => {
expression: 'sort_by(`[]`, &age)',
expected: [],
},
{
expression: 'sort_by(people, &name)',
expected: [
{ age: 10, age_str: '10', bool: true, name: 3 },
{ age: 20, age_str: '20', bool: true, name: 'a', extra: 'foo' },
{ age: 40, age_str: '40', bool: false, name: 'b', extra: 'bar' },
{ age: 30, age_str: '30', bool: true, name: 'c' },
{ age: 50, age_str: '50', bool: false, name: 'd' },
],
},
])('should support sorty_by() special cases', ({ expression, expected }) => {
// Prepare
const data = {
Expand Down Expand Up @@ -1892,6 +1882,11 @@ describe('Functions tests', () => {
error:
'Invalid argument type for function sort_by(), expected "string" but found "boolean" in expression: sort_by(people, &bool)',
},
{
expression: 'sort_by(people, &name)',
error:
'Invalid argument type for function sort_by(), expected "string" but found "number" in expression: sort_by(people, &name)',
},
{
expression: 'sort_by(people, name)',
error:
Expand Down
64 changes: 64 additions & 0 deletions packages/jmespath/tests/unit/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
EmptyExpressionError,
FunctionEvaluationError,
JMESPathError,
JMESPathTypeError,
LexerError,
search,
UnknownFunctionError,
Expand Down Expand Up @@ -91,6 +92,69 @@ describe('Coverage tests', () => {
// Assess
expect(result).toBe('b');
});

it.each([
{ expression: 'a < b' },
{ expression: 'a <= b' },
{ expression: 'a > b' },
{ expression: 'a >= b' },
{ expression: "'a' < 'b'" },
{ expression: '`true` > `false`' },
])(
'returns null when ordering non-numbers: $expression',
({ expression }) => {
// Act
const result = search(expression, { b: 1 });

// Assess
expect(result).toBeNull();
}
);

it.each([
{ expression: 'type(a < b)', expected: 'null' },
{ expression: '{x: a < b, y: `1`}', expected: { x: null, y: 1 } },
{ expression: '(a < b) == c', expected: true },
{ expression: "not_null(a < b, 'fallback')", expected: 'fallback' },
{ expression: '[a < b, `1`][*]', expected: [1] },
])(
'treats the result of ordering non-numbers as null: $expression',
({ expression, expected }) => {
// Act
const result = search(expression, { b: 1 });

// Assess
expect(result).toStrictEqual(expected);
}
);

it.each([
{ expression: 'sort(@)', data: ['b', 1] },
{ expression: 'sort(@)', data: [null, 2, 1] },
{ expression: 'max(@)', data: ['b', 1] },
{ expression: 'max(@)', data: [null] },
{ expression: 'min(@)', data: [true, 5, 2] },
])(
'throws when $expression receives an array of mixed or invalid types: $data',
({ expression, data }) => {
// Act & Assess
expect(() => search(expression, data)).toThrow(
`expected one of "array-number", "array-string" but found "array" in expression: ${expression}`
);
}
);

it.each([
{ expression: 'sort_by(@, &a)', data: [{ a: 2 }, { a: 'x' }] },
{ expression: 'max_by(@, &a)', data: [{ a: 2 }, { a: 'x' }] },
{ expression: 'min_by(@, &a)', data: [{ a: 'x' }, { a: 2 }] },
])(
'throws when $expression evaluates to keys of mixed types',
({ expression, data }) => {
// Act & Assess
expect(() => search(expression, data)).toThrow(JMESPathTypeError);
}
);
});

describe('type checking', () => {
Expand Down
Loading