@contentrail/core

中文文档
0.2.6发布于 2026/11/08MITTypeScriptES Module近 30 天下载 2304

Framework-independent ContentRail schemas, validation, and content types.

原始使用文档
文档目录14 节
  1. Requirements
  2. Install
  3. Monorepo (current)
  4. From npm ( 0.2.6 )
  5. Define a collection
  6. Unexpected fields
  7. Validate an entry
  8. Validate a collection (array of entries)
  9. Type inference
  10. ContentSchema
  11. Errors
  12. Public exports
  13. Compatibility
  14. Current limitations

@contentrail/core

Framework-independent ContentRail schemas, validation, and inferred content types.

This package defines collections and validation only. It does not read files, talk to networks, store content, authenticate users, or publish sites.

Requirements

  • Node.js ≥ 22 (engines.node)
  • TypeScript 5.9+ recommended for consumers
  • Zod ^4.6.7 (dependency + peer; native zod entry — not zod/v3)

Prefer validateEntry / validateCollection / assertValidEntry for untrusted input. Calling schema.safeParse() directly bypasses ContentRail JSON bounds.

Upgrading from ContentRail 0.1.0 (Zod 3): see docs/MIGRATION_ZOD4.md.

Install

Monorepo (current)

pnpm install
pnpm --filter @contentrail/core build

From npm (0.2.6)

pnpm add @contentrail/[email protected] zod@^4.6.7

Define a collection

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(),
  }),
});

Collection names must match ^[a-z][a-z0-9_-]*$.

Unexpected fields

unknownKeysBehaviour
"strip" (default)Unknown root-object keys are removed.
"strict"Unknown root-object keys fail validation.

The policy is applied when the schema is a Zod object. Non-object schemas are left unchanged.

Validate an entry

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 never throws for invalid content. Use assertValidEntry when you prefer exceptions.

Validate a collection (array of entries)

import { validateCollection } from "@contentrail/core";

const result = validateCollection(articles, [
  { title: "Welcome", slug: "welcome", published: true },
  { title: "Next", slug: "next", published: false },
]);

Failing issues are prefixed with the entry index, for example 1.title.

Type inference

import type { InferEntry } from "@contentrail/core";

type Article = InferEntry<typeof articles>;
// { title: string; slug: string; published: boolean; publishedAt?: string }

ContentSchema

ContentSchema is the public Zod 4 schema constraint used by defineCollection / ContentCollection generics. It is an alias of z.ZodType from the native zod entry:

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,
});

Prefer defining schemas with z and letting defineCollection infer types; use ContentSchema when you need an explicit annotation.

Errors

ErrorWhen
ContentDefinitionErrorInvalid defineCollection input (thrown).
ContentValidationErrorInvalid content (validate* result or assertValidEntry).

ContentValidationError includes:

  • collection — collection name
  • issues — { path, code, message, expected?, received? }[]
  • toJSON() — stable serializable shape

Public exports

From @contentrail/core:

  • defineCollection
  • validateEntry
  • validateCollection
  • assertValidEntry
  • ContentDefinitionError
  • ContentValidationError
  • Types: ContentSchema, ContentCollection, DefineCollectionInput, InferEntry, UnknownKeysPolicy, ValidationResult, ValidationSuccess, ValidationFailure, ValidationIssue

There is a single browser-safe export map entry (.). No server-only subpath exists in this package.

Compatibility

  • Runtime: any modern ESM environment (browsers, Node, Workers) that can run Zod
  • Module format: ESM only
  • Not coupled to Astro, Vite, Cloudflare, or React

Current limitations

  • No content I/O, Git, R2, or publishing APIs
  • No editor schema introspection helpers beyond Zod itself
  • No JSON Schema export
  • Requires Zod 4.6.7+; Zod 3 schemas are not supported
  • See root README / docs/PUBLISHING.md for 0.2.6 publication status

版本与发布记录

@contentrail/core 共发布 9 个版本,首次发布于 2026/10/30;过去 12 个月发布了 9 个版本。

版本发布日期距今
0.2.6latest2026/11/082 天前
0.2.52026/11/082 天前
0.2.42026/11/038 天前
0.2.32026/11/028 天前
0.2.22026/11/028 天前
0.2.12026/10/3110 天前

关于 @contentrail/core 的常见问题

@contentrail/core 是什么?

Framework-independent ContentRail schemas, validation, and content types.

如何安装 @contentrail/core?

在项目目录运行 npm install @contentrail/core,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:zod(^4.6.7)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@contentrail/core 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@contentrail/core 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@contentrail/core 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=22。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@contentrail/core 可以商用吗?

@contentrail/core 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@contentrail/core 的下载量有多少?

根据 npm 官方下载统计,@contentrail/core 在 2026/10/11 – 2026/11/09 这 30 天内被下载 2,304 次,日均约 77 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@contentrail/core 还在维护吗?

最新版本 0.2.6 发布于 2026/11/08(2 天前);至今共发布 9 个版本,其中过去 12 个月发布了 9 个;首次发布于 2026/10/30。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。