lint: add require_schema_declaration rule#885
Conversation
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>
🤖 Augment PR SummarySummary: Adds a new diagnostic-only linter rule that warns when the root JSON Schema does not declare its dialect via Changes:
Technical Notes: The rule is diagnostic-only ( 🤖 Was this summary useful? React with 👍 or 👎 |
| const auto &outcome, const auto &fixable) { | ||
| traces.emplace_back(pointer, name, message, outcome, fixable); | ||
| }, | ||
| "https://json-schema.org/draft/2019-09/schema"); |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
Summary
Adds a new lint rule
require_schema_declarationthat fires when the root schema does not declare a$schemakeyword. 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 rulesrc/alterschema/alterschema.cc- rule registered for all dialectssrc/alterschema/CMakeLists.txt- header addedtest/alterschema/alterschema_lint_draft3_test.cc- tests addedtest/alterschema/alterschema_lint_draft4_test.cc- tests addedtest/alterschema/alterschema_lint_draft6_test.cc- tests addedtest/alterschema/alterschema_lint_draft7_test.cc- tests addedtest/alterschema/alterschema_lint_2019_09_test.cc- tests addedtest/alterschema/alterschema_lint_2020_12_test.cc- tests addedPart of sourcemeta/core#1975.