Skip to content

feat(schema): describe rule options from the config structs - #1000

Merged
rvben merged 6 commits into
rvben:mainfrom
anci3ntr0ck:feat/schema-stage2
Nov 13, 2026
Merged

rvben merged 6 commits into
rvben:mainfrom
anci3ntr0ck:feat/schema-stage2

Conversation

@anci3ntr0ck

@anci3ntr0ck anci3ntr0ck commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

What this is

Stage 2 of #988. Stage 1 (#997) taught rumdl.schema.json which sections exist and which keys each one takes; the type of an option came from the default value its rule serialised, so an editor could flag line-length = "eighty" but had nothing to say about what line-length means.

This PR derives schemars::JsonSchema on the rule config structs and publishes what they already document: a description per option, the real default, and the accepted spellings.

What the schema now says

Stage 1 here
artifact 100,941 B 191,691 B
rule options described 0 311 of 318
defaults published 41 311
spellings listed for enumerated options 0 46 options
rule $defs / root properties 90 / 213 90 / 213 (unchanged)

The seven options that remain undescribed are MD073's: it is the only rule with no config struct (it writes its section by hand), so its options keep the types inferred from the default. A test names MD073 as the only allowed gap, so a new rule cannot join that list quietly.

$defs/MD013.properties.line-length now reads, next to the key an editor completes:

Maximum line length. Lines longer than this are reported, and reflowed when the fix is enabled. Set to 0 to disable the limit. (default: 80)

Two options stay looser than the struct, on purpose

Your first constraint was that the schema must never be stricter than rumdl. Two places needed that to win over detail:

  1. Sentinel options. Where a rule marks a key nullable or polymorphic, the loader skips its own type check because no single TOML type describes the key (preferred-style takes a string or a list). The derived schema would describe whichever variant the field's Deserialize names first and reject the other, so those 38 options publish a description and no type.
  2. Enumerated spellings are described, not enumerated. reflow-mode = "sentence_per_line" is accepted because the field carries a #[serde(alias)], and schemars does not report aliases; several style enums are parsed by hand and are case-insensitive. Publishing an enum would reject configs rumdl accepts, so the spellings go into the description instead ("One of default, normalize, …"). The cost is real — no value completion for those keys — and it is the same trade $defs/Severity already makes.

The spelling of an enum is now the spelling users type

Deriving the schema surfaced a bug Stage 1 could not have: enums with a hand-written Deserialize have no serde(rename_all), so schemars published the Rust variant name and an editor suggested MD003 style = "AtxClosed", which the loader rejects. Each variant now carries #[schemars(rename = …)] with the spelling from its own Display/FromStr. A guard test reads the spellings back out of the generated descriptions and parses each one, in the module that owns its parser.

Tests

  • test_schema_accepts_every_rule_default — every default a rule publishes validates.
  • test_schema_accepts_values_the_loader_parses — MD084/MD088 allow = ["U+200B"], duplicates and U+1F3FB load, then validate.
  • test_schema_accepts_enum_aliases_rumdl_accepts — the reflow-mode / length-mode aliases.
  • test_schema_describes_the_options_it_derives — descriptions, defaults, and the single rule allowed to have none.
  • test_schema_never_enumerates_option_values — no rule option publishes enum/const.
  • test_schema_accepts_the_spellings_it_publishes — every listed spelling validates.
  • schema_publishes_spellings_that_parse (×5, one per parsing module) — every listed spelling parses, with negative controls that must fail.

Verification

cargo fmt --check, cargo clippy --lib --bins --tests -- -D warnings -D clippy::uninlined_format_args, cargo test --lib (9,331 passed), cargo test --tests (14 binaries), cargo test --doc, RUSTDOCFLAGS="-D warnings" cargo doc --no-deps, rumdl schema check, and rumdl fmt --check docs/ all pass. RUMDL_UPDATE_SCHEMA=1 reproduces the committed file byte for byte.

What it looks like in an editor

Hovering reflow-mode in [MD013] now shows the description, the default and the accepted spellings — all three read from the struct field, so they cannot drift from what the loader parses:

VS Code hover showing the description, default and accepted spellings of MD013.reflow-mode ## Notes
  • No CHANGELOG.md entry: CONTRIBUTING.md calls this routine, and Stage 1 took the same route.
  • The single documented strictness from Stage 1 (MD013 = false is rejected while the parser warns and drops it) is unchanged, as is the [rules] wrapper.
  • The schema is Stable — additive changes only per docs/stability.md; this is additive.
  • Written largely by an AI coding agent under review; commits are agent-authored in style. Happy to split this differently, or to move the enum-spelling fix into its own PR if you would rather review that separately.

Refs #988

Every rule config struct now derives `schemars::JsonSchema`, so
`rumdl.schema.json` publishes the doc comment and the default of each option
instead of only the type inferred from a serialized default. The struct that
deserializes a section is the struct that describes it, so the keys an editor
completes and the keys the loader accepts cannot drift apart.

`Rule::config_json_schema` is the new trait method; `impl_rule_config_schema!`
implements it for the 55 rules that go through it, and `RuleRegistry` collects
the result next to the rest of the per-rule metadata, so generating the schema
still constructs no rule of its own. Rules that do not derive the trait keep the
types inferred from their registry entry.

Two things stay open on purpose, for the same reason as before: a key the
registry marks as untyped (sentinel) keeps an empty schema, and an option whose
value is an enum is described rather than enumerated, because `#[serde(alias)]`
on a variant widens the spellings the loader takes and `schemars` does not
report aliases. Both decisions are documented in the module and pinned by tests:

- `test_schema_accepts_every_rule_default` validates all 170 non-sentinel rule
  defaults against the schema;
- `test_schema_accepts_enum_aliases_rumdl_accepts` loads `reflow-mode =
  "sentence_per_line"` and friends through the real loader and asserts the
  schema takes them too;
- `test_schema_describes_the_options_it_derives` pins the description, the
  default and the sentinel case, and fails if the derives are dropped.

The schema grows from 100,941 to 176,660 bytes: 331 of 498 rule option
properties now carry a description.

Refs rvben#988
Review of a83ef10 turned up two places where the generated schema was
stricter than the loader and one where it was needlessly silent.

MD084/MD088 `allow` took the derived HashSet<char> schema, which caps each item
at one character and forbids repeats; the loader reads the same values from
strings like "U+200B", so both written forms and duplicates are accepted there
and now validate here. schemars also writes format "uint" and "uint8" for
usize/u8, neither of which JSON Schema defines and both of which editors have
reported as errors before (rvben#368); the non-standard formats are stripped,
leaving the minimum/maximum the derive already provides.

Options the registry deliberately leaves untyped kept an empty schema. They now
keep their description, which constrains nothing: an editor shows what
`preferred-style` does without being told which of its two shapes is legal.

The rest is coverage: field doc comments for the options that had none
(MD033/MD044/MD060/MD054/MD026/MD055/MD061), MD076 hooked up through
config_json_schema_named instead of being skipped, relax_value_enums recursing
into the array and map keywords it used to miss, and a single is_sentinel
predicate instead of two copies. Described rule options go from 241/318 to
311/318, the remainder being MD073, the one rule without a config struct.

Refs rvben#988
schemars names enum variants after the Rust identifiers, but the enums that
deserialize by hand take kebab-case: the schema offered `MD003 style =
"AtxClosed"` and `MD063 style = "TitleCase"` in a hover and then rejected them,
and the same leak showed up for MD046, MD048, MD049, MD050, MD060 and MD076.
Each variant now carries the spelling its `Display` and `Deserialize` agree on.

test_schema_publishes_spellings_the_loader_accepts walks every "One of" list in
the generated descriptions, feeds each spelling to the real loader and asserts
the schema takes it too, so a hand-written enum that forgets this fails the
build instead of the editor.

Refs rvben#988
The guard added with the enum renames loaded a `rumdl.toml` and expected the
loader to reject a spelling it could not parse. It never does: `rule_config_serde`
warns about a value it cannot parse and keeps the rule's default, so the load
succeeded and the assertion could not fail.

Parse instead. `HeadingStyle`, `ColumnAlign` and `HeadingCapStyle` carry their own
`FromStr`/`Deserialize`, so the check runs from `src/rules/mod.rs` against the
spellings in the schema's own "One of" text. `CodeBlockStyle`, `CodeFenceStyle`,
`EmphasisStyle` and `StrongStyle` are parsed by their config modules, so each of
those carries the same check beside the parser it is checking. MD076's style is
matched inside `from_config` and has no deserializable config struct to route the
check through, so it stays with the description as its only contract.

Mutation-checked: renaming MD003's `atx-closed` back to `AtxClosed`, or MD046's
`fenced` to `atx`, fails the test with the offending spelling in the message.

Refs rvben#988
The spelling guard had six copies of "find the words after One of", and the one in
`config/tests.rs` skipped a description it could not parse. Read the list through a
single helper, `schema_gen::published_spellings`, which panics when a description
stops enumerating its spellings, so a wording drift fails the test instead of
quietly reducing what is covered.

MD076's style was the last uncovered spelling set: it is matched inside `from_config`
rather than by a derive. Move that match into `ListItemSpacingStyle::from_spelling`
(behaviour unchanged — an unknown value still keeps the default) and check the
schema's spellings against it, like the other four config modules.

Refs rvben#988

@rvben rvben left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Thanks for adding the schema descriptions. The schema tests pass locally, and I’ll merge once the required checks pass.

@rvben
rvben merged commit 2237c7a into rvben:main Nov 13, 2026
15 checks passed
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