Spectral CLI
$ spectral 官方出品 低风险Spectral CLI(@stoplight/spectral-cli)是一款基于规则集的灵活 JSON/YAML 检查器,广泛作为 OpenAPI、AsyncAPI、Arazzo 与自定义 JSON Schema 文档的 CI 治理门禁。内置 --format json / sarif、--ruleset 与 --fail-severity 参数,是自动化 API 设计校验的标杆。
npm install -g @stoplight/spectral-cli spectral lint --format json 可直接依据仓库内的 .spectral.yaml 规则集毫秒级评估 API 规范,无需 MCP。
Agent 友好度体检单
当前为静态评估(本机尚未安装或尚未运行探针)。可在本地运行 npm run data:probe 并提交 data/probes/ 升级为实测证据。
FCRS-1.0 六维评分拆解
专为 CI 与 Agent 打造:支持 --format json、--format sarif、--quiet、--stdin-filepath、--display-only-failures 与 --fail-severity。
- 11 built-in formatters including
json,sarif, andgithub-actions - Zero interactive prompts and deterministic severity-based exit codes
严格只读检查工具,绝不修改任何 API 规范源文件。
- Zero write/mutation side effects on source specs
- No credentials required
提供 npm(@stoplight/spectral-cli)、npx、Docker 与 GitHub Releases 独立二进制分发。
npm install -g @stoplight/spectral-cliand standalone binaries
业界标准的 API 治理 Linter,广泛覆盖 OpenAPI、AsyncAPI、Arazzo 与任意 YAML/JSON。
- Extensible JSONPath-based custom rulesets (
.spectral.yaml)
Stoplight 官方仓库(stoplightio/spectral)以 Apache-2.0 协议维护。
- Official Stoplight GitHub organization
docs.stoplight.io 官方文档对 CLI 参数、规则集与内置函数说明完备。
- Official docs at docs.stoplight.io/docs/spectral/
安全边界、鉴权与放权指南
无需任何凭据;纯本地校验工具(仅当规范中显式引用远程 $ref 或规则 URL 时才会发起 HTTP 读取)。
免费开源软件(Apache-2.0)。
# 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 高频安全命令配方
只读查询(安全推荐)
spectral lint openapi.yaml --format json --quiet
依据 .spectral.yaml(或默认 OAS 规则)校验 openapi.yaml 并输出安静的 JSON 诊断结果。
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。