fix(no-required-schema-properties-undefined): resolve false positives for bare-required schemas in allOf-composed types#2836
Conversation
🦋 Changeset detectedLatest commit: 2cc6d1f The changes in this PR will be included in the next version bump. This PR includes changesets to release 3 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 |
8856aa1 to
929f198
Compare
929f198 to
2cc6d1f
Compare
tatomyr
left a comment
There was a problem hiding this comment.
Finally got my hands on this. It looks mostly okay for me. However, I'd like to ask you to explore the possibility to solve this without introducing a new concept leveraging the existing logic (if possible).
| - Name | ||
| ``` | ||
|
|
||
| The rule accepts bare `required` constraints on property sub-schemas when the property's type is defined in a parent `allOf` sibling. This is a valid JSON Schema pattern for adding presence constraints on top of a referenced base type: |
There was a problem hiding this comment.
Please do not modify V1 docs.
| !hasProperty(compositionRoot, requiredProperty, new Set()) && | ||
| !hasPropertyInParentContext(requiredProperty, compositionRoot ?? currentSchema) |
There was a problem hiding this comment.
This line actually seems like it should do the same as the previous one, but from a different prospective. Maybe it worth modifying findCompositionRoot / isCompositionChild instead?
Also, the existing code has protection against circular references. Does your code have it? (I haven't found that in your tests.)
Problem
The
no-required-schema-properties-undefinedrule incorrectly reported errors for two valid JSON Schema draft-04 patterns:Bare-required property sub-schema override — a property sub-schema that uses
requiredas a pure presence constraint with no localproperties, relying on the property's type definition resolved through a parentallOf.$ref:oneOf constraint fragments —
oneOfbranches that each contain only arequiredkeyword:Both are valid per the JSON Schema specification, where
requiredandpropertiesare fully independent keywords.Solution
Added a
hasPropertyInParentContexthelper to the rule'senterclosure. When the first two checks (localpropertiesandcompositionRoot) both fail to find a required property, the helper walks up theparentsstack to locate the ancestor schema that contains the current schema as a named property value. It then resolves that ancestor'sallOf/anyOf/oneOfsiblings and checks whether the property key is defined in any of them — the samehasPropertytraversal used elsewhere in the rule.This approach intentionally preserves misspelling detection: a misspelled key (e.g.,
giveNamevsgivenName) still fails the parent-context check and is reported as an error.Tests
Added 4 new test cases (23 total, all passing):
should not report bare required when property is defined in parent allOf siblingshould not report bare required in oneOf branches when property is defined in parent allOf siblingshould not report bare required in anyOf branches when property is defined in parent allOf siblingshould report misspelled required property even when parent allOf sibling defines the propertyrelates to #2320 #2060 #2061
Check yourself
Security
Note
Low Risk
Lint-rule behavior change with broad test coverage; reduces false positives without relaxing checks for genuinely undefined or misspelled required keys.
Overview
Fixes false positives in
no-required-schema-properties-undefinedfor valid JSON Schema patterns where a property sub-schema only addsrequired(oroneOf/anyOfbranches with barerequired) while the field’s shape comes from a siblingallOf$ref.The rule now uses
hasPropertyInParentContextto walk ancestors, match the property key, and resolveallOf/anyOf/oneOfsiblings before flagging a name as undefined; diagnostics use “is undefined” instead of “is not defined.” New tests cover barerequired,oneOf/anyOfconstraint fragments, and misspellings still reported; v1/v2 docs and a patch changeset document the behavior.Reviewed by Cursor Bugbot for commit 2cc6d1f. Bugbot is set up for automated code reviews on this repo. Configure here.