CodeSampleX

Exemple

zod 4.1.12: z

Échantillon vérifié pour npm zod 4.1.12: z. Le contrat s'est exécuté sur node 22 · linux alpine/x64 · docker et a réussi : z.object validates structured…

sha256:8b25af574b39d2d2e49ad18ef57ac04ea78980ff32a57adcbfa4f72cbfa64716

Ce réseau offre une seule chose : un échantillon qui compile. Il l'a exécuté dans un bac à sable et conservé le reçu signé. Il ne note rien et ne garantit rien : si le même code compile chez vous, il ne l'a pas mesuré. Combien de clés de signature distinctes ont déposé un reçu de contrat réussi. Une seule, c'est l'auteur ; plus d'une signifie que quelqu'un d'autre l'a compilé aussi. Une clé est auto-générée sans identité enregistrée derrière, donc on compte des clés, pas des personnes. MIT-0

Preuves d'exécution

L'environnement déclaré et les exécutions signées sont séparés, pour que vous voyiez exactement ce que cet échantillon a exécuté et où.

Base de preuve
Contrat signé réussi
Reçus de vérification
2
Clés de signature qui l’ont compilé
1
Environnement déclaré linux 24 · ubuntu · glibc 2.39 x64 npm

Environnements des exécutions de vérification

Environnement Contrat Étapes Exécution
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-23
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-07

Cas

HOW
Objectif
verify zod.z in pkg:npm/zod@4.1.12
Paquets
Symboles
  • zod.z
Créé
2026-08-23T05:38:36Z

Contrat

  1. z.object validates structured schema with nested primitive validators and defaults
  2. z.string validates constraints including email and min length
  3. z.number validates integer and non-negative constraints
  4. z.enum validates allowed string literal choices and defaults
  5. z.coerce converts string input to typed numbers and booleans
  6. z.array validates lists of elements
  7. safeParse returns structured validation issues on invalid payload

Fichiers

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

Télécharger l’artefact source (tar.gz)

Code 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 zod.z in pkg:npm/zod@4.1.12
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:npm/zod@4.1.12
Demonstrate these symbols/APIs:
  - zod.z

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:9a770d701dbe0d4606a127538f05f507b6ba6e039254d1c022ed0cca3489b505","contract":["z.object validates structured schema with nested primitive validators and defaults","z.string validates constraints including email and min length","z.number validates integer and non-negative constraints","z.enum validates allowed string literal choices and defaults","z.coerce converts string input to typed numbers and booleans","z.array validates lists of elements","safeParse returns structured validation issues on invalid payload"],"goal":"verify zod.z in pkg:npm/zod@4.1.12","kind":"HOW","packages":["pkg:npm/zod@4.1.12"],"schemaVersion":1,"symbols":["zod.z"]},"contractCommand":["node","test/contract.js"],"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/zod@4.1.12"],"schemaVersion":1,"subject":"pkg:npm/zod@4.1.12","symbols":["zod.z"],"verifierAdapter":"node-typescript@1"}
index.js
const { z } = require('zod');

// Schema for user profiles demonstrating primitives, object, enum, defaults, and constraints
const userSchema = z.object({
  id: z.string().min(1),
  name: z.string().min(1),
  age: z.number().int().nonnegative(),
  email: z.string().email(),
  role: z.enum(['admin', 'user', 'guest']).default('user'),
  isActive: z.boolean().default(true),
  tags: z.array(z.string()).default([]),
});

// Schema demonstrating coercion and range bounds
const configSchema = z.object({
  port: z.coerce.number().int().min(1).max(65535),
  host: z.string().min(1),
  debug: z.coerce.boolean().default(false),
});

function parseUser(input) {
  return userSchema.parse(input);
}

function safeParseUser(input) {
  return userSchema.safeParse(input);
}

function parseConfig(input) {
  return configSchema.parse(input);
}

function safeParseConfig(input) {
  return configSchema.safeParse(input);
}

module.exports = {
  z,
  userSchema,
  configSchema,
  parseUser,
  safeParseUser,
  parseConfig,
  safeParseConfig,
};
package-lock.json
{
  "name": "sample-zod",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "sample-zod",
      "version": "1.0.0",
      "license": "MIT-0",
      "dependencies": {
        "zod": "4.1.12"
      }
    },
    "node_modules/zod": {
      "version": "4.1.12",
      "resolved": "https://registry.npmjs.org/zod/-/zod-4.1.12.tgz",
      "integrity": "sha512-JInaHOamG8pt5+Ey8kGmdcAcg3OL9reK8ltczgHTAwNhMys/6ThXHityHxVV2p3fkw/c+MAvBHFVYHFZDmjMCQ==",
      "license": "MIT",
      "funding": {
        "url": "https://github.com/sponsors/colinhacks"
      }
    }
  }
}
package.json
{
  "name": "sample-zod",
  "version": "1.0.0",
  "private": true,
  "description": "Clean-room verification sample for zod.z in zod",
  "main": "index.js",
  "scripts": {
    "test": "node test/contract.js"
  },
  "license": "MIT-0",
  "dependencies": {
    "zod": "4.1.12"
  }
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify zod.z in pkg:npm/zod@4.1.12",
  "kind": "HOW",
  "packages": [
    "pkg:npm/zod@4.1.12"
  ],
  "symbols": [
    "zod.z"
  ]
}
test/contract.js
const assert = require('assert');
const {
  z,
  userSchema,
  configSchema,
  parseUser,
  safeParseUser,
  parseConfig,
  safeParseConfig,
} = require('../index.js');

function runTests() {
  // 1. z.object validates structured schema with nested primitive validators and defaults
  {
    const input = {
      id: 'usr_123',
      name: 'Alice',
      age: 28,
      email: 'alice@example.com',
    };
    const user = parseUser(input);
    assert.strictEqual(user.id, 'usr_123');
    assert.strictEqual(user.name, 'Alice');
    assert.strictEqual(user.age, 28);
    assert.strictEqual(user.email, 'alice@example.com');
    assert.strictEqual(user.role, 'user', 'applies default role');
    assert.strictEqual(user.isActive, true, 'applies default isActive');
    assert.deepStrictEqual(user.tags, [], 'applies default empty tags array');
  }

  // 2. z.string validates constraints including email and min length
  {
    const invalidEmail = safeParseUser({
      id: 'usr_123',
      name: 'Alice',
      age: 28,
      email: 'not-a-valid-email',
    });
    assert.strictEqual(invalidEmail.success, false);
    assert.ok(
      invalidEmail.error.issues.some((issue) => issue.path.includes('email')),
      'reports invalid email issue'
    );

    const emptyName = safeParseUser({
      id: 'usr_123',
      name: '',
      age: 28,
      email: 'alice@example.com',
    });
    assert.strictEqual(emptyName.success, false);
    assert.ok(
      emptyName.error.issues.some((issue) => issue.path.includes('name')),
      'reports empty name issue'
    );
  }

  // 3. z.number validates integer and non-negative constraints
  {
    const negativeAge = safeParseUser({
      id: 'usr_123',
      name: 'Alice',
      age: -1,
      email: 'alice@example.com',
    });
    assert.strictEqual(negativeAge.success, false);
    assert.ok(
      negativeAge.error.issues.some((issue) => issue.path.includes('age')),
      'reports negative age issue'
    );

    const floatAge = safeParseUser({
      id: 'usr_123',
      name: 'Alice',
      age: 28.5,
      email: 'alice@example.com',
    });
    assert.strictEqual(floatAge.success, false);
    assert.ok(
      floatAge.error.issues.some((issue) => issue.path.includes('age')),
      'reports non-integer age issue'
    );
  }

  // 4. z.enum validates allowed string literal choices and defaults
  {
    const validRole = parseUser({
      id: 'usr_456',
      name: 'Bob',
      age: 35,
      email: 'bob@example.com',
      role: 'admin',
    });
    assert.strictEqual(validRole.role, 'admin');

    const invalidRole = safeParseUser({
      id: 'usr_456',
      name: 'Bob',
      age: 35,
      email: 'bob@example.com',
      role: 'superadmin',
    });
    assert.strictEqual(invalidRole.success, false);
    assert.ok(
      invalidRole.error.issues.some((issue) => issue.path.includes('role')),
      'reports invalid enum value issue'
    );
  }

  // 5. z.coerce converts string input to typed numbers and booleans
  {
    const config = parseConfig({
      port: '8080',
      host: 'localhost',
      debug: 'true',
    });
    assert.strictEqual(config.port, 8080);
    assert.strictEqual(typeof config.port, 'number');
    assert.strictEqual(config.host, 'localhost');
    assert.strictEqual(config.debug, true);
    assert.strictEqual(typeof config.debug, 'boolean');

    const outOfRange = safeParseConfig({
      port: '99999',
      host: 'localhost',
    });
    assert.strictEqual(outOfRange.success, false);
    assert.ok(
      outOfRange.error.issues.some((issue) => issue.path.includes('port')),
      'reports port out of range issue'
    );
  }

  // 6. z.array validates lists of elements
  {
    const userWithTags = parseUser({
      id: 'usr_789',
      name: 'Charlie',
      age: 22,
      email: 'charlie@example.com',
      tags: ['developer', 'tester'],
    });
    assert.deepStrictEqual(userWithTags.tags, ['developer', 'tester']);

    const invalidTags = safeParseUser({
      id: 'usr_789',
      name: 'Charlie',
      age: 22,
      email: 'charlie@example.com',
      tags: ['valid', 123],
    });
    assert.strictEqual(invalidTags.success, false);
  }

  // 7. safeParse returns structured validation issues on invalid payload
  {
    const res = safeParseUser({});
    assert.strictEqual(res.success, false);
    assert.ok(Array.isArray(res.error.issues));
    assert.ok(res.error.issues.length >= 3, 'multiple missing field issues reported');
  }

  // 8. Direct access to zod.z exports
  {
    assert.strictEqual(typeof z.object, 'function');
    assert.strictEqual(typeof z.string, 'function');
    assert.strictEqual(typeof z.number, 'function');
    assert.strictEqual(typeof z.boolean, 'function');
    assert.strictEqual(typeof z.array, 'function');
    assert.strictEqual(typeof z.enum, 'function');
    assert.strictEqual(typeof z.coerce, 'object');
  }

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

runTests();

Seeder d'origine

anonyme