--- published: true layout: post title: 'Test Your Governance Rules Before They Test Your APIs' image: https://kinlane-images.s3.amazonaws.com/apievangelist/api-evangelist-images/test-your-governance-rules-before-they-test-your-apis.png date: 2026-12-03 author: Kin Lane tags: - API Evangelist Tools - MCP - API Governance - Spectral - Testing --- Every team that writes Spectral rules ends up trusting them on faith. You write a rule, lint one spec, see a warning show up, and ship it into CI. Nobody goes back to ask whether the rule fires on everything it should, stays quiet on everything it shouldn't, or fires at all. Governance rules are code, and we rarely give them the tests we'd insist on for any other code. The API Evangelist `test_ruleset` tool, which is the `POST /governance/test-ruleset` operation on the API Evangelist API, exists to close that gap. You hand it a ruleset and a set of fixture documents with expectations. It runs them, tells you pass or fail for each case, and reports which rules none of your cases actually exercise. ## What I gave it I wrote a small four-rule ruleset using only Spectral's built-in functions, the kind of starter set many teams begin with: - `info-contact`: `$.info` must have a `contact` (`truthy`), warn. - `operation-operationId`: every get/post/put/patch/delete must have an `operationId`, error. - `operation-summary`: every operation must have a `summary`, warn. - `servers-https`: every server URL must match `^https://`, error. Then I wrote three tiny OpenAPI 3.1 fixtures, each with an expectation in the shape the [operation reference](https://developer.apievangelist.com/governance-api) describes: - **clean spec**: everything present, `expect: []`, meaning it must lint clean. - **missing operationId twice**: a `GET` and a `POST` with no `operationId`, `expect: {"operation-operationId": 2}`, an exact count. - **plain http server**: the server URL is `http://`, `expect: ["servers-https"]`, plus `forbid: ["info-contact"]` so the contact rule must stay quiet. On a second run I added a fourth case: **empty contact object**, where `info.contact` is `{}`, with `expect: ["info-contact"]`. An empty contact tells a consumer nothing, so I wanted the rule to catch it. ## What came back The first run passed, three for three. Then I added the fourth case, and this came back: ```json { "pass": false, "summary": { "cases": 4, "passed": 3, "failed": 1 }, "cases": [ { "name": "clean spec", "pass": true, "fired": {} }, { "name": "missing operationId twice", "pass": true, "fired": { "operation-operationId": 2 } }, { "name": "plain http server", "pass": true, "fired": { "servers-https": 1 } }, { "name": "empty contact object", "pass": false, "failures": ["expected info-contact to fire; it did not"], "fired": {} } ], "coverage": { "rules": 4, "asserted": 3, "untested": ["operation-summary"], "neverFired": ["info-contact", "operation-summary"] } } ``` The failing case is the story. `truthy` passes an empty object, so my `info-contact` rule lets `contact: {}` through. That's exactly the kind of hole a team would never notice by linting real specs, because the spec looks compliant and the dashboard stays green. The fix is to point the rule at `$.info.contact` and require something inside it, a `name`, `email` or `url`, and then rerun the same fixture to prove it fires. The coverage block is the part I didn't expect to like this much. It splits out two different gaps. `untested` lists rules no case asserts on: on the first run that was `info-contact` and `operation-summary`, and once I added the fourth case only `operation-summary` was left. `neverFired` lists rules no fixture ever triggered. `info-contact` is still on that list even after I wrote a case for it, because the case I wrote is the one the rule fails. So a rule can be "tested" and still never fire once. That's a useful way to find a rule that's quietly dead. Something honest about my own setup: the first run's `"pass": true` would have fooled me if I'd only looked at the top line. Three cases passed, but half the ruleset was never exercised. Read the coverage block, not just the pass flag. ## What that makes possible - **Unit tests for a style guide.** Keep a fixture file next to your ruleset and run `test_ruleset` in CI whenever a rule changes, so an edited `given` path or a swapped function can't silently turn a rule off. - **Proving a fix.** When someone reports a false negative like my empty contact, turn the report into a failing case first, fix the rule, and keep the case so the bug stays fixed. - **Retiring dead rules.** Anything that stays in `neverFired` after a decent set of fixtures either needs a fixture or doesn't belong in the ruleset. - **Agents that write rules safely.** An agent drafting governance rules can test its own output against expectations before proposing it, rather than asking a human to take a new rule on faith. The natural next step after a failing case is `explain_rule_result`, which tells you why a specific rule did or didn't fire on a document. ## Try it `test_ruleset` is **free**. It takes up to 50 cases per call. - MCP: add `https://mcp.apievangelist.com/mcp` (or the lighter `https://mcp.apievangelist.com/mcp/governance`) and call `test_ruleset` with `ruleset` and `cases`. - REST: ```sh curl -X POST https://api.apievangelist.com/v1/governance/test-ruleset \ -H 'Content-Type: application/json' \ -d '{"ruleset":{"rules":{...}},"cases":[{"name":"clean spec","document":{...},"expect":[]}]}' ``` - Reference: [Governance API](https://developer.apievangelist.com/governance-api) - MCP docs: [developer.apievangelist.com/mcp-server](https://developer.apievangelist.com/mcp-server) - Getting started: [developer.apievangelist.com/getting-started](https://developer.apievangelist.com/getting-started)