# Look up a rule from a finding

Source: https://watership.ca/api-docs/docs/tasks/look-up-a-rule

Turn a finding’s rule id into the rule’s guidance and examples, and cache rules by release.

A finding tells you which rule the text breaks. To show the writer why, fetch the rule. This guide shows how to get a rule from a finding, and how to cache rules so that a check with 40 findings doesn't make 40 extra requests.

## Get the rule [#get-the-rule]

Take an id from the finding's `rule_ids` and pass it to `GET /v1/rules/{rule_id}`:

```sh
curl http://127.0.0.1:4010/v1/rules/term-please \
  -H "Authorization: Bearer wsk_test_example"
```

The rule's guidance is in `body`, as Markdown, and its examples are in `examples`, each a pair of text to avoid and text to write instead.

If you only need to send a person to the rule, you don't need a request at all. Every finding has a `link` to the rule's page in its guide.

## Handle a finding with more than one rule [#handle-a-finding-with-more-than-one-rule]

Most findings name one rule. A few checks enforce rules in two guides at once. The `Email` check, for example, reports `term-email` from the terminology section and `spell-closed-compounds` from the spelling section. Show the first id as the main rule and link the others:

```js
const [main, ...related] = finding.rule_ids;
```

A company claims finding has an empty `rule_ids` list. Use its `link`, which points to the organization's claims policy.

## Cache rules by release [#cache-rules-by-release]

Rules and checks change only in a new release, which is a new version of the Watership checks, such as `0.2.0`. Every response carries the release in the `Watership-Release` header, and a check result also has it in `release`. Use the release and the rule id together as the cache key:

```js
const cache = new Map();

async function getRule(id, release) {
  const key = `${release}:${id}`;
  if (!cache.has(key)) {
    const response = await fetch(`${API_URL}/v1/rules/${encodeURIComponent(id)}`, {
      headers: { Authorization: `Bearer ${API_KEY}` },
    });
    if (response.status === 404) return null;
    if (!response.ok) throw new Error(`Rule lookup failed: ${response.status}`);
    cache.set(key, await response.json());
  }
  return cache.get(key);
}

// Fetch each rule once, however many findings name it.
const ids = [...new Set(result.findings.flatMap((f) => f.rule_ids))];
const rules = await Promise.all(ids.map((id) => getRule(id, result.release)));
```

When the release changes, the old keys stop matching and the cache fills again with the new rules.

## Handle an unknown id [#handle-an-unknown-id]

A rule id you saved from an old release may have been retired or renamed. The API responds with `404` and a problem object, and the `detail` suggests the closest id when there is one. Treat it as "no rule to show" and fall back to the finding's `message`.

## Related [#related]

- [`GET /v1/rules/{rule_id}`](https://watership.ca/api-docs/docs/reference/api/rules/get-a-rule) in the API reference
- [How findings map to the guides](https://watership.ca/api-docs/docs/concepts/findings-and-guides)
