@eslint/object-schema

中文文档
3.0.5发布于 2026/04/08Apache-2.0TypeScriptESM + CJS零依赖构建来源已验证近 30 天下载 5.1亿

An object schema merger/validator

原始使用文档
文档目录17 节
  1. Overview
  2. Installation
  3. Usage
  4. Tips and Tricks
  5. Named merge strategies
  6. Named validation strategies
  7. Built-in strategy classes
  8. Subschemas
  9. Remove Keys During Merge
  10. Requiring Another Key Be Present
  11. License
  12. Sponsors
  13. Platinum Sponsors
  14. Gold Sponsors
  15. Silver Sponsors
  16. Bronze Sponsors
  17. Technology Sponsors

ObjectSchema Package

Overview

A JavaScript object merge/validation utility where you can define a different merge and validation strategy for each key. This is helpful when you need to validate complex data structures and then merge them in a way that is more complex than Object.assign(). This is used in the @eslint/config-array package but can also be used on its own.

Installation

For Node.js and compatible runtimes:

npm install @eslint/object-schema
# or
yarn add @eslint/object-schema
# or
pnpm install @eslint/object-schema
# or
bun add @eslint/object-schema

For Deno:

deno add @eslint/object-schema

Usage

Import the ObjectSchema constructor:

// using ESM
import { ObjectSchema } from "@eslint/object-schema";

// using CommonJS
const { ObjectSchema } = require("@eslint/object-schema");

const schema = new ObjectSchema({
    // define a definition for the "downloads" key
    downloads: {
        required: true,
        merge(value1 = 0, value2) {
            return value1 + value2;
        },
        validate(value) {
            if (typeof value !== "number") {
                throw new Error("Expected downloads to be a number.");
            }
        },
    },

    // define a strategy for the "versions" key
    versions: {
        required: true,
        merge(value1 = [], value2) {
            return value1.concat(value2);
        },
        validate(value) {
            if (!Array.isArray(value)) {
                throw new Error("Expected versions to be an array.");
            }
        },
    },
});

const record1 = {
    downloads: 25,
    versions: ["v1.0.0", "v1.1.0", "v1.2.0"],
};

const record2 = {
    downloads: 125,
    versions: ["v2.0.0", "v2.1.0", "v3.0.0"],
};

// make sure the records are valid
schema.validate(record1);
schema.validate(record2);

// merge together (schema.merge() accepts any number of objects)
const result = schema.merge(record1, record2);

// result looks like this:
// {
// 	downloads: 150,
// 	versions: ["v1.0.0", "v1.1.0", "v1.2.0", "v2.0.0", "v2.1.0", "v3.0.0"],
// }

Tips and Tricks

Named merge strategies

Instead of specifying a merge() method, you can specify one of the following strings to use a default merge strategy:

  • "assign" - use Object.assign() to merge the two values into one object.
  • "overwrite" - the second value always replaces the first.
  • "replace" - the second value replaces the first if the second is not undefined.

For example:

const schema = new ObjectSchema({
    name: {
        merge: "replace",
        validate() {},
    },
});

Named validation strategies

Instead of specifying a validate() method, you can specify one of the following strings to use a default validation strategy:

  • "array" - value must be an array.
  • "boolean" - value must be a boolean.
  • "number" - value must be a number.
  • "object" - value must be a non-null object, including arrays and non-plain objects.
  • "object?" - value must be an object or null, including arrays and non-plain objects.
  • "string" - value must be a string.
  • "string!" - value must be a non-empty string.

For example:

const schema = new ObjectSchema({
    name: {
        merge: "replace",
        validate: "string",
    },
});

Built-in strategy classes

The package also exports the built-in merge and validation strategies as two classes with static methods:

  • MergeStrategy - built-in merge functions (assign, overwrite, replace).
  • ValidationStrategy - built-in validation functions (array, boolean, number, object, object?, string, string!).

These are the same strategies used when you specify a strategy by name (for example, merge: "replace"). You can reference the functions directly if you prefer passing a function instead of a string:

import {
    ObjectSchema,
    MergeStrategy,
    ValidationStrategy,
} from "@eslint/object-schema";

const schema = new ObjectSchema({
    name: {
        required: true,
        merge: MergeStrategy.replace,
        validate: ValidationStrategy["string!"],
    },
    options: {
        required: false,
        merge: MergeStrategy.assign,
        validate: ValidationStrategy["object?"],
    },
});

Note: Because object? and string! aren't valid identifiers, you must access them using bracket notation (for example, ValidationStrategy["object?"]).

Subschemas

If you are defining a key that is, itself, an object, you can simplify the process by using a subschema. Instead of defining merge() and validate(), set a schema property that contains a schema definition, like this:

const schema = new ObjectSchema({
    name: {
        schema: {
            first: {
                merge: "replace",
                validate: "string",
            },
            last: {
                merge: "replace",
                validate: "string",
            },
        },
    },
});

schema.validate({
    name: {
        first: "n",
        last: "z",
    },
});

Remove Keys During Merge

If the merge strategy for a key returns undefined, then the key will not appear in the final object. For example:

const schema = new ObjectSchema({
    date: {
        merge() {
            return undefined;
        },
        validate(value) {
            if (isNaN(Date.parse(value))) {
                throw new Error("Invalid date.");
            }
        },
    },
});

const object1 = { date: "5/5/2005" };
const object2 = { date: "6/6/2006" };

const result = schema.merge(object1, object2);

console.log("date" in result); // false

Requiring Another Key Be Present

If you'd like the presence of one key to require the presence of another key, you can use the requires property to specify an array of other properties that any key requires. For example:

const schema = new ObjectSchema({
    date: {
        merge() {
            return undefined;
        },
        validate(value) {
            if (isNaN(Date.parse(value))) {
                throw new Error("Invalid date.");
            }
        },
    },
    time: {
        requires: ["date"],
        merge(first, second) {
            return second;
        },
        validate(value) {
            // ...
        },
    },
});

// throws error: Key "time" requires keys "date".
schema.validate({
    time: "13:45",
});

In this example, even though date is an optional key, it is required to be present whenever time is present.

License

Apache 2.0

Sponsors

The following companies, organizations, and individuals support ESLint's ongoing maintenance and development. Become a Sponsor to get your logo on our READMEs and website.

Platinum Sponsors

Automattic

Gold Sponsors

Qlty Software

Silver Sponsors

Vite Liftoff StackBlitz

Bronze Sponsors

Cybozu SAP CrawlJobs aeriusventilation's Org Depot Icons8 Discord GitBook HeroCoders TestMu AI Open Source Office (Formerly LambdaTest)

Technology Sponsors

Technology sponsors allow us to use their products and services for free as part of a contribution to the open source ecosystem and our work.

Netlify Algolia 1Password

版本与发布记录

@eslint/object-schema 共发布 11 个版本,首次发布于 2024/05/31;过去 12 个月发布了 6 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
3.0.5latest2026/04/086 个月前
3.0.42026/04/047 个月前
3.0.32026/03/078 个月前
3.0.22026/02/208 个月前
3.0.12026/02/078 个月前
3.0.02025/11/1511 个月前

关于 @eslint/object-schema 的常见问题

@eslint/object-schema 是什么?

An object schema merger/validator

如何安装 @eslint/object-schema?

在项目目录运行 npm install @eslint/object-schema,或使用 pnpm add / yarn add / bun add。

@eslint/object-schema 支持 TypeScript 吗?

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

@eslint/object-schema 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@eslint/object-schema 需要什么版本的 Node.js?

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

@eslint/object-schema 可以商用吗?

@eslint/object-schema 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@eslint/object-schema 的下载量有多少?

根据 npm 官方下载统计,@eslint/object-schema 在 2026/10/07 – 2026/11/05 这 30 天内被下载 511,789,461 次,日均约 17,059,649 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@eslint/object-schema 还在维护吗?

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