Skip to content

lint: add require_schema_declaration rule#885

Open
AcEKaycgR wants to merge 1 commit into
sourcemeta:mainfrom
AcEKaycgR:feat/linter-require_schema_declaration
Open

lint: add require_schema_declaration rule#885
AcEKaycgR wants to merge 1 commit into
sourcemeta:mainfrom
AcEKaycgR:feat/linter-require_schema_declaration

Conversation

@AcEKaycgR

@AcEKaycgR AcEKaycgR commented Jun 27, 2026

Copy link
Copy Markdown
Contributor

Summary

Adds a new lint rule require_schema_declaration that fires when the root schema does not declare a $schema keyword. Without $schema, the dialect is ambiguous and validators may apply the wrong rules.

Applies to all dialects (Draft 3, 4, 6, 7, 2019-09, 2020-12).

Changes

  • src/alterschema/linter/require_schema_declaration.h - new rule
  • src/alterschema/alterschema.cc - rule registered for all dialects
  • src/alterschema/CMakeLists.txt - header added
  • test/alterschema/alterschema_lint_draft3_test.cc - tests added
  • test/alterschema/alterschema_lint_draft4_test.cc - tests added
  • test/alterschema/alterschema_lint_draft6_test.cc - tests added
  • test/alterschema/alterschema_lint_draft7_test.cc - tests added
  • test/alterschema/alterschema_lint_2019_09_test.cc - tests added
  • test/alterschema/alterschema_lint_2020_12_test.cc - tests added

Part of sourcemeta/core#1975.

Adds a new diagnostic-only lint rule that fires when the root schema
does not declare its dialect using the `$schema` keyword. Applies to
all dialects (Draft 3, 4, 6, 7, 2019-09, 2020-12).

Part of sourcemeta/core#1975.

Signed-off-by: AcE <kintan0108@gmail.com>

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 9 files

Re-trigger cubic

@augmentcode

augmentcode Bot commented Jun 27, 2026

Copy link
Copy Markdown
🤖 Augment PR Summary

Summary: Adds a new diagnostic-only linter rule that warns when the root JSON Schema does not declare its dialect via $schema.

Changes:

  • Introduced RequireSchemaDeclaration (require_schema_declaration) as a non-mutating SchemaTransformRule that only runs at the root schema location.
  • Rule triggers when the schema is an object, the active vocabularies correspond to supported JSON Schema dialects (Draft 3/4/6/7, 2019-09, 2020-12), and $schema is not declared.
  • Registered the new rule in the Alterschema linter bundle so it runs during lint checks.
  • Updated the Alterschema CMake target to include the new linter header.
  • Added unit tests for Draft 3/4/6/7, 2019-09, and 2020-12 covering both: (a) schemas that already declare $schema (no findings) and (b) schemas missing $schema (one lint trace, non-fixable).
  • Tests for missing $schema explicitly pass a default dialect to SchemaTransformer::check to ensure dialect inference still works.

Technical Notes: The rule is diagnostic-only (mutates = false) and reports a single finding anchored at the top-level schema.

🤖 Was this summary useful? React with 👍 or 👎

@augmentcode augmentcode Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review completed. 1 suggestion posted.

Fix All in Augment

Comment augment review to trigger a new review at any time.

Comment thread src/alterschema/linter/require_schema_declaration.h
const auto &outcome, const auto &fixable) {
traces.emplace_back(pointer, name, message, outcome, fixable);
},
"https://json-schema.org/draft/2019-09/schema");

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, but because the linter will analyse the schema, this ONLY ever works if you pass a default dialect, right? If so, we should be able to transform to fix?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Though on the other side, I know people use defaultDialect in jsonschema.json for big collection of schemas, and that setup will start failing given this, so I'm not sure. I think the case we want to protect is where we don't even know the dialect of a schema, but sounds like that's not possible at all?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right that the linter can only fire this rule when it already knows the dialect. Making it fixable makes sense then ,the fix would insert $schema with the resolved dialect URI. On the defaultDialect concern, I agree it could be noisy for users who manage a collection of schemas that way, but with auto-fix it becomes a one-shot cleanup. I can update the rule to be fixable if that is the direction you want.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants