You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Swagger Editor自定义语义验证器失效求助

自定义Swagger Editor语义验证器未生效的排查与修复

问题背景

在Swagger Editor开源代码的plugin/validate-semantic/validators目录下新增customValidator.js,实现校验YAML中info.version必须为2.0或3.0.0的逻辑,不符合则抛出错误。随后在plugin/validate-semantic/index.js中导入该验证器并添加到validate的actions集合,但验证器未生效。

相关代码

customValidator.js

export const myValidator = sys => {
  return sys.validateSelectors
    .allInfos()
    .then(nodes => {
      return nodes.reduce((acc, node) => {
        const value = node.node;

        if (value.version !== '2.0' && value.version !== '3.0.0') {
          acc.push({
            level: 'error',
            message: `Unsupported Swagger version: ${value.version}`,
            path: [...node.path, 'version']
          });
        }

        return acc;
      }, []);
    });
};

index.js导入片段

import * as customValidator from "./validators/2and3/customValidator"
// ...
actions: {
  // ...其他验证器
  ...customValidator,
}

排查与修复步骤

  1. 修正选择器调用
    OpenAPI规范中info是根节点下的单个对象,而非数组,原代码中使用的allInfos()选择器会返回空数组,导致验证逻辑完全未执行。将选择器改为info()获取单个节点:

    export const myValidator = sys => {
      return sys.validateSelectors
        .info()
        .then(node => {
          const errors = [];
          if (node) {
            const { version } = node.node;
            if (version && version !== '2.0' && version !== '3.0.0') {
              errors.push({
                level: 'error',
                message: `Unsupported Swagger version: ${version}`,
                path: [...node.path, 'version']
              });
            }
          }
          return errors;
        });
    };
    
  2. 核对文件导入路径
    确认customValidator.js的实际位置:

    • 若文件直接在validators目录下,导入路径应为./validators/customValidator
    • 若在validators/2and3子目录下,当前路径正确
  3. 重新构建项目
    修改Swagger Editor源码后,必须执行构建命令重启服务:

    npm run build
    npm start
    

    仅刷新浏览器无法加载修改后的代码。

  4. 对齐官方验证器规范
    参考validators目录下官方验证器的写法,确保函数返回格式、错误结构与现有验证器一致,避免因格式不兼容被忽略。

内容的提问来源于stack exchange,提问作者Amine BEN JABEUR

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.22 21:40:29