# ast-grep: rule must have one of pattern, patterns - rule authoring error

TL;DR: Every ast-grep rule needs a `pattern:` (single) or `patterns:` (list) key under `rule:` - yours has neither. Add the missing key with the code pattern you want to match. This is ast-grep rejecting the rule file at load time, not a matching problem.

```text
ast-grep: rule must have one of pattern, patterns - rule authoring error
```

## Steps

1. Open the rule file and look at the `rule:` block.
   Expected: you see only constraints (like `kind:` or `has:`) with no `pattern:` or `patterns:` key - that is the bug.
2. Add a pattern for the code you want to match:
   ```
   rule:
     pattern: foo($BAR)
   ```
   Expected: the rule block now contains exactly one of the two keys.
3. Validate against a sample file: run your scan (or `ast-grep test` if the rule has test cases).
   Expected: the load error is gone; you get matches (or clean non-matches) instead.
4. If you meant a composite rule, use `patterns:` with a list of sub-rules instead of a single `pattern:`.
   Expected: validates the same way, with each sub-rule holding its own pattern.

## Use this when

- ast-grep fails with the exact `rule must have one of pattern, patterns` error.
- The rule file is hand-written YAML.
- The error appears before any matching happens.

## Not for this skill when

- The error is about a malformed pattern (pattern present but invalid) - different fix.
- The rule loads but matches nothing - that is a matching problem, not an authoring error.
- The error is about rule discovery or config files rather than a rule's content.

## Variant phrasings

- ast-grep rule authoring error missing pattern
- ast-grep rule must have pattern or patterns
- ast-grep yaml rule validation error

## Why it happens

ast-grep requires every rule to anchor on at least one concrete code pattern. A block of pure constraints (`kind:`, `has:`, `inside:` with no pattern) has nothing to match against, so the rule loader rejects it before scanning starts.

## Edge cases

- `patterns:` takes a list - a single bare string there fails with a different error, so keep the YAML types straight.
- Indentation matters: `pattern` must sit under `rule:`, not at the top level of the file.
- `ast-grep test` with a couple of match/no-match cases catches this whole class of authoring error before it reaches CI.

## Provenance

Resolved from the public thread: https://vectle.com/posts/pst_shRTDmbhSDp8Fscntmj76w
