From 5dbca049b6da07df1a5e81fd5fa7c064b739ab50 Mon Sep 17 00:00:00 2001 From: CPTProgrammer <46586216+CPTProgrammer@users.noreply.github.com> Date: Mon, 6 Jul 2026 08:02:40 +0800 Subject: [PATCH] Relax field constraints to match OpenAPI spec Remove length, url format, and minimum value validations to align with the upstream OpenAPI specification. Adjust field optionality in BaseVersion so that CreatableVersion and Version can differ correctly. --- src/types/modrinth/create-version.ts | 14 ++++++++--- src/types/modrinth/version.ts | 36 +++++++++++++++++----------- 2 files changed, 33 insertions(+), 17 deletions(-) diff --git a/src/types/modrinth/create-version.ts b/src/types/modrinth/create-version.ts index f8b38d9..34c53aa 100644 --- a/src/types/modrinth/create-version.ts +++ b/src/types/modrinth/create-version.ts @@ -1,12 +1,20 @@ import { z } from "zod"; -import { BaseVersionSchema, FileTypeEnumSchema } from "./version"; +import { + BaseVersionSchema, + FileTypeEnumSchema, + VersionDependencySchema, +} from "./version"; // region CreatableVersion — 创建版本请求中的 data JSON +// OpenAPI CreatableVersion.required: file_parts, project_id, name, version_number, +// game_versions, version_type, loaders, featured, dependencies export const CreatableVersionSchema = BaseVersionSchema.extend({ - /** 目标项目的 ID(8 位 base62) */ - project_id: z.string().length(8), + /** 目标项目的 ID */ + project_id: z.string(), /** 所有承载文件的 multipart 字段名列表 */ file_parts: z.array(z.string()), + /** 依赖列表。override BaseVersion 的 optional → required */ + dependencies: z.array(VersionDependencySchema), /** 主文件的 multipart 字段名;不提供时推断第一个文件为主文件 */ primary_file: z.string().optional(), /** 目标环境 */ diff --git a/src/types/modrinth/version.ts b/src/types/modrinth/version.ts index a52729f..c5cf79e 100644 --- a/src/types/modrinth/version.ts +++ b/src/types/modrinth/version.ts @@ -40,29 +40,30 @@ export const VersionFileSchema = z.object({ /** 文件哈希,包含 sha512 和 sha1 */ hashes: VersionFileHashesSchema, /** 文件直链下载地址 */ - url: z.string().url(), + url: z.string(), /** 文件名 */ filename: z.string(), /** 是否为主文件。每版本最多一个主文件;若无则为第一个文件 */ primary: z.boolean(), /** 文件大小(字节) */ - size: z.number().int().min(0), + size: z.number().int(), /** 文件类型;普通主 jar 为 null */ file_type: FileTypeEnumSchema.nullable(), }); export type VersionFile = z.infer; // endregion -// region BaseVersion — 版本基础字段(Version 和 CreatableVersion 的公共基类) +// region BaseVersion — 版本基础字段 +// OpenAPI BaseVersion 无 required 数组,以下按 "同时在两个子类都 required" 的字段设为必填 export const BaseVersionSchema = z.object({ /** 版本名称 */ name: z.string(), /** 版本号,建议遵循语义化版本 */ version_number: z.string(), /** 更新日志(Markdown/纯文本);使用 include_changelog=false 时为 null */ - changelog: z.string().nullable(), - /** 依赖列表 */ - dependencies: z.array(VersionDependencySchema), + changelog: z.string().nullable().optional(), + /** 依赖列表。CreatableVersion 中必填,Version 响应中可选;子类通过 override 差异化 */ + dependencies: z.array(VersionDependencySchema).optional(), /** 支持的 Minecraft 版本列表 */ game_versions: z.array(z.string()), /** 发布渠道 */ @@ -72,25 +73,32 @@ export const BaseVersionSchema = z.object({ /** 是否为精选版本 */ featured: z.boolean(), /** 版本状态 */ - status: z.enum(["listed", "archived", "draft", "unlisted", "scheduled"]), + status: z + .enum(["listed", "archived", "draft", "unlisted", "scheduled", "unknown"]) + .default("listed"), /** 请求的状态(用于审核流程) */ - requested_status: z.enum(["listed", "archived", "draft", "unlisted"]).nullable(), + requested_status: z + .enum(["listed", "archived", "draft", "unlisted"]) + .nullable() + .optional(), }); export type BaseVersion = z.infer; // endregion -// region Version — 版本完整响应体(GET /version/{id}、GET /project/{id}/version 等返回) +// region Version — 版本完整响应体 +// OpenAPI Version.required: id, project_id, author_id, date_published, downloads, files, +// name, version_number, game_versions, version_type, loaders, featured export const VersionSchema = BaseVersionSchema.extend({ /** 版本唯一 ID(8 位 base62) */ - id: z.string().length(8), - /** 所属项目 ID(8 位 base62) */ - project_id: z.string().length(8), + id: z.string(), + /** 所属项目 ID */ + project_id: z.string(), /** 发布者用户 ID */ author_id: z.string(), /** 发布时间(ISO-8601) */ - date_published: z.string().datetime(), + date_published: z.string(), /** 下载次数 */ - downloads: z.number().int().min(0), + downloads: z.number().int(), /** changelog URL(已废弃,始终为 null) */ changelog_url: z.string().nullable(), /** 该版本的所有文件列表 */