{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "title": "云书阁规则书源 Schema（Legado 3.x / Sigma 兼容与 OpenAPI v1 扩展）",
  "description": "面向 Legado 3.x / Sigma 兼容书源及 OpenAPI v1 扩展（omnireadLogin、omnireadMediaType）的结构校验。OpenAPI v2 固定接口书源请使用 next/source.schema.json。未知字段会在导出时无损保留，但不会自动获得引擎能力。",
  "oneOf": [
    {
      "$ref": "#/$defs/source"
    },
    {
      "type": "array",
      "minItems": 1,
      "maxItems": 5000,
      "items": {
        "$ref": "#/$defs/source"
      }
    }
  ],
  "$defs": {
    "source": {
      "type": "object",
      "required": [
        "bookSourceName",
        "bookSourceUrl"
      ],
      "properties": {
        "bookSourceName": {
          "type": "string",
          "minLength": 1,
          "description": "书源显示名称"
        },
        "bookSourceUrl": {
          "type": "string",
          "format": "uri",
          "pattern": "^https?://[^?#@]+$",
          "description": "书源去重身份与相对地址基准，须为干净的 HTTP(S) 地址"
        },
        "bookSourceType": {
          "type": "integer",
          "enum": [
            0,
            1,
            2,
            3,
            4
          ],
          "default": 0,
          "description": "0=小说，1=有声书，2=漫画，3=文件/直链下载，4=短剧/视频"
        },
        "bookSourceVersion": {
          "type": "string",
          "minLength": 1,
          "description": "作者维护的版本号"
        },
        "bookSourceGroup": {
          "type": "string",
          "description": "书源分组标签"
        },
        "bookSourceComment": {
          "type": "string",
          "description": "作者说明与备注（不作为可执行配置）"
        },
        "variableComment": {
          "type": "string",
          "description": "源变量说明；非空时在书源设置中渲染「源变量」安全输入卡片"
        },
        "bookUrlPattern": {
          "type": "string",
          "description": "全匹配正则；搜索结果页地址命中时直接按单本详情解析"
        },
        "customOrder": {
          "type": "integer",
          "default": 0
        },
        "enabled": {
          "type": "boolean",
          "default": true
        },
        "enabledExplore": {
          "type": "boolean",
          "default": true
        },
        "enabledCookieJar": {
          "type": "boolean",
          "default": false,
          "description": "是否启用 Cookie 持久化；与 omnireadLogin 互斥"
        },
        "concurrentRate": {
          "type": "string",
          "minLength": 1,
          "description": "限流配置：毫秒间隔或 次数/毫秒，例如 500 或 3/1000"
        },
        "header": {
          "type": "string",
          "minLength": 1,
          "description": "JSON 对象字符串，或返回 JSON 对象字符串的 @js:/<js> 脚本"
        },
        "jsLib": {
          "type": "string",
          "description": "内联 JavaScript 代码，或映射绝对 http(s) 脚本 URL 的 JSON 对象字符串（上限 2 MiB）"
        },
        "loginUrl": {
          "type": "string",
          "description": "纯网页登录的 http(s) 地址，或定义 function login() 与辅助函数的 JavaScript 脚本"
        },
        "loginUi": {
          "type": "string",
          "description": "RowUi 控件数组的 JSON 字符串，或返回该数组的 @js:/<js> 脚本"
        },
        "loginCheckJs": {
          "type": "string",
          "description": "每次 LegadoWebBook 请求后执行的登录/响应校验脚本"
        },
        "coverDecodeJs": {
          "type": "string",
          "description": "封面图片二进制解密脚本（输入字节数组 result，输出解码后字节数组，上限 40 MiB）"
        },
        "exploreUrl": {
          "type": "string",
          "description": "静态 标题::URL 列表、ExploreKind JSON 数组字符串，或返回二者的脚本"
        },
        "exploreScreen": {
          "type": "string",
          "description": "扩展筛选元数据（无损保留并暴露给 source.exploreScreen）"
        },
        "searchUrl": {
          "type": "string",
          "minLength": 1,
          "description": "搜索请求模板，支持 {{key}}、{{page}}、<p1,p2>、,{options} 与脚本"
        },
        "charset": {
          "type": "string",
          "minLength": 1,
          "description": "响应编码提示，如 utf-8、gbk、gb18030、big5"
        },
        "lastUpdateTime": {
          "type": "integer"
        },
        "respondTime": {
          "type": "integer"
        },
        "weight": {
          "type": "integer"
        },
        "eventListener": {
          "type": "boolean",
          "default": false,
          "description": "启用 Sigma 事件回调（ruleContent.callBackJs）"
        },
        "customButton": {
          "type": "boolean",
          "default": false
        },
        "ruleSearch": {
          "type": "object",
          "properties": {
            "checkKeyWord": {
              "type": "string"
            },
            "bookList": {
              "type": "string"
            },
            "name": {
              "type": "string"
            },
            "author": {
              "type": "string"
            },
            "intro": {
              "type": "string"
            },
            "kind": {
              "type": "string"
            },
            "lastChapter": {
              "type": "string"
            },
            "updateTime": {
              "type": "string"
            },
            "bookUrl": {
              "type": "string"
            },
            "coverUrl": {
              "type": "string"
            },
            "wordCount": {
              "type": "string"
            }
          },
          "required": [
            "bookList",
            "name",
            "bookUrl"
          ],
          "additionalProperties": true
        },
        "ruleExplore": {
          "type": "object",
          "properties": {
            "bookList": {
              "type": "string"
            },
            "name": {
              "type": "string"
            },
            "author": {
              "type": "string"
            },
            "intro": {
              "type": "string"
            },
            "kind": {
              "type": "string"
            },
            "lastChapter": {
              "type": "string"
            },
            "updateTime": {
              "type": "string"
            },
            "bookUrl": {
              "type": "string"
            },
            "coverUrl": {
              "type": "string"
            },
            "wordCount": {
              "type": "string"
            }
          },
          "required": [],
          "additionalProperties": true
        },
        "ruleBookInfo": {
          "type": "object",
          "properties": {
            "init": {
              "type": "string"
            },
            "name": {
              "type": "string"
            },
            "author": {
              "type": "string"
            },
            "intro": {
              "type": "string"
            },
            "kind": {
              "type": "string"
            },
            "lastChapter": {
              "type": "string"
            },
            "updateTime": {
              "type": "string"
            },
            "coverUrl": {
              "type": "string"
            },
            "tocUrl": {
              "type": "string"
            },
            "wordCount": {
              "type": "string"
            },
            "canReName": {
              "type": "string"
            },
            "downloadUrls": {
              "type": "string"
            },
            "downloadUrl": {
              "type": "string",
              "description": "downloadUrls 的兼容别名，新源请使用 downloadUrls"
            }
          },
          "required": [],
          "additionalProperties": true
        },
        "ruleToc": {
          "type": "object",
          "properties": {
            "preUpdateJs": {
              "type": "string"
            },
            "chapterList": {
              "type": "string"
            },
            "chapterName": {
              "type": "string"
            },
            "chapterUrl": {
              "type": "string"
            },
            "formatJs": {
              "type": "string"
            },
            "isVolume": {
              "type": "string"
            },
            "isVip": {
              "type": "string"
            },
            "isPay": {
              "type": "string"
            },
            "updateTime": {
              "type": "string"
            },
            "nextTocUrl": {
              "type": "string"
            }
          },
          "required": [
            "chapterList",
            "chapterName",
            "chapterUrl"
          ],
          "additionalProperties": true
        },
        "ruleContent": {
          "type": "object",
          "properties": {
            "content": {
              "type": "string"
            },
            "subContent": {
              "type": "string"
            },
            "title": {
              "type": "string"
            },
            "nextContentUrl": {
              "type": "string"
            },
            "webJs": {
              "type": "string"
            },
            "sourceRegex": {
              "type": "string"
            },
            "replaceRegex": {
              "type": "string"
            },
            "imageStyle": {
              "type": "string"
            },
            "imageDecode": {
              "type": "string"
            },
            "payAction": {
              "type": "string"
            },
            "callBackJs": {
              "type": "string"
            }
          },
          "required": [
            "content"
          ],
          "additionalProperties": true
        },
        "ruleReview": {
          "oneOf": [
            {
              "type": "object",
              "properties": {
                "enabled": {
                  "oneOf": [
                    {
                      "type": "boolean"
                    },
                    {
                      "type": "string"
                    }
                  ]
                },
                "reviewSummaryUrl": {
                  "type": "string"
                },
                "summaryListRule": {
                  "type": "string"
                },
                "summaryParagraphIndexRule": {
                  "type": "string"
                },
                "summaryParagraphDataRule": {
                  "type": "string"
                },
                "summaryCountRule": {
                  "type": "string"
                },
                "reviewDetailUrl": {
                  "type": "string"
                },
                "reviewUrl": {
                  "type": "string"
                },
                "reviewDetailNextPageUrl": {
                  "type": "string"
                },
                "detailListRule": {
                  "type": "string"
                },
                "detailIdRule": {
                  "type": "string"
                },
                "detailAvatarRule": {
                  "type": "string"
                },
                "avatarRule": {
                  "type": "string"
                },
                "detailNameRule": {
                  "type": "string"
                },
                "detailBadgeRule": {
                  "type": "string"
                },
                "detailContentRule": {
                  "type": "string"
                },
                "contentRule": {
                  "type": "string"
                },
                "postTimeRule": {
                  "type": "string"
                },
                "reviewQuoteUrl": {
                  "type": "string"
                },
                "replyListRule": {
                  "type": "string"
                },
                "replyIdRule": {
                  "type": "string"
                },
                "replyAvatarRule": {
                  "type": "string"
                },
                "replyNameRule": {
                  "type": "string"
                },
                "replyBadgeRule": {
                  "type": "string"
                },
                "replyContentRule": {
                  "type": "string"
                }
              },
              "additionalProperties": true
            },
            {
              "type": "string"
            }
          ]
        },
        "omnireadLogin": {
          "type": "object",
          "required": [
            "version",
            "type",
            "loginUrl",
            "sessionUrl"
          ],
          "additionalProperties": false,
          "properties": {
            "version": {
              "type": "integer",
              "const": 1
            },
            "type": {
              "enum": [
                "password",
                "apiKey"
              ]
            },
            "loginUrl": {
              "type": "string",
              "format": "uri",
              "pattern": "^https://[^?#@]+$"
            },
            "sessionUrl": {
              "type": "string",
              "format": "uri",
              "pattern": "^https://[^?#@]+$"
            }
          }
        },
        "omnireadMediaType": {
          "type": "string",
          "const": "video",
          "description": "云书阁 v1 短剧扩展，须与 bookSourceType: 0 配合（也可直接使用 bookSourceType: 4）"
        }
      },
      "additionalProperties": true,
      "allOf": [
        {
          "anyOf": [
            {
              "required": [
                "searchUrl",
                "ruleSearch"
              ]
            },
            {
              "required": [
                "exploreUrl",
                "ruleExplore"
              ]
            }
          ]
        },
        {
          "if": {
            "properties": {
              "bookSourceType": {
                "const": 3
              }
            },
            "required": [
              "bookSourceType"
            ]
          },
          "then": {
            "required": [
              "ruleBookInfo"
            ],
            "properties": {
              "ruleBookInfo": {
                "anyOf": [
                  {
                    "required": [
                      "downloadUrls"
                    ]
                  },
                  {
                    "required": [
                      "downloadUrl"
                    ]
                  }
                ]
              }
            }
          },
          "else": {
            "required": [
              "ruleToc",
              "ruleContent"
            ]
          }
        },
        {
          "if": {
            "required": [
              "omnireadLogin"
            ]
          },
          "then": {
            "not": {
              "anyOf": [
                {
                  "required": [
                    "loginUrl"
                  ]
                },
                {
                  "required": [
                    "loginUi"
                  ]
                },
                {
                  "required": [
                    "loginCheckJs"
                  ]
                }
              ]
            },
            "properties": {
              "bookSourceUrl": {
                "pattern": "^https://"
              },
              "enabledCookieJar": {
                "const": false
              }
            }
          }
        },
        {
          "if": {
            "required": [
              "omnireadMediaType"
            ]
          },
          "then": {
            "properties": {
              "bookSourceType": {
                "const": 0
              }
            }
          }
        }
      ]
    }
  }
}
