CodeSampleX

Sample

js-yaml 4.1.0: Type

Verified sample for npm js-yaml 4.1.0: Type. The contract ran on node 22 · linux alpine/x64 · docker and passed: yaml.Type defines custom scalar, sequence…

sha256:ea99d3e910bbf120abfaa21282eac9c4211165b73db1d613f1e68fe19aee04f9

This network offers one thing: a sample that builds. It ran the sample in a sandbox and kept the signed receipt. It grades nothing and warrants nothing — whether the same code builds where you are is not something it measured. How many distinct signing keys filed a passing contract receipt. One is the author alone; more than one means somebody else built it too. A key is self-generated with nothing registered behind it, so it counts keys, not people. MIT-0

Execution evidence

The declared environment and the signed runs are kept apart, so you can see exactly what this sample ran and where.

Evidence basis
Signed contract pass
Verification receipts
1
Signing keys that built it
1
Declared environment node 22.23 linux 24 · ubuntu · glibc 2.39 x64 node 22.23 javascript npm 10

Verification-run environments

Environment Contract Stages Run
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-28

Case

HOW
Goal
verify js-yaml.Type in pkg:npm/js-yaml@4.1.0
Packages
Symbols
  • js-yaml.Type
Environment
node 22.23.2
Created
2026-08-28T03:34:19Z

Contract

  1. yaml.Type defines custom scalar, sequence, and mapping YAML tags with construct and represent handlers
  2. yaml.DEFAULT_SCHEMA.extend([customType]) enables parsing custom YAML tags into JavaScript objects and serializing them back
  3. yaml.Type resolve predicate rejects invalid tag representations with YAMLException

Files

  • PROMPT.md
  • README.md
  • csx.json
  • index.js
  • package-lock.json
  • package.json
  • spec.json
  • test/contract.cjs

Download the source artifact (tar.gz)

Source

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 js-yaml.Type in pkg:npm/js-yaml@4.1.0
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:npm/js-yaml@4.1.0
Demonstrate these symbols/APIs:
  - js-yaml.Type
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.
README.md
# js-yaml Type Custom Tag Verification

Demonstrates defining custom YAML data types (scalar, sequence, mapping) using `js-yaml.Type` in `js-yaml@4.1.0`.

## Contract
- Creates custom tags (`!hex`, `!point`, `!person`) using `new yaml.Type(...)`
- Extends schema using `yaml.DEFAULT_SCHEMA.extend(...)`
- Verifies offline loading, dumping, roundtrip consistency, and error handling for invalid tags.
csx.json
{"case":{"caseId":"case:sha256:f142220e24e271795534c657780c9801daa8c74aa8897947cf8e2df876317480","constraints":{"executionContext":"node"},"contract":["yaml.Type defines custom scalar, sequence, and mapping YAML tags with construct and represent handlers","yaml.DEFAULT_SCHEMA.extend([customType]) enables parsing custom YAML tags into JavaScript objects and serializing them back","yaml.Type resolve predicate rejects invalid tag representations with YAMLException"],"goal":"verify js-yaml.Type in pkg:npm/js-yaml@4.1.0","kind":"HOW","packages":["pkg:npm/js-yaml@4.1.0"],"schemaVersion":1,"symbols":["js-yaml.Type"]},"contractCommand":["node","test/contract.cjs"],"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/js-yaml@4.1.0"],"schemaVersion":1,"subject":"pkg:npm/js-yaml@4.1.0","symbols":["js-yaml.Type"],"verifierAdapter":"node-typescript@1"}
index.js
const yaml = require('js-yaml');

class HexColor {
  constructor(value) {
    this.value = value;
  }
}

const HexColorType = new yaml.Type('!hex', {
  kind: 'scalar',
  resolve(data) {
    return typeof data === 'string' && /^0x[0-9a-fA-F]+$/i.test(data);
  },
  construct(data) {
    return new HexColor(parseInt(data, 16));
  },
  instanceOf: HexColor,
  represent(hex) {
    return '0x' + hex.value.toString(16).toUpperCase();
  }
});

class Point2D {
  constructor(x, y) {
    this.x = x;
    this.y = y;
  }
}

const Point2DType = new yaml.Type('!point', {
  kind: 'sequence',
  resolve(data) {
    return Array.isArray(data) && data.length === 2 && typeof data[0] === 'number' && typeof data[1] === 'number';
  },
  construct(data) {
    return new Point2D(data[0], data[1]);
  },
  instanceOf: Point2D,
  represent(point) {
    return [point.x, point.y];
  }
});

class Person {
  constructor(name, age) {
    this.name = name;
    this.age = age;
  }
}

const PersonType = new yaml.Type('!person', {
  kind: 'mapping',
  resolve(data) {
    return data !== null && typeof data === 'object' && typeof data.name === 'string' && typeof data.age === 'number';
  },
  construct(data) {
    return new Person(data.name, data.age);
  },
  instanceOf: Person,
  represent(person) {
    return { name: person.name, age: person.age };
  }
});

const CustomSchema = yaml.DEFAULT_SCHEMA.extend([HexColorType, Point2DType, PersonType]);

function parseCustomYaml(text) {
  return yaml.load(text, { schema: CustomSchema });
}

function dumpCustomYaml(object) {
  return yaml.dump(object, { schema: CustomSchema });
}

module.exports = {
  HexColor,
  HexColorType,
  Point2D,
  Point2DType,
  Person,
  PersonType,
  CustomSchema,
  parseCustomYaml,
  dumpCustomYaml
};
package-lock.json
{
  "name": "sample-js-yaml-type",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "sample-js-yaml-type",
      "version": "1.0.0",
      "dependencies": {
        "js-yaml": "4.1.0"
      }
    },
    "node_modules/argparse": {
      "version": "2.0.1",
      "resolved": "https://registry.npmjs.org/argparse/-/argparse-2.0.1.tgz",
      "integrity": "sha512-8+9WqebbFzpX9OR+Wa6O29asIogeRMzcGtAINdpMHHyAg10f05aSFVBbcEqGf/PXw1EjAZ+q2/bEBg3DvurK3Q==",
      "license": "Python-2.0"
    },
    "node_modules/js-yaml": {
      "version": "4.1.0",
      "resolved": "https://registry.npmjs.org/js-yaml/-/js-yaml-4.1.0.tgz",
      "integrity": "sha512-wpxZs9NoxZaJESJGIZTyDEaYpl0FKSA+FB9aJiyemKhMwkxQg63h4T1KJgUGHpTqPDNRcmmYLugrRjJlBtWvRA==",
      "license": "MIT",
      "dependencies": {
        "argparse": "^2.0.1"
      },
      "bin": {
        "js-yaml": "bin/js-yaml.js"
      }
    }
  }
}
package.json
{
  "name": "sample-js-yaml-type",
  "version": "1.0.0",
  "private": true,
  "type": "commonjs",
  "dependencies": {
    "js-yaml": "4.1.0"
  }
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify js-yaml.Type in pkg:npm/js-yaml@4.1.0",
  "kind": "HOW",
  "packages": [
    "pkg:npm/js-yaml@4.1.0"
  ],
  "symbols": [
    "js-yaml.Type"
  ],
  "constraints": {
    "executionContext": "node"
  },
  "runtimeConditions": {
    "ecosystem": "npm",
    "language": "javascript",
    "moduleSystem": "cjs",
    "packageManager": "npm@10.9.8",
    "runtime": "node@22.23.2"
  }
}
test/contract.cjs
const assert = require('assert');
const yaml = require('js-yaml');
const {
  HexColor,
  HexColorType,
  Point2D,
  Point2DType,
  Person,
  PersonType,
  CustomSchema,
  parseCustomYaml,
  dumpCustomYaml
} = require('../index.js');

// 1. Verify yaml.Type instances have correct tag and kind properties
assert.strictEqual(HexColorType instanceof yaml.Type, true, 'HexColorType should be an instance of yaml.Type');
assert.strictEqual(HexColorType.tag, '!hex', 'HexColorType tag should match !hex');
assert.strictEqual(HexColorType.kind, 'scalar', 'HexColorType kind should be scalar');

assert.strictEqual(Point2DType instanceof yaml.Type, true, 'Point2DType should be an instance of yaml.Type');
assert.strictEqual(Point2DType.tag, '!point', 'Point2DType tag should match !point');
assert.strictEqual(Point2DType.kind, 'sequence', 'Point2DType kind should be sequence');

assert.strictEqual(PersonType instanceof yaml.Type, true, 'PersonType should be an instance of yaml.Type');
assert.strictEqual(PersonType.tag, '!person', 'PersonType tag should match !person');
assert.strictEqual(PersonType.kind, 'mapping', 'PersonType kind should be mapping');

// 2. Verify YAML deserialization using custom schema
const yamlText = [
  'color: !hex 0xABC123',
  'coords: !point [100, 200]',
  'profile: !person',
  '  name: Sample User',
  '  age: 32'
].join('\n');

const parsed = parseCustomYaml(yamlText);
assert.ok(parsed.color instanceof HexColor, 'parsed color should be HexColor');
assert.strictEqual(parsed.color.value, 0xABC123, 'parsed color value should match');

assert.ok(parsed.coords instanceof Point2D, 'parsed coords should be Point2D');
assert.strictEqual(parsed.coords.x, 100, 'parsed x coord should match');
assert.strictEqual(parsed.coords.y, 200, 'parsed y coord should match');

assert.ok(parsed.profile instanceof Person, 'parsed profile should be Person');
assert.strictEqual(parsed.profile.name, 'Sample User', 'parsed profile name should match');
assert.strictEqual(parsed.profile.age, 32, 'parsed profile age should match');

// 3. Verify YAML serialization using custom schema
const dumped = dumpCustomYaml(parsed);
assert.ok(dumped.includes('!hex'), 'dumped output should include !hex tag');
assert.ok(dumped.includes('!point'), 'dumped output should include !point tag');
assert.ok(dumped.includes('!person'), 'dumped output should include !person tag');

// 4. Verify roundtrip deserialization
const roundtrip = parseCustomYaml(dumped);
assert.strictEqual(roundtrip.color.value, parsed.color.value, 'roundtrip color value should match');
assert.strictEqual(roundtrip.coords.x, parsed.coords.x, 'roundtrip x coord should match');
assert.strictEqual(roundtrip.coords.y, parsed.coords.y, 'roundtrip y coord should match');
assert.strictEqual(roundtrip.profile.name, parsed.profile.name, 'roundtrip profile name should match');
assert.strictEqual(roundtrip.profile.age, parsed.profile.age, 'roundtrip profile age should match');

// 5. Verify resolve validation throws on invalid input
assert.throws(
  () => {
    parseCustomYaml('badColor: !hex not-a-hex');
  },
  (err) => err instanceof yaml.YAMLException,
  'Invalid hex format should fail resolution and throw YAMLException'
);

assert.throws(
  () => {
    parseCustomYaml('badCoords: !point [10, "twenty"]');
  },
  (err) => err instanceof yaml.YAMLException,
  'Invalid sequence items should fail resolution and throw YAMLException'
);

assert.throws(
  () => {
    parseCustomYaml('badPerson: !person invalid-mapping');
  },
  (err) => err instanceof yaml.YAMLException,
  'Invalid mapping format should fail resolution and throw YAMLException'
);

console.log('All contract assertions passed offline.');
process.exit(0);

Origin Seeder

anonymous