CLI 目录 横向对比 FCRS-1.0 标准 /llms.txt
← 返回 CLI 目录

Spectral CLI

$ spectral 官方出品 低风险
Stoplight · 设计与文档排版 · Apache-2.0 · macos / linux / windows

Spectral CLI(@stoplight/spectral-cli)是一款基于规则集的灵活 JSON/YAML 检查器,广泛作为 OpenAPI、AsyncAPI、Arazzo 与自定义 JSON Schema 文档的 CI 治理门禁。内置 --format json / sarif、--ruleset 与 --fail-severity 参数,是自动化 API 设计校验的标杆。

FCRS-1.0 综合评分
92 /100
极佳 (Excellent)
中置信度 (源码/文档)
零摩擦安装命令
npm install -g @stoplight/spectral-cli
CLI vs MCP 选型建议 仅 CLI 可用

spectral lint --format json 可直接依据仓库内的 .spectral.yaml 规则集毫秒级评估 API 规范,无需 MCP。

Agent 友好度体检单

✓ 原生 JSON 输出
— 字段裁剪
✓ 无头非交互
— 支持 Dry-Run
— 只读模式开关
— 支持 NO_COLOR
✓ 自省/Schema 发现
— 官方 SKILL.md

当前为静态评估(本机尚未安装或尚未运行探针)。可在本地运行 npm run data:probe 并提交 data/probes/ 升级为实测证据。

FCRS-1.0 六维评分拆解

Agent 友好度 24 / 25

专为 CI 与 Agent 打造:支持 --format json、--format sarif、--quiet、--stdin-filepath、--display-only-failures 与 --fail-severity。

  • 11 built-in formatters including json, sarif, and github-actions
  • Zero interactive prompts and deterministic severity-based exit codes
安全与权限边界 24 / 25

严格只读检查工具,绝不修改任何 API 规范源文件。

  • Zero write/mutation side effects on source specs
  • No credentials required
安装与运行摩擦 14 / 15

提供 npm(@stoplight/spectral-cli)、npx、Docker 与 GitHub Releases 独立二进制分发。

  • npm install -g @stoplight/spectral-cli and standalone binaries
能力覆盖与边际价值 14 / 15

业界标准的 API 治理 Linter,广泛覆盖 OpenAPI、AsyncAPI、Arazzo 与任意 YAML/JSON。

  • Extensible JSONPath-based custom rulesets (.spectral.yaml)
维护与来源背书 8 / 10

Stoplight 官方仓库(stoplightio/spectral)以 Apache-2.0 协议维护。

  • Official Stoplight GitHub organization
文档与可验证性 8 / 10

docs.stoplight.io 官方文档对 CLI 参数、规则集与内置函数说明完备。

  • Official docs at docs.stoplight.io/docs/spectral/

安全边界、鉴权与放权指南

鉴权方式
无需鉴权
凭证存储位置

无需任何凭据;纯本地校验工具(仅当规范中显式引用远程 $ref 或规则 URL 时才会发起 HTTP 读取)。

潜在副作用
只读查询
费用与配额提示

免费开源软件(Apache-2.0)。

可复制到 CLAUDE.md / AGENTS.md 的安全护栏
# Spectral CLI Agent Rules
- Always run `spectral lint <spec> --format json --quiet` after editing OpenAPI or AsyncAPI files.
- Respect the repository's `.spectral.yaml` / `.spectral.yml` ruleset rather than bypassing rules.
- Use `--stdin-filepath` when linting unsaved spec snippets via stdin.

Agent 高频安全命令配方

只读查询(安全推荐)

以 JSON 输出校验 OpenAPI 规范
spectral lint openapi.yaml --format json --quiet

依据 .spectral.yaml(或默认 OAS 规则)校验 openapi.yaml 并输出安静的 JSON 诊断结果。

以 SARIF 格式批量检查规范目录
spectral lint "specs/*.yaml" --format sarif --fail-severity warn

针对 specs/*.yaml 输出 SARIF 报告,并在出现警告或错误时返回非零退出码。

核心优势

  • + 内置丰富的机器可读格式器(--format json、sarif、junit、github-actions)及 --quiet / --fail-severity 控制。
  • + 严格只读校验 OpenAPI、AsyncAPI、Arazzo 与自定义 JSON/YAML 文档。
  • + 支持 --stdin-filepath 从标准输入快速校验未落盘片段,并提供独立二进制包。

注意事项与踩坑点

  • ! 若 .spectral.yaml 规则集引用了自定义 JS 函数,运行时会执行本地 Node 代码。
  • ! 定位纯静态校验,打包合并与 SDK 生成需配合 redocly bundle 或 openapi-generator-cli。

已验证兼容的 Agent 宿主

Claude Code Codex CLI Gemini CLI