feat(linter): add format_type_mismatch rule to detect invalid format usage#2338
Open
Vaibhav701161 wants to merge 2 commits intosourcemeta:mainfrom
Open
feat(linter): add format_type_mismatch rule to detect invalid format usage#2338Vaibhav701161 wants to merge 2 commits intosourcemeta:mainfrom
Vaibhav701161 wants to merge 2 commits intosourcemeta:mainfrom
Conversation
Signed-off-by: Vaibhav mittal <vaibhavmittal929@gmail.com>
🤖 Augment PR SummarySummary: Adds a new AlterSchema linter rule, Changes:
Technical Notes: The rule is non-mutating (no auto-fix) and reports the issue at the keyword locations returned via 🤖 Was this summary useful? React with 👍 or 👎 |
- apply clang-format to all modified files - fix test expectations to align with existing rule interactions - ensure full CI pipeline passes locally Signed-off-by: Vaibhav mittal <vaibhavmittal929@gmail.com>
Contributor
Author
|
@jviotti , kindly review |
Member
|
Nice one! Though note that we recently move the |
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.
Summary
This PR introduces a new lint rule:
format_type_mismatch.The rule detects schemas where the
formatkeyword is used alongside a non-stringtype.{ "type": "integer", "format": "email" }In JSON Schema,
formatvalidation is defined for string instances. Using it with other types does not have any effect and is typically an authoring mistake.This rule highlights such cases to improve schema correctness and clarity.
The rule emits a diagnostic when:
formatexiststypeexiststypeis a single string valuetypeis not"string"The diagnostic points to the
formatkeyword location.This rule is non auto-fixable because modifying either
typeorformatwould require assumptions about the author’s intent.Implementation
The rule follows the existing alterschema linter architecture:
src/extension/alterschema/linter/format_type_mismatch.halterschema.ccSOURCESlist in the alterschema CMake configurationThe rule checks:
formatis defined and is a stringtypeis defined and is a stringtype != "string"If these conditions are met the rule returns:
Design Notes
This rule complements the existing
non_applicable_type_specific_keywordsrule.While the existing rule detects keywords that are not applicable to a given type, this rule provides a more explicit and focused diagnostic specifically for misuse of the
formatkeyword.This improves clarity for users by directly pointing out incorrect
formatusage rather than reporting it as a generic type incompatibility.The rule intentionally skips cases where
typeis an array (e.g.,["string", "integer"]), sinceformatmay still apply when the instance is a string.The rule applies across all supported drafts where both
typeandformatare valid keywords.Tests
Tests were added across all supported dialects using the existing lint testing utilities:
The following scenarios are covered:
Rule fires when
formatis used with non-string typesRule does not fire when
typeis"string"Rule does not fire when
formatis missingRule does not fire when
typeis missingRule does not fire when
typeis an array including"string"Rule fires inside nested subschemas (
properties,items, etc.)Rule correctly handles
$ref:Tests are structured consistently with existing linter rule tests and account for interactions with other rules where applicable.
Related Work
Refs: #1975