作者:
    Creation:2026-11-25Last update:2026-11-25

    使用 Schema 校验字典

    Schema 描述了字典内容的预期结构。你只需在 intlayer.config.ts 中声明一次并为其命名,然后通过 schema 字段将该名称关联到任意字典。

    之后 Intlayer 会:

    • 在构建字典时针对每个 locale 校验字典,并报告每个问题所在的 locale 和字段路径。
    • 在编辑器中为字典内容提供类型,让缺失或类型错误的字段在构建之前就被发现。

    当多个字典需要共享相同结构(SEO 元数据、商品卡片、类 CMS 条目),或者内容需要满足 TypeScript 无法表达的约束(例如每条翻译的最大长度)时,Schema 非常有用。

    在渲染 Markdown 前,验证 frontmatter 中的 title、description、date 和 tags 等字段。构建词典时会检查必填字段及其预期类型,并报告错误元数据对应的语言和字段路径。

    验证用于生成页面标题、描述、规范 URL、社交图片和索引指令的元数据。Schema 可以要求标题长度为 1 到 60 个字符,将描述限制为 160 个字符,并检查 URL 是否有效。某个语言的无效内容会在导致页面渲染错误前被拒绝。

    为具有相同内容约定的词典使用同一个 Schema,例如产品条目、导航项或邮件模板。缺少字段或字段值类型错误会使该词典在构建时校验失败,避免不一致的内容进入应用。

    1. 在配置中声明 schema

      安装 Zod:

      bash
      npm install zod --save-dev
      

      然后在 schemas 键下声明你的 schema。每个键就是你在字典中引用的名称。

      intlayer.config.ts
      import { Locales, type IntlayerConfig } from "intlayer";
      import { z } from "zod";
      
      const config: IntlayerConfig = {
        internationalization: {
          locales: [Locales.ENGLISH, Locales.FRENCH, Locales.SPANISH],
          defaultLocale: Locales.ENGLISH,
        },
        schemas: {
          "seo-metadata": z.object({
            title: z.string().min(10).max(60),
            description: z.string().min(50).max(160),
          }),
          "product-card": z.object({
            name: z.string(),
            price: z.number().positive(),
            tags: z.array(z.string()),
          }),
        },
      };
      
      export default config;
      
    2. 将 schema 关联到字典

      将字典的 schema 字段设置为配置中声明的某个名称。

      src/about/aboutMetadata.content.ts
      import { t, type Dictionary } from "intlayer";
      
      const aboutMetadataContent = {
        key: "about-metadata",
        schema: "seo-metadata",
        content: {
          title: t({
            en: "About us | Our story",
            fr: "À propos | Notre histoire",
            es: "Sobre nosotros | Nuestra historia",
          }),
          description: t({
            en: "Discover the team, the mission and the values behind our product.",
            fr: "Découvrez l'équipe, la mission et les valeurs derrière notre produit.",
            es: "Descubre el equipo, la misión y los valores detrás de nuestro producto.",
          }),
        },
      } satisfies Dictionary<undefined, "seo-metadata">;
      
      export default aboutMetadataContent;
      

      同一个 schema 可以关联到任意数量的字典。

    3. 构建并查看报告

      每次 Intlayer 构建字典时都会执行校验:无论是通过 npx intlayer build,还是在 dev 和 build 模式下通过打包工具插件。如果某条翻译违反了 schema,Intlayer 会输出 locale、字段路径和 Zod 的错误信息:

      bash
      [intlayer] Local dictionary about-metadata has invalid content according to schema seo-metadata for locale French (fr) - src/about/aboutMetadata.content.ts
      [intlayer] ✗ Error: about-metadata - French (fr) - title: Too big: expected string to have <=60 characters
      

      单次运行 intlayer build 时,遇到未声明的 schema 名称或校验失败都会导致构建失败。在监听模式下,Intlayer 会排除无效的字典,直到其内容得到修正。

    根据 schema 为内容提供类型

    Dictionary 的泛型参数决定了 TypeScript 检查字典的严格程度:

    声明schema 字段content 字段
    satisfies Dictionary自动补全,未知名称会报错不由 TypeScript 检查
    satisfies Dictionary<undefined, "seo-metadata">必须为 "seo-metadata"根据 schema 推导类型
    satisfies Dictionary<{ cta: string }, "seo-metadata">必须为 "seo-metadata"根据 schema 和你自己的类型推导

    使用带类型的 schema 时,每个字段既可以接受普通值,也可以接受生成该值的内容节点,因此仍然可以使用 t():

    src/product/productCard.content.ts
    import { t, type Dictionary } from "intlayer";
    
    const productCardContent = {
      key: "product-card",
      schema: "product-card",
      content: {
        name: t({ en: "Running shoes", fr: "Chaussures de course" }),
        price: "89", // ❌ Type 'string' is not assignable to type 'number'
        tags: ["sport", "outdoor"],
      },
    } satisfies Dictionary<undefined, "product-card">;
    
    export default productCardContent;
    
    每次构建字典时,Intlayer 都会根据你的 Zod schema 在 .intlayer/types 中生成这些类型。类型只覆盖结构:.min()、.max() 或 .regex() 等约束会在构建时检查。

    校验内容

    对于 internationalization.locales 中列出的每个 locale,Intlayer 会先解析该 locale 下的字典内容,再校验结果。因此 schema 描述的是字段的最终值(title: string),而不是保存翻译的 t() 节点。

    • 缺失的翻译:在默认的 strictMode 下,缺失的翻译会在校验前回退到默认 locale。在 strictMode: "strict" 下,缺失的翻译会导致必填字段校验失败。
    • 其他内容节点:enu()、plural()、cond()、gender()、select() 和 insert() 会被解析为函数,md() 或 html() 会被解析为节点。请使用 z.any() 声明这些字段,并将约束放在解析为普通值的字段上。
    • 不会被校验:由函数或 Promise 提供的内容(function fetching),以及从 Intlayer CMS 拉取的字典。

    使用其他校验库

    schemas 中的条目并不限定于 Zod。任何提供与 Zod 兼容的 safeParse(data) 方法的对象都可以使用,只要它返回 { success: true } 或 { success: false, error: { issues } },其中每个 issue 都包含 path 数组和 message。内容类型只会从 Zod schema 生成:使用其他库时,关联字典的内容类型为 any。

    常见问题

    单次运行 intlayer build 时,如果字典不符合其 schema,构建就会失败。在监听模式下,Intlayer 会记录错误并排除无效的字典,直到你修正并保存它。

    可以。schema 通过名称引用,因此任意数量的字典都可以指向它。这正是主要用例:为所有页面元数据、商品条目或邮件模板强制统一的结构。

    只要声明了至少一个 schema,TypeScript 就会报错,因为 schema 字段只接受已声明的名称。如果 JSON 文件引用了未声明的 schema 名称,Intlayer 会在构建时报错并拒绝该字典。

    会校验 internationalization.locales 中列出的所有 locale。报告会标明失败的 locale,因此很容易发现只在德语中过长的翻译。

    相关文档