Skip to content

Spectral: warn on rule names written as error.code when Error.code is the HTTP status #163

Closed

Description

@clabonte

Summary

Add a Spectral rule that warns when prose names a rule in code, e.g. error.code = template_in_use. Since #160 (compatibility.md §54), Error.code is the HTTP status and the rule name goes in reason.

Follow-up from #159 ("Optional"). The rule was first proposed in compatibility.md §51 item 2 as a generator rule and never forwarded. It fits better in the kit's ruleset, because it checks prose, not generated code.

Why

  • The failure is silent. A description saying error.code = slot_already_booked passes validate and generate, but the wire can't carry it: code is an integer. Clients written from the docs branch on a field that never holds the value. One consumer had 17 operation descriptions like this (§51 item 2).
  • Projects migrating off the old shape (§54 migration note) will leave such prose behind. A schema change doesn't touch description text.
  • Examples are already covered. spectral:oas example validation rejects a string code in a response example. Prose is the gap.

Proposed rule

specfuse-error-rule-name-in-code, severity warn, in schemas/spectral/specfuse-openapi.yaml.

  • Gate: fire only when the resolved components.schemas.Error.properties.code has type: integer, so a project still on string code isn't flagged.
  • Targets: description and summary on operations, responses, and x-* blocks that carry prose (info.description included).
  • Match: a snake_case identifier assigned to or compared with code. For example:
    • code = foo_bar, code == 'foo_bar', code: foo_bar, "code": "foo_bar";
    • with or without an error. / errors[0]. prefix.
    • A numeric value (code = 409) does not match.
  • Message: "Error.code is the HTTP status; name the rule in reason (error.reason = foo_bar). API Handbook §11."

It likely needs a small custom function (functions/openapiErrorReasonProse.js): the gate reads a different path from the targets, which a plain pattern rule can't express.

Done when

  • Rule plus function, with fixtures in both directions:
    • fires on error.code = template_in_use when code is an integer;
    • silent for error.reason = template_in_use, for code = 409, and when code is a string.
  • hello-orders and the init scaffold lint clean (no new findings).
  • API Handbook §11 names the rule.
  • compatibility.md §51 item 2 / §54: the lint is no longer "not forwarded".
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions