Skip to content

fix(jmespath): return null for ordering non-numbers and reject mixed types - #5967

Merged
dreamorosi merged 3 commits into
mainfrom
fix/jmespath-ordering-and-mixed-types
Nov 16, 2026
Merged

dreamorosi merged 3 commits into
mainfrom
fix/jmespath-ordering-and-mixed-types

Conversation

@dreamorosi

Copy link
Copy Markdown
Contributor

Summary

Ordering comparators returned undefined when an operand wasn't a number, and that value broke the rest of the expression. sort(), max(), min(), sort_by(), max_by() and min_by() also accepted mixed-type input that the JMESPath specification rejects. They now return null or throw JMESPathTypeError, as the specification says.

Changes

  • <, <=, > and >= return null unless both operands are numbers (TreeInterpreter.ts). This follows the specification, which allows ordering only on numbers. Strings return null too: 'a' < 'b' returned undefined before and now returns null. jmespath.py compares strings here as an extension to the specification, and this change doesn't.
  • sort(), max() and min() check every element of the array and throw when the array mixes types or holds something other than numbers or strings (utils.ts). Before, leading elements that didn't match were skipped, so sort(['b', 1]) and max([null]) succeeded. The error now lists both accepted types (expected one of "array-number", "array-string" but found "array") instead of naming the type of whichever element failed last.
  • sort_by(), max_by() and min_by() throw when the expression returns a mix of numbers and strings. A new typeCheckSortKeys helper in utils.ts replaces the per-element check each function had.
  • The ported compliance test for sort_by(people, &name) expected a sorted result. The upstream functions.json expects invalid-type, so the test now expects an error. The three max/min/sort error messages in the compliance tests were updated to the new wording.
  • index.test.ts has new tests for every case in the issue. 23 of them fail on main.
  • Removed the v8 ignore else comment that kept the missing return out of coverage.

Issue number: closes #5966


By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

Disclaimer: We value your time and bandwidth. As such, any pull requests created on non-triaged issues might not be successful.

…types

Ordering comparators returned undefined when an operand wasn't a number. sort(), max() and min() accepted arrays whose leading elements didn't match the expected type, and sort_by(), max_by() and min_by() accepted keys that mixed numbers and strings.

Fixes #5966
@powertools-for-aws-oss-automation powertools-for-aws-oss-automation Bot added the size/L PRs between 100-499 LOC label Nov 14, 2026
@dreamorosi
dreamorosi marked this pull request as ready for review November 14, 2026 11:00
Comment thread packages/jmespath/src/utils.ts Resolved
typeCheckSortKeys now asserts that every visited key is a number or a string, so max_by, min_by and sort_by no longer need to cast them.
@dreamorosi
dreamorosi requested review from svozza and removed request for leandrodamascena November 16, 2026 15:52
@dreamorosi
dreamorosi enabled auto-merge (squash) November 16, 2026 22:37
@dreamorosi
dreamorosi merged commit 3c0ac7c into main Nov 16, 2026
43 checks passed
@dreamorosi
dreamorosi deleted the fix/jmespath-ordering-and-mixed-types branch November 16, 2026 22:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size/L PRs between 100-499 LOC

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: JMESPath ordering comparators return undefined, and sort/max/min/sort_by/max_by/min_by accept mixed-type arrays

2 participants