fix: correct non-transitive keywordComparator in validation.js#123
Open
Suyog241005 wants to merge 1 commit into
Open
fix: correct non-transitive keywordComparator in validation.js#123Suyog241005 wants to merge 1 commit into
Suyog241005 wants to merge 1 commit into
Conversation
The previous comparator always returned -1 or 1, never 0. This violates the mathematical contract that Array.prototype.sort() requires: compare(a, a) must return 0 (reflexivity) When two non-last-keywords are compared against each other, the old comparator returned 1 (meaning 'a > b'), which is incorrect—they are equal in priority. V8's Timsort can produce non-deterministic or incorrect orderings when comparators break this contract. Replace it with a correct, stable comparator that returns 0 for two elements that have the same 'lastness', 1 if only a is a last-keyword, and -1 if only b is a last-keyword.
Contributor
Author
|
Hi @jdesrosiers , I noticed a bug in the keyword sorting logic where the custom comparator for This can cause unstable or incorrect keyword ordering depending on the JavaScript engine. I've submitted a quick PR to fix it using a standard transitive comparator. All linting and tests pass successfully. Let me know what you think! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
In
lib/keywords/validation.js, thekeywordComparatoris used to sort compiled keywords so thatunevaluatedPropertiesandunevaluatedItemsare evaluated last.The previous comparator was defined as:
This comparator violates the
Array.prototype.sort()contract:0(which signifies equal sorting priority).AandBresults inA > BandB > Asimultaneously).Because of this, the sorting behavior becomes non-deterministic and engine-dependent. Depending on the JS engine (V8, SpiderMonkey, JSC) or array size,
unevaluatedProperties/unevaluatedItemscould fail to sort to the end, causing unexpected validation bugs.Solution
Refactored the comparator to correctly compare both inputs and respect the sort contract:
This ensures that: