CodeSampleX

サンプル

@sinclair/typebox 0.27.8

検証済みサンプル — npm @sinclair/typebox 0.27.8. node 22 · linux alpine/x64 · docker で contract を実行し、成功しました: Value.Create constructs schema default values matching…

sha256:973d51be0473ae5fea1a1fef7916124d7035d9ea38486983e71f28daab32b1cb

このネットワークが提供するのは一つだけです。ビルドされるサンプル。サンドボックスで実行し、署名済みの受領証を保管します。等級はつけず、何も保証しません — 同じコードがあなたの環境でビルドされるかは測定していません。 合格した契約受領証を提出した異なる署名鍵の数です。1 なら作者だけ、2 以上なら他の誰かもビルドしています。鍵は自己生成で背後に登録された身元がないため、数えているのは人ではなく鍵です。 MIT-0

実行証拠

宣言された環境と署名済みの実行を分けてあります。このサンプルが何をどこで実行したかをそのまま確認できます。

証拠の基準
署名済みコントラクト合格
検証レシート
1
ビルドした署名鍵
1
宣言された環境 node 22.23 linux 24 · ubuntu · glibc 2.39 x64 node 22.23 javascript npm 10

検証実行環境

環境 コントラクト ステージ 実行日
node 22 · linux alpine/x64 · docker ed25519:c1973797be207ac4 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · node-typescript@1node:22-alpine@sha256:c610fcdfb1d5…
2026-08-27

ケース

HOW
ゴール
verify pkg:npm/%40sinclair/typebox@0.27.8
パッケージ
環境
node 22.23.2
作成日
2026-08-27T14:51:38Z

コントラクト

  1. Value.Create constructs schema default values matching structural schema definitions
  2. Value.Cast coerces and fills missing default fields on partial input objects
  3. Value.Check validates values against schema and returns boolean status
  4. Value.Clone creates deep clones and Value.Equal compares values by structural equality
  5. Value.Diff computes structural JSON patch differences and Value.Patch updates targets
  6. Value.Errors identifies specific validation errors with precise JSON paths and error messages

ファイル

  • PROMPT.md
  • csx.json
  • package-lock.json
  • package.json
  • spec.json
  • src/index.js
  • 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 pkg:npm/%40sinclair/typebox@0.27.8
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:npm/%40sinclair/typebox@0.27.8
Constraints:
  - executionContext: node
Required runtime conditions:
  - ecosystem: npm
  - language: javascript
  - moduleSystem: cjs
  - packageManager: npm@10.9.8
  - runtime: node@22.23.2

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:40c4f3f571cc199cc2491ce96f08cc4083f3bbb0d488cc8a61b02e7914fa31ea","constraints":{"executionContext":"node"},"contract":["Value.Create constructs schema default values matching structural schema definitions","Value.Cast coerces and fills missing default fields on partial input objects","Value.Check validates values against schema and returns boolean status","Value.Clone creates deep clones and Value.Equal compares values by structural equality","Value.Diff computes structural JSON patch differences and Value.Patch updates targets","Value.Errors identifies specific validation errors with precise JSON paths and error messages"],"goal":"verify pkg:npm/%40sinclair/typebox@0.27.8","kind":"HOW","packages":["pkg:npm/%40sinclair/typebox@0.27.8"],"schemaVersion":1},"contractCommand":["node","test/contract.mjs"],"environment":{"arch":"x64","distro":"ubuntu","ecosystem":"npm","executionContext":"node","language":"javascript","libc":"glibc","libcVersion":"2.39","moduleSystem":"cjs","os":"linux","osVersionBucket":"24","packageManager":"npm","packageManagerVersion":"10.9.8","runtime":"node","runtimeVersion":"22.23.2","schemaVersion":1},"license":"MIT-0","packages":["pkg:npm/%40sinclair/typebox@0.27.8"],"schemaVersion":1,"subject":"pkg:npm/%40sinclair/typebox@0.27.8","verifierAdapter":"node-typescript@1"}
package-lock.json
{
  "name": "sample-typebox",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "sample-typebox",
      "version": "1.0.0",
      "license": "MIT-0",
      "dependencies": {
        "@sinclair/typebox": "0.27.8"
      }
    },
    "node_modules/@sinclair/typebox": {
      "version": "0.27.8",
      "resolved": "https://registry.npmjs.org/@sinclair/typebox/-/typebox-0.27.8.tgz",
      "integrity": "sha512-+Fj43pSMwJs4KRrH/938Uf+uAELIgVBmQzg/q1YG10djyfA3TnrU8N8XzqCh/okZdszqBQTZf96idMfE5lnwTA==",
      "license": "MIT"
    }
  }
}
package.json
{
  "name": "sample-typebox",
  "version": "1.0.0",
  "private": true,
  "type": "commonjs",
  "license": "MIT-0",
  "dependencies": {
    "@sinclair/typebox": "0.27.8"
  }
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify pkg:npm/%40sinclair/typebox@0.27.8",
  "kind": "HOW",
  "packages": [
    "pkg:npm/%40sinclair/typebox@0.27.8"
  ],
  "constraints": {
    "executionContext": "node"
  },
  "runtimeConditions": {
    "ecosystem": "npm",
    "language": "javascript",
    "moduleSystem": "cjs",
    "packageManager": "npm@10.9.8",
    "runtime": "node@22.23.2"
  }
}
src/index.js
const { Type } = require('@sinclair/typebox');
const { Value } = require('@sinclair/typebox/value');

/**
 * Creates a product configuration schema with various types, defaults, and constraints.
 */
const ProductConfigSchema = Type.Object({
  sku: Type.String({ minLength: 3, maxLength: 20 }),
  name: Type.String({ default: 'Unnamed Product' }),
  price: Type.Number({ minimum: 0, default: 0 }),
  inStock: Type.Boolean({ default: true }),
  tags: Type.Array(Type.String(), { default: [] }),
  metadata: Type.Optional(Type.Record(Type.String(), Type.String()))
}, { additionalProperties: false });

/**
 * Generate default value for a schema.
 * @param {import('@sinclair/typebox').TSchema} schema
 */
function createDefault(schema) {
  return Value.Create(schema);
}

/**
 * Casts a value to match schema, filling missing defaults and sanitizing structure.
 * @param {import('@sinclair/typebox').TSchema} schema
 * @param {unknown} value
 */
function castValue(schema, value) {
  return Value.Cast(schema, value);
}

/**
 * Validates a value against schema directly.
 * @param {import('@sinclair/typebox').TSchema} schema
 * @param {unknown} value
 */
function checkValue(schema, value) {
  return Value.Check(schema, value);
}

/**
 * Clones a value deeply.
 * @param {unknown} value
 */
function cloneValue(value) {
  return Value.Clone(value);
}

/**
 * Checks equality between two values.
 * @param {unknown} a
 * @param {unknown} b
 */
function areEqual(a, b) {
  return Value.Equal(a, b);
}

/**
 * Computes structural differences between two values.
 * @param {unknown} current
 * @param {unknown} next
 */
function computeDiff(current, next) {
  return Value.Diff(current, next);
}

/**
 * Applies diff patches to a base value.
 * @param {unknown} base
 * @param {Array<{ type: string, path: string, value?: unknown }>} patch
 */
function applyPatch(base, patch) {
  return Value.Patch(base, patch);
}

/**
 * Collects all validation errors for a value against schema.
 * @param {import('@sinclair/typebox').TSchema} schema
 * @param {unknown} value
 */
function collectErrors(schema, value) {
  const errors = [];
  for (const err of Value.Errors(schema, value)) {
    errors.push({
      path: err.path,
      message: err.message,
      value: err.value
    });
  }
  return errors;
}

module.exports = {
  Type,
  Value,
  ProductConfigSchema,
  createDefault,
  castValue,
  checkValue,
  cloneValue,
  areEqual,
  computeDiff,
  applyPatch,
  collectErrors
};
test/contract.mjs
import assert from 'node:assert/strict';
import { createRequire } from 'node:module';

const require = createRequire(import.meta.url);
const {
  Type,
  Value,
  ProductConfigSchema,
  createDefault,
  castValue,
  checkValue,
  cloneValue,
  areEqual,
  computeDiff,
  applyPatch,
  collectErrors
} = require('../src/index.js');

// 1. Value.Create constructs schema default values matching structural schema definitions
const defaultValue = createDefault(ProductConfigSchema);
assert.equal(defaultValue.name, 'Unnamed Product', 'Default name should be applied');
assert.equal(defaultValue.price, 0, 'Default price should be 0');
assert.equal(defaultValue.inStock, true, 'Default inStock should be true');
assert.deepEqual(defaultValue.tags, [], 'Default tags should be empty array');

// 2. Value.Cast coerces and fills missing default fields on partial input objects
const partialInput = { sku: 'PROD-001' };
const casted = castValue(ProductConfigSchema, partialInput);
assert.equal(casted.sku, 'PROD-001', 'sku should be preserved');
assert.equal(casted.name, 'Unnamed Product', 'name should be filled from default');
assert.equal(casted.price, 0, 'price should be filled from default');
assert.equal(casted.inStock, true, 'inStock should be filled from default');
assert.deepEqual(casted.tags, [], 'tags should be filled from default');

// 3. Value.Check validates values against schema and returns boolean status
assert.equal(checkValue(ProductConfigSchema, casted), true, 'Casted object should be valid');
assert.equal(checkValue(ProductConfigSchema, { sku: 'OK', price: -5 }), false, 'Negative price should be invalid');
assert.equal(checkValue(ProductConfigSchema, { sku: 123 }), false, 'Non-string sku should be invalid');

// 4. Value.Clone creates deep clones and Value.Equal compares values by structural equality
const original = { sku: 'ITEM-99', name: 'Widget', price: 29.99, inStock: true, tags: ['hardware', 'gadget'] };
const cloned = cloneValue(original);
assert.notEqual(cloned, original, 'Clone should be a new object reference');
assert.notEqual(cloned.tags, original.tags, 'Nested array should be a new array reference');
assert.equal(areEqual(original, cloned), true, 'Original and clone should be deeply equal');
cloned.price = 39.99;
assert.equal(areEqual(original, cloned), false, 'Modified clone should not be equal');

// 5. Value.Diff computes structural JSON patch differences and Value.Patch updates targets
const stateA = { sku: 'ITEM-1', name: 'Original', price: 10, inStock: true, tags: ['a'] };
const stateB = { sku: 'ITEM-1', name: 'Updated', price: 15, inStock: false, tags: ['a', 'b'] };
const diffOps = computeDiff(stateA, stateB);
assert.ok(Array.isArray(diffOps), 'Diff should return an array of operations');
assert.ok(diffOps.length > 0, 'Diff should contain patch operations');

const patched = applyPatch(stateA, diffOps);
assert.deepEqual(patched, stateB, 'Patching stateA with diffOps should produce stateB');

// 6. Value.Errors identifies specific validation errors with precise JSON paths and error messages
const invalidPayload = {
  sku: 'X',
  price: -10,
  tags: [100],
  extra: 'bad'
};
const errors = collectErrors(ProductConfigSchema, invalidPayload);
assert.ok(errors.length >= 3, 'Should report at least 3 validation errors');
assert.ok(errors.some(e => e.path === '/sku'), 'Should report error for /sku');
assert.ok(errors.some(e => e.path === '/price'), 'Should report error for /price');
assert.ok(errors.some(e => e.path.startsWith('/tags')), 'Should report error for /tags');

console.log('Contract tests passed successfully.');

オリジンシーダー

匿名