liveBook · Manning

Linter rule

As explained in The Design of Web APIs, Second Edition

A linting rule is a statically evaluated constraint that detects or enforces a specified pattern in an API description or other structured artifact.

A linter rule checks a specified pattern in a structured artifact. For an API, it can inspect an OpenAPI document and flag design or authoring problems before they become harder to change. Rules turn recurring guidelines—such as property naming or search pagination—into checks, leaving people to judge whether the design meets consumers’ needs. section 18.0 section 18.2.1

Catch a query-parameter typo

Suppose GET /products has a query parameter named buyPRice. A Spectral rule can select query parameter names with `given: $..parameters[?(@.in === "query")].name`, then apply the `casing` function with the `camel` option. It flags buyPRice and points toward buyPrice. The rule needs both a precise target and a check; otherwise it may report the wrong elements or miss the typo. section 18.3.2 section 18.5

Make each rule useful rather than merely strict:

  • Give it a reason grounded in an API design guideline and link that guidance with `documentationUrl`. Do not enforce a preference whose value has not been established. section 18.4.1
  • Check one right-sized concern at a time. Search pagination, for example, may need separate rules for the presence and schemas of its parameters; mandatory pagination and optional filters need not share a severity. section 18.4.2 section 18.4.4
  • Prefer a shared OpenAPI component when it can replace detailed structural checks. A rule can require a 500 response to reference a reusable response instead of rechecking its media type, description, and schema. section 18.4.3 section 18.6.4
  • Return feedback that identifies the problem, its location, importance, and a way to fix it. Set severity and message accordingly. section 18.7

In Spectral, a rule uses `given` JSONPaths to find elements and one or more `then` functions to check them. Start by testing the JSONPath against a valid, realistic document; a temporary `undefined` check reveals what it selected. Functions can check values or, with `field: "@key"`, object keys. Reference checks can verify use of shared components. section 18.5.1 section 18.6.1 section 18.6.4

Keep shared rules in versioned rulesets. An API-local `.spectral.yaml` can import them, change severities, disable unsuitable checks, and add API-specific rules. A linter cannot settle every design question: deciding whether an operation should run for a long time, for example, requires context beyond its API description. section 18.8 section 18.9.1 section 18.2.3

Try a rule

Write a rule that checks semantic versioning at `info.version` in an OpenAPI document. Test it against `version: athena-1.0.0.bugfix`, and make the resulting message useful to someone fixing the value. section 18.12 section 18.5.1

How The Design of Web APIs, Second Edition covers it

How deep it goes
Develops rules through purpose, targeting, checks, feedback, organization, and local customization, while acknowledging design judgments beyond linting.
Examples
Uses camel-cased query parameters, search pagination, shared error responses, and a faulty semantic version as concrete checks.
What it assumes
Assumes API design guidelines and OpenAPI documents, while introducing Spectral’s targeting and checking procedure.
What you can do after
Build it from scratch in Spectral, test its target, provide actionable feedback, and adapt shared rules to an API.
How it connects
Connects API design guidelines, OpenAPI, JSONPath, JSON Schema, shared components, and rulesets.

Also explained in

Compare how 5 books explain linter rule →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Design of Web APIs, Second Edition. The section links point to the parts of the book it draws on.