js-yaml 5 config files validated with Zod 4: working versions (tested 2026-10-15)
Parse YAML app config with js-yaml, validate it with Zod 4 (including env defaults), and reject unknown keys or bad types with field errors.
passed3 passed · 0 failed2026-10-15 16:01 UTC- Status
- passed
- Last passed
- 2026-10-15 16:01 UTC
- Tests
- 3 / 3
- Language
- TypeScript
- Runtime
- node v22.23.3
npm install --no-audit --no-fund --ignore-scripts=false npm test
node v22.23.3 linux 6.17.0-1022-azure x64 runner: github-actions 38764699834
The code
Exactly the files that passed. Your agent can pull them with get_recipe("js-yaml-zod-config") (install), or see them on GitHub.
{
"name": "js-yaml-zod-config",
"private": true,
"type": "module",
"scripts": {
"test": "node --import tsx --test test/*.test.ts"
},
"dependencies": {
"js-yaml": "5.4.3",
"zod": "4.6.7"
},
"devDependencies": {
"@types/node": "26.6.5",
"tsx": "4.23.15",
"@types/js-yaml": "4.0.9"
}
}
import { load } from "js-yaml";
import { z } from "zod";
export const AppConfig = z.object({
name: z.string().min(1),
port: z.number().int().min(1).max(65535).default(3000),
features: z.object({
billing: z.boolean().default(false),
beta: z.boolean().default(false),
}).default({ billing: false, beta: false }),
database: z.object({
url: z.string().min(1),
pool: z.number().int().positive().default(5),
}),
});
export type AppConfig = z.infer<typeof AppConfig>;
export function loadConfig(source: string): AppConfig {
const raw = load(source);
return AppConfig.parse(raw);
}
export function safeLoadConfig(source: string) {
const raw = load(source);
const r = AppConfig.safeParse(raw);
if (!r.success) return { ok: false as const, issues: z.flattenError(r.error).fieldErrors };
return { ok: true as const, data: r.data };
}
import { test } from "node:test";
import assert from "node:assert/strict";
import { loadConfig, safeLoadConfig } from "../src/config.ts";
const good = `
name: provenpin
database:
url: "sqlite://:memory:"
features:
billing: true
`;
test("loads yaml with defaults", () => {
const cfg = loadConfig(good);
assert.equal(cfg.name, "provenpin");
assert.equal(cfg.port, 3000);
assert.equal(cfg.features.billing, true);
assert.equal(cfg.features.beta, false);
assert.equal(cfg.database.pool, 5);
});
test("rejects missing database", () => {
const r = safeLoadConfig("name: x\n");
assert.equal(r.ok, false);
if (!r.ok) assert.ok(r.issues.database);
});
test("rejects bad types", () => {
const r = safeLoadConfig("name: x\nport: nope\ndatabase:\n url: u\n");
assert.equal(r.ok, false);
});
Connect to ProvenPin
Add the free MCP server so your agent can pull this recipe and others like it. Same setup as the docs.
claude mcp add --transport http provenpin https://mcp.provenpin.com/mcp
{ "mcpServers": { "provenpin": { "url": "https://mcp.provenpin.com/mcp" } } }{ "servers": { "provenpin": { "type": "http", "url": "https://mcp.provenpin.com/mcp" } } }{ "mcpServers": { "provenpin": { "serverUrl": "https://mcp.provenpin.com/mcp" } } }{ "mcpServers": { "provenpin": { "type": "streamableHttp", "url": "https://mcp.provenpin.com/mcp" } } }Any other MCP client: Streamable HTTP at https://mcp.provenpin.com/mcp. No API key, no signup.
Related stacks
Anthropic TypeScript SDK tool use loop, tested with a mocked fetch
Run a Claude tool_use loop with the official Anthropic SDK and Zod-validated tool inputs, tested offline with an injected fetch.
better-sqlite3 transactions with Zod-validated inputs
A small ledger on better-sqlite3 using db.transaction for atomic transfers, nested transactions as savepoints for all-or-nothing batches, and Zod 4 to validate inputs and rows, tested on in-memory SQLite.
Cheerio HTML scraping with Zod-validated records and saved fixtures
Scrape a product listing and a spec table with Cheerio, normalise prices and relative URLs, and validate every record with Zod 4 so broken cards are reported, tested offline against saved HTML fixtures.
Commander CLI with Zod option validation, tested in-process
A Commander subcommand CLI with variadic arguments, repeatable options, choices and Zod-backed argument parsers, made testable with exitOverride and configureOutput so node:test runs it in-process without spawning.