fix(no-enum-type-mismatch): report every value when type is an array - #3050
Open
luantaraschi wants to merge 1 commit into
Open
fix(no-enum-type-mismatch): report every value when type is an array#3050luantaraschi wants to merge 1 commit into
luantaraschi wants to merge 1 commit into
Conversation
The array-type branch collected its results in an object keyed by the enum value. Object keys are strings, so 1 and '1' shared a key and the report earned by the number was deleted when the string passed. The location then looked the key up with indexOf against the original array, which returns -1 for anything that is not a string, so non-string values were reported at a pointer that does not exist and Object.keys put them out of document order on the way. Walk the values with their index and report the ones that match none of the listed types, the way the scalar branch above already does.
🦋 Changeset detectedLatest commit: 9bdf0be The changes in this PR will be included in the next version bump. This PR includes changesets to release 4 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
Contributor
Performance Benchmark (Lower is Faster)
|
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.
When
typeis written as an array,no-enum-type-mismatchdrops violations and points at an index that does not exist.redocly lintsays the description is valid. Writing the same constraint astype: stringreports the number, so the array form loses it. It is not about single element arrays either:type: [string, boolean]with the same enum is also silent.The second symptom shows up as soon as the offending value is not a string:
Both are reported, but
2comes out first at pointer.../schema/enum/-1, beforezzatenum/0. That pointer goes out throughlint -f json,--format=checkstyleand--generate-ignore-file, andgetAstNodeByPointerdoesitems[parseInt('-1', 10)], getsundefinedand breaks, so the codeframe underlines the wholeenumsequence instead of the entry.Both come from the accumulator in the array branch being keyed by the enum value:
Object keys are strings, so
1and'1'land on"1". The second pass resets the entry, the string passes, and thedeletethen removes the report the number had earned. The location doesschema.enum.indexOf(mismatchedKey)with a key that came back out ofObject.keys, so it is always a string:[2].indexOf('2')is-1.Object.keysalso hoists integer-like keys, which is why the order flips.The scalar branch right above already does it the plain way, iterating the values and reporting each one. I made the array branch do the same and drop the accumulator: for each value in order, report it when it matches none of the listed types. The message and the reference are unchanged.
Two tests, one per symptom, both failing on
main: the first returns[]there, the second returns2atenum/-1beforezzatenum/0.vitest run packages/coregives the same 34 failures before and after (they are pre-existing here, mostly config and component-name suites), with 1037 passing before and 1039 after.oxlintreports nothing on the rule andoxfmt --checkis clean on all three files. Changeset added.Check yourself
The rule's documentation page already describes the behaviour this restores, so I did not change it. It says the rule requires every
enumvalue to conform to the schema'stype, and lists OAS 3.1 as supported.Security
Nothing here reads input in a new way. The rule looks at the same
schema.enumandschema.typeit always did; what changes is that a value is no longer able to hide behind another one that shares its string form. If anything the linter now reports strictly more, which is the safer direction for a validation rule.