CodeSampleX

示例

@eslint/object-schema 2.1.7: ObjectSchema

已验证示例 — npm @eslint/object-schema 2.1.7: ObjectSchema. contract 在 node 22 · linux debian/x64 · docker 上运行并通过: ObjectSchema instantiates with key definitions…

sha256:bb92cd7773098d62df401c9a6730ea568d847c4d365f798f193a591eda9ce2bd

本网络只提供一件事:能构建的样本。它在沙箱中运行并保留签名回执。它不评级、不担保——同样的代码能否在你的环境构建,它没有测量过。 提交了通过的契约回执的不同签名密钥数量。为 1 表示只有作者;大于 1 表示还有其他人构建过。密钥是自行生成的,背后没有注册身份,因此计的是密钥而非人。 MIT-0

执行证据

声明的环境与签名的运行分开呈现,你可以看到这个样本究竟运行了什么、在哪里运行。

证据依据
签名契约通过
验证回执
1
构建过它的签名密钥
1
声明的环境 linux 24 · ubuntu · glibc 2.39 x64 npm

验证运行环境

环境 契约 阶段 运行日期
node 22 · linux debian/x64 · docker ed25519:c1973797be207ac4 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · node-typescript@1node:22@sha256:8a34c4ab3ea2…
2026-09-06

案例

HOW
目标
verify @eslint/object-schema.ObjectSchema in pkg:npm/%40eslint/object-schema@2.1.7
包
符号
  • @eslint/object-schema.ObjectSchema
创建时间
2026-09-06T10:28:52Z

契约

  1. ObjectSchema instantiates with key definitions and checks key presence using hasKey
  2. ObjectSchema validates objects against definitions and rejects unexpected keys, missing required keys, or invalid value types
  3. ObjectSchema enforces dependency constraints between keys using requires
  4. ObjectSchema supports custom validation functions and custom merge functions that retain existing values when returning undefined
  5. ObjectSchema recursively validates nested objects defined by subschemas
  6. ObjectSchema merges multiple objects using assign, overwrite, and replace strategies
  7. ValidationStrategy and MergeStrategy provide static helper methods for type validation and merge logic

文件

  • PROMPT.md
  • csx.json
  • index.mjs
  • package-lock.json
  • package.json
  • spec.json
  • test/contract.mjs

下载源代码构件 (tar.gz)

源代码

PROMPT.md
Clean-room public code sample — generation instructions

Write a brand-new, minimal, self-contained code sample in this clean-room directory.
Do not copy, paraphrase, or reference any existing project source. Work only from this spec.

A csx.json manifest scaffold already exists. Do not recreate it from memory. Preserve its case.goal, packages and symbols; fill its empty case.contract with exact assertions and correct its environment, commands and verifierAdapter for the files you generate.

Goal: verify @eslint/object-schema.ObjectSchema in pkg:npm/%40eslint/object-schema@2.1.7
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:npm/%40eslint/object-schema@2.1.7
Demonstrate these symbols/APIs:
  - @eslint/object-schema.ObjectSchema

Rules:
  - One focused purpose; the smallest project that proves the goal.
  - Include a contract test (test/contract.*) that runs OFFLINE and exits 0 exactly when the goal behavior works.
  - Pin every dependency with a lockfile so resolution is reproducible.
  - No secrets, credentials, or tokens. No real URLs (only example.com or localhost). No absolute paths.
  - No personal names, emails, company names, or project identifiers of any kind.
  - No binaries and no generated output (node_modules, dist, target, venv, .git, .env).
  - Keep it under 200 files and 256KB packed.
csx.json
{"case":{"caseId":"case:sha256:82cf096b71e8a1a74e2da426ebd4a2c1e6c5b96ccfda3828ad8b676223d356e4","contract":["ObjectSchema instantiates with key definitions and checks key presence using hasKey","ObjectSchema validates objects against definitions and rejects unexpected keys, missing required keys, or invalid value types","ObjectSchema enforces dependency constraints between keys using requires","ObjectSchema supports custom validation functions and custom merge functions that retain existing values when returning undefined","ObjectSchema recursively validates nested objects defined by subschemas","ObjectSchema merges multiple objects using assign, overwrite, and replace strategies","ValidationStrategy and MergeStrategy provide static helper methods for type validation and merge logic"],"goal":"verify @eslint/object-schema.ObjectSchema in pkg:npm/%40eslint/object-schema@2.1.7","kind":"HOW","packages":["pkg:npm/%40eslint/object-schema@2.1.7"],"schemaVersion":1,"symbols":["@eslint/object-schema.ObjectSchema"]},"contractCommand":["node","test/contract.mjs"],"environment":{"arch":"x64","distro":"ubuntu","ecosystem":"npm","libc":"glibc","libcVersion":"2.39","os":"linux","osVersionBucket":"24","packageManager":"npm","schemaVersion":1},"license":"MIT-0","packages":["pkg:npm/%40eslint/object-schema@2.1.7"],"schemaVersion":1,"subject":"pkg:npm/%40eslint/object-schema@2.1.7","symbols":["@eslint/object-schema.ObjectSchema"],"verifierAdapter":"node-typescript@1"}
index.mjs
import { ObjectSchema, ValidationStrategy, MergeStrategy } from "@eslint/object-schema";

export function createConfigSchema(definitions) {
  return new ObjectSchema(definitions);
}

export { ObjectSchema, ValidationStrategy, MergeStrategy };
package-lock.json
{
  "name": "sample-eslint-object-schema",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "sample-eslint-object-schema",
      "version": "1.0.0",
      "dependencies": {
        "@eslint/object-schema": "2.1.7"
      }
    },
    "node_modules/@eslint/object-schema": {
      "version": "2.1.7",
      "resolved": "https://registry.npmjs.org/@eslint/object-schema/-/object-schema-2.1.7.tgz",
      "integrity": "sha512-VtAOaymWVfZcmZbp6E2mympDIHvyjXs/12LqWYjVw6qjrfF+VK+fyG33kChz3nnK+SU5/NeHOqrTEHS8sXO3OA==",
      "license": "Apache-2.0",
      "engines": {
        "node": "^18.18.0 || ^20.9.0 || >=21.1.0"
      }
    }
  }
}
package.json
{
  "name": "sample-eslint-object-schema",
  "version": "1.0.0",
  "type": "module",
  "private": true,
  "scripts": {
    "test": "node test/contract.mjs"
  },
  "dependencies": {
    "@eslint/object-schema": "2.1.7"
  }
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify @eslint/object-schema.ObjectSchema in pkg:npm/%40eslint/object-schema@2.1.7",
  "kind": "HOW",
  "packages": [
    "pkg:npm/%40eslint/object-schema@2.1.7"
  ],
  "symbols": [
    "@eslint/object-schema.ObjectSchema"
  ]
}
test/contract.mjs
import assert from "node:assert/strict";
import { ObjectSchema, ValidationStrategy, MergeStrategy, createConfigSchema } from "../index.mjs";

// 1. ObjectSchema instantiates with key definitions and checks key presence using hasKey
const schema = createConfigSchema({
  id: {
    required: true,
    merge: "replace",
    validate: "number"
  },
  name: {
    required: true,
    merge: "overwrite",
    validate: "string!"
  },
  tag: {
    required: false,
    merge: "overwrite",
    validate: "string"
  },
  settings: {
    required: false,
    merge: "assign",
    validate: "object"
  },
  items: {
    required: false,
    merge: "overwrite",
    validate: "array"
  },
  metadata: {
    required: false,
    merge: "replace",
    validate: "object?"
  },
  enabled: {
    required: false,
    merge: "overwrite",
    validate: "boolean"
  },
  token: {
    required: false,
    requires: ["id"],
    merge: "replace",
    validate: "string"
  },
  customField: {
    required: false,
    merge(first, second) {
      if (second === "retain") return undefined;
      return [first, second].filter(Boolean).join(":");
    },
    validate(val) {
      if (typeof val !== "string" || (!val.startsWith("val_") && val !== "retain")) {
        throw new TypeError("Must start with val_ or be retain");
      }
    }
  },
  nestedConfig: {
    required: false,
    merge: "assign",
    validate: "object",
    schema: {
      subKey: {
        required: false,
        merge: "replace",
        validate: "string"
      }
    }
  }
});

assert.equal(schema.hasKey("id"), true);
assert.equal(schema.hasKey("name"), true);
assert.equal(schema.hasKey("nonexistent"), false);

// 2. ObjectSchema validates objects against definitions and rejects unexpected keys, missing required keys, or invalid value types
assert.doesNotThrow(() => {
  schema.validate({
    id: 1,
    name: "main-item",
    tag: "alpha",
    settings: { enabled: true },
    items: [1, 2, 3],
    metadata: null,
    enabled: true,
    token: "tok_123",
    customField: "val_one",
    nestedConfig: { subKey: "inner" }
  });
});

assert.throws(() => {
  schema.validate({
    id: 1
  });
}, /Missing required key/i);

assert.throws(() => {
  schema.validate({
    id: 1,
    name: "main-item",
    unexpected: true
  });
}, /Unexpected key/i);

assert.throws(() => {
  schema.validate({
    id: "not-a-number",
    name: "main-item"
  });
}, /Expected a number/i);

assert.throws(() => {
  schema.validate({
    id: 1,
    name: ""
  });
}, /Expected a non-empty string/i);

// 3. ObjectSchema enforces dependency constraints between keys using requires
assert.throws(() => {
  schema.validate({
    name: "main-item",
    token: "tok_123"
  });
}, /requires keys/i);

// 4. ObjectSchema supports custom validation functions and custom merge functions that retain existing values when returning undefined
assert.throws(() => {
  schema.validate({
    id: 1,
    name: "main-item",
    customField: "bad_val"
  });
}, /Must start with val_/);

const initialObj = { id: 1, name: "item1", customField: "val_a" };
const retainObj = { id: 2, name: "item2", customField: "retain" };
const mergeRetained = schema.merge(initialObj, retainObj);
assert.equal(mergeRetained.customField, "val_a");

// 5. ObjectSchema recursively validates nested objects defined by subschemas
assert.throws(() => {
  schema.validate({
    id: 1,
    name: "main-item",
    nestedConfig: { subKey: 999 }
  });
}, /Expected a string/i);

// 6. ObjectSchema merges multiple objects using assign, overwrite, and replace strategies
const objA = {
  id: 100,
  name: "initial-name",
  tag: "v1",
  settings: { timeout: 5000 },
  customField: "val_first",
  nestedConfig: { subKey: "v1-inner" }
};

const objB = {
  id: 200,
  name: "updated-name",
  settings: { retries: 3 },
  items: ["elem1"],
  customField: "val_second",
  nestedConfig: { subKey: "v2-inner" }
};

const merged = schema.merge(objA, objB);
assert.equal(merged.id, 200);
assert.equal(merged.name, "updated-name");
assert.equal(merged.tag, "v1");
assert.deepEqual(merged.settings, { timeout: 5000, retries: 3 });
assert.deepEqual(merged.items, ["elem1"]);
assert.equal(merged.customField, "val_first:val_second");
assert.deepEqual(merged.nestedConfig, { subKey: "v2-inner" });

// 7. ValidationStrategy and MergeStrategy provide static helper methods for type validation and merge logic
assert.doesNotThrow(() => ValidationStrategy.array([]));
assert.doesNotThrow(() => ValidationStrategy.boolean(true));
assert.doesNotThrow(() => ValidationStrategy.number(3.14));
assert.doesNotThrow(() => ValidationStrategy.object({ key: "val" }));
assert.doesNotThrow(() => ValidationStrategy["object?"](null));
assert.doesNotThrow(() => ValidationStrategy["object?"]({}));
assert.doesNotThrow(() => ValidationStrategy.string("hello"));
assert.doesNotThrow(() => ValidationStrategy["string!"]("non-empty"));

assert.throws(() => ValidationStrategy.array("string"), TypeError);
assert.throws(() => ValidationStrategy.boolean(0), TypeError);
assert.throws(() => ValidationStrategy.number("42"), TypeError);
assert.throws(() => ValidationStrategy.object(null), TypeError);
assert.throws(() => ValidationStrategy.string(100), TypeError);
assert.throws(() => ValidationStrategy["string!"](""), TypeError);

assert.equal(MergeStrategy.overwrite("a", "b"), "b");
assert.equal(MergeStrategy.replace("a", undefined), "a");
assert.equal(MergeStrategy.replace("a", "b"), "b");
assert.deepEqual(MergeStrategy.assign({ a: 1 }, { b: 2 }), { a: 1, b: 2 });

console.log("Contract verified successfully.");

原始种子者

匿名