@contentrail/core
框架独立的ContentRail schemas,校验,并推断出内容类型.
此软件包定义 仅收集并验证。它不读取文件、与网络交谈、存储内容、认证用户或发布网站.
所需资源
- 节点.js 第22条 (单位:千美元)
engines.node(中文(简体) ) - 为消费者推荐的脚本类型 5.9+
- 佐德语Name
^4.6.7(依赖性+对等点;本地)zod条目-不zod/v3(中文(简体) )
偏爱 validateEntry 编号 : validateCollection 编号 : assertValidEntry 对不可信者
输入。 通话 schema.safeParse() 直接绕过ContentRail JSON边界.
从内容文件夹升级 0.1.0 (第3区):见 docs/MIGRATION_ZOD4.md。 。 。 .
安装
Monorepo( 当前)
pnpm install
pnpm --filter @contentrail/core build
从 npm (0.2.6(中文(简体) )
pnpm add @contentrail/[email protected] zod@^4.6.7
定义收藏
import { defineCollection } from "@contentrail/core";
import { z } from "zod";
const articles = defineCollection({
name: "articles",
schema: z.object({
title: z.string().min(1),
slug: z.string().min(1),
published: z.boolean(),
publishedAt: z.string().optional(),
}),
});
收藏名称必须匹配 ^[a-z][a-z0-9_-]*$。 。 。 .
非预期字段
unknownKeys | 行为 |
|---|---|
"strip" (默认) | 未知的根对象密钥被删除 . |
"strict" | 未知的根对象键失败验证 . |
当 schema 是 Zod 对象时, 将应用此策略 。 非对象计划保持不变.
验证条目
import { validateEntry } from "@contentrail/core";
const result = validateEntry(articles, {
title: "Welcome",
slug: "welcome",
published: true,
});
if (!result.success) {
console.error(result.error.issues);
} else {
console.log(result.data.title);
}
validateEntry 永不丢弃无效内容 。 使用 assertValidEntry 当你喜欢例外.
验证收藏( 条目组)
import { validateCollection } from "@contentrail/core";
const result = validateCollection(articles, [
{ title: "Welcome", slug: "welcome", published: true },
{ title: "Next", slug: "next", published: false },
]);
失败的问题在条目索引中前缀, 例如 1.title。 。 。 .
类型推论
import type { InferEntry } from "@contentrail/core";
type Article = InferEntry<typeof articles>;
// { title: string; slug: string; published: boolean; publishedAt?: string }
ContentSchema
ContentSchema 公共 Zod 4 策略约束
defineCollection 编号 : ContentCollection 通则. 它的别名是
z.ZodType 当地人 zod 条目 :
import { defineCollection, type ContentSchema } from "@contentrail/core";
import { z } from "zod";
const schema: ContentSchema = z.object({
title: z.string().min(1),
});
const pages = defineCollection({
name: "pages",
schema,
});
更倾向于用 z 并让 defineCollection 推断类型;
使用情况 ContentSchema 当你需要一个明确的注释.
错误
| 错误 | 何时 |
|---|---|
ContentDefinitionError | 无效 defineCollection 输入( thrown). |
ContentValidationError | 无效的内容( E)validate* 结果或 assertValidEntry) (中文(简体) ). |
ContentValidationError 包括:
collection— 收藏名称issues—— 说{ path, code, message, expected?, received? }[]toJSON()- 稳定的可序列形状
公共出口
从 @contentrail/core数字 :
defineCollectionvalidateEntryvalidateCollectionassertValidEntryContentDefinitionErrorContentValidationError- 类型 :
ContentSchema, (中文(简体) ),ContentCollection, (中文(简体) ),DefineCollectionInput, (中文(简体) ),InferEntry, (中文(简体) ),UnknownKeysPolicy, (中文(简体) ),ValidationResult, (中文(简体) ),ValidationSuccess, (中文(简体) ),ValidationFailure, (中文(简体) ),ValidationIssue
有一个单一的浏览器安全导出地图条目( Name.) (中文(简体) ). 此软件包中不存在仅服务器的子路径 .
兼容性
- 运行时间:任何能够运行Zod的现代无害环境管理环境(浏览器、节点、工人)
- 模块格式:仅限无害环境管理
- 不与Astro、Vite、Cloudflare或反应结合
目前的限制
- 无内容 I/O, Git, R2, 或发布 API
- 除了Zod本身之外,没有编辑器的反演帮助
- 没有 JSON Schema 导出
- 需要 Zod 吗 4.6.7+ (中文(简体) ); Zod 3 计划不支持
- 参见根 REDME /
docs/PUBLISHING.md页:10.2.6出版状况