fast-xml-parser with Zod validation: working versions (tested 2026-10-15)
Parse XML payloads with fast-xml-parser then validate the object shape with Zod, covering attributes, arrays and builder roundtrips offline.
passed2 passed · 0 failed2026-10-15 15:59 UTC- Status
- passed
- Last passed
- 2026-10-15 15:59 UTC
- Tests
- 2 / 2
- 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("fast-xml-parser-zod") (install), or see them on GitHub.
{
"name": "fast-xml-parser-zod",
"private": true,
"type": "module",
"scripts": {
"test": "node --import tsx --test test/*.test.ts"
},
"dependencies": {"fast-xml-parser":"5.11.2","zod":"4.6.7"},
"devDependencies": {"@types/node":"26.6.5","tsx":"4.23.15"}
}
import { XMLParser, XMLBuilder } from "fast-xml-parser";
import { z } from "zod";
const ItemSchema = z.object({
id: z.coerce.string(),
name: z.string(),
qty: z.coerce.number(),
});
const CatalogSchema = z.object({
catalog: z.object({
item: z.array(ItemSchema).min(1),
}),
});
const parser = new XMLParser({
ignoreAttributes: false,
attributeNamePrefix: "",
isArray: (name) => name === "item",
});
const builder = new XMLBuilder({
ignoreAttributes: false,
attributeNamePrefix: "",
});
export function parseCatalog(xml: string) {
const raw = parser.parse(xml);
return CatalogSchema.parse(raw).catalog.item;
}
export function buildCatalog(items: z.infer<typeof ItemSchema>[]) {
return builder.build({ catalog: { item: items } });
}
import { test } from "node:test";
import assert from "node:assert/strict";
import { parseCatalog, buildCatalog } from "../src/xml.ts";
test("parses and validates catalog xml", () => {
const xml = `<catalog>
<item id="1" name="Bolt" qty="10"></item>
<item id="2" name="Nut" qty="4"></item>
</catalog>`;
// attributes come as strings; coerce via rebuild path using element children style
const itemsXml = `<catalog>
<item><id>1</id><name>Bolt</name><qty>10</qty></item>
<item><id>2</id><name>Nut</name><qty>4</qty></item>
</catalog>`;
const items = parseCatalog(itemsXml);
assert.equal(items.length, 2);
assert.equal(items[0].name, "Bolt");
assert.equal(items[0].qty, 10);
});
test("builder roundtrip", () => {
const xml = buildCatalog([{ id: "9", name: "Washer", qty: 2 }]);
const items = parseCatalog(xml);
assert.deepEqual(items, [{ id: "9", name: "Washer", qty: 2 }]);
});
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.