知识卡片
必填规则选择父对象
在规则中用 given: $.info 让 Spectral 找到父对象,再用 then.field: description 与 then.function: truthy 检查描述。若直接选 $.info.description,缺失时可能没有选中元素,规则无法发现它。应至少用有、无与空描述的定义验证预期。truthy 校验真值,字段类型另应交给 schema 或专用函数。
这张卡片看不懂?
这张卡片已通过人工审查,不再接受失败反馈。
已有反馈 · 0 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- API Lint 与设计一致性 3.6 Creating a Spectral rule for info.description · 查看原书依据
The given property is a JSONPath expression or collection of expressions that specify the parts of the document where the rule should be applied. The JSONPath expression can return zero or more elements, but if it returns no elements, the rule won’t be evaluated. For your scenario, the given property can be the JSONPath expression $.info, which returns the entire info object.
参考来源
- 位置:《Automating API Delivery APIOps with OpenAPI》《第 03 章 API Lint 与设计一致性》(3.6 Creating a Spectral rule for info.description;来源项目 book-source,extracted/Automating API Delivery APIOps with OpenAPI/_epub-src/OEBPS/Text/chapter-3.xhtml#p61)
- 结论依据:原文明确选择器返回零项时规则不运行,因此缺字段检查要选择父对象。
- 原始内容:The given property is a JSONPath expression or collection of expressions that specify the parts of the document where the rule should be applied. The JSONPath expression can return zero or more elements, but if it returns no elements, the rule won’t be evaluated. For your scenario, the given property can be the JSONPath expression $.info, which returns the entire info object.