Skip to content

为 yaml 配置文件增加提示

安装 yaml 插件

在应用市场安装 vscode 插件

定义 schema

示例如下

json
{
  "$schema": "http://json-schema.org/draft-07/schema",
  "$id": "jason",
  "title": "应用配置",
  "type": "object",
  "properties": {
    "server": {
      "type": "object",
      "description": "当前服务",
      "required": ["name", "host", "port", "prefix"],
      "properties": {
        "name": {
          "type": "string",
          "enum": [
            "gateway-service",
            "goods-service",
            "auth-service",
            "order-service"
          ]
        },
        "host": {
          "type": "string",
          "title": "主机",
          "description": "主机ip"
        },
        "port": {
          "type": "number"
        },
        "prefix": {
          "type": "string"
        }
      }
    },
    "isProd": {
      "type": "boolean"
    },
    "isDev": {
      "type": "boolean"
    },
    "mysql": {
      "type": "object",
      "properties": {
        "user": {
          "type": "string"
        },
        "password": {
          "type": "string"
        },
        "database": {
          "type": "string"
        },
        "charset": {
          "type": "string"
        },
        "host": {
          "type": "string"
        },
        "port": {
          "type": "integer"
        }
      }
    },
    "mongo": {
      "type": "object",
      "properties": {
        "uri": {
          "type": "string"
        }
      }
    },
    "redis": {
      "type": "object",
      "properties": {
        "host": {
          "type": "string"
        },
        "port": {
          "type": "number"
        },
        "password": {
          "type": "string"
        },
        "db": {
          "type": "number"
        },
        "mqdb": {
          "type": "number"
        }
      }
    },
    "swagger": {
      "type": "object",
      "properties": {
        "prefix": {
          "type": "string"
        }
      }
    },
    "emailAuth": {
      "type": "object",
      "properties": {
        "user": {
          "type": "string"
        },
        "pass": {
          "type": "string"
        }
      }
    },
    "expireTime": {
      "type": "object",
      "properties": {
        "emailCode": {
          "type": "number"
        },
        "resendEmailCode": {
          "type": "number"
        }
      }
    },

    "nest": {
      "type": "object",
      "properties": {
        "gateway": {
          "type": "array",
          "items": {
            "type": "object",
            "properties": {
              "id": {
                "type": "string"
              },
              "path": {
                "type": "string"
              },
              "target": {
                "type": "string"
              }
            }
          }
        }
      }
    },
    "geetest": {
      "type": "object",
      "properties": {
        "CAPTCHA_ID": {
          "type": "string"
        },
        "CAPTCHA_KEY": {
          "type": "string"
        },
        "API_SERVER": {
          "type": "string"
        }
      }
    }
  }
}

引入 schema 配置(任选一种)

  • yml 文件顶部插入以下
    # yaml-language-server:$schema=path/to/schema.json,路径可以为相对路径或者网络地址
  • settings.json中增加如下配置
    json
    {
      "yaml.schemas": {
        "path/to/schema.json": ["path/to/yml"]
      }
    }

参考资料

Released under the MIT License.