Skip to content

feat(rule): add items_must_match to list rules - #89

Closed
kinchoKayaba wants to merge 1 commit into
jackchuka:mainfrom
kinchoKayaba:feature/list-items-must-match
Closed

kinchoKayaba wants to merge 1 commit into
jackchuka:mainfrom
kinchoKayaba:feature/list-items-must-match

Conversation

@kinchoKayaba

@kinchoKayaba kinchoKayaba commented Sep 30, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Adds items_must_match to list rules, so a schema can require every item of a list to follow a format, not just require the list to exist:

- heading: "## Components"
  lists:
    - min: 1
      type: unordered
      items_must_match:
        - pattern: '^[A-Za-z][A-Za-z0-9]*(\s.*)?$'
## Components
- Button                 ✓
- **Card**               ✗ List item '**Card**' in section 'Components' does not match '^[A-Za-z][A-Za-z0-9]*(\s.*)?$'
- `X`                    ✗ List item '`X`' in section 'Components' does not match '^[A-Za-z][A-Za-z0-9]*(\s.*)?$'
  - nested field         (not checked)

Why

required_text passes as soon as one match exists anywhere in the section, and forbidden_text can only list the spellings to reject one by one. Neither can say "every item must look like this". Approximating it with a complement regex in forbidden_text works, but the violation shows the regex rather than the offending item.

Behavior

  • Entries use the same forms as required_text (reusing RequiredTextPattern): a plain string is a substring match, {pattern: ...} is a regex.
  • Multiple entries combine as AND, like required_text: each item must satisfy every entry. For alternatives, use | in one regex.
  • Matching uses the item's raw Markdown source, so **Name** keeps its asterisks. The list marker and sub-lists are excluded; wrapped lines are joined with a space.
  • Only top-level items of lists matching type are checked. Items of nested sub-lists are skipped.
  • One violation per failing (item, entry), reported at the item's own line and column.
  • An invalid regex falls back to a substring match, same as required_text / forbidden_text.

Changes

  • internal/schema/schema.go: ItemsMustMatch []RequiredTextPattern on ListRule
  • internal/parser/: List gains Nested and Items; new ListItem{Text, Line, Column} built from the item's first text block
  • internal/rules/list.go: item validation; the type filter is extracted into listMatchesType; generated templates get a must match: ... comment
  • schema.json: regenerated via mdschema schema -o schema.json
  • README: lists bullet and rules table
  • Tests: parser (raw text, nested flag, positions), rules (basic, AND, literal form, nested skipped, type respected, multi-line items, generator), loader (both entry forms load without unknown-key warnings)

Open questions

  • Naming: items_must_match was chosen to read as exclusive ("every item must match"). Happy to rename.
  • Type: it reuses RequiredTextPattern, so its JSON Schema description reads "Required text pattern". A dedicated or shared TextPattern type may be cleaner.

Test plan

  • go test ./...: all green
  • go vet ./...: clean
  • golangci-lint run (v2, repo .golangci.yml): 0 issues
  • schema.json matches generator output
  • E2E with the built binary on the example above: only **Card** and `X` are reported; adding a second entry '^\S+$' also reports an item containing spaces (AND)

🤖 Generated with Claude Code

Section rules could require a list but not constrain what its items
say, so a convention such as "each item is a single identifier" had
no enforceable form.

- parse each list's direct items, keeping the item's raw Markdown
  source (inline markup such as **bold** is preserved) and marking
  sub-lists as nested
- add `items_must_match` to list rules, reusing required_text's
  "text" (substring) / {pattern: ...} (regex) entry forms; every
  top-level item of a list matching `type` must satisfy every entry,
  with one violation per failing (item, entry) at the item's position
- nested sub-list items are not checked
- document the option and describe it in generated templates

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@jackchuka

Copy link
Copy Markdown
Owner

Thanks for this, and for the clear write-up. Per-item constraints are a good fit for mdschema.

I've reworked it in #102 on top of #100 and #101, with a few design changes:

You're credited as co-author on the commit. Closing this in favor of #102. Feedback there is very welcome.

@jackchuka jackchuka closed this Nov 11, 2026
jackchuka added a commit that referenced this pull request Nov 11, 2026
A list rule could require a list but not constrain what its items say.
Add `items` to list rules, applying `required_text` and `forbidden_text`
to every top-level item of a matching list. Items are matched against
their raw Markdown, and one violation is reported per failing (item,
pattern) pair at the item's position.

The parser now records each list's items, replacing ItemCount with
Items. The literal/regex matching shared by required_text and
forbidden_text moves into a single helper.

Based on the design in #89.


Claude-Session: https://claude.ai/code/session_014K1p9VkeotiX1SR8pLWJSK

Co-authored-by: sakanaka <[email protected]>
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