ArkType 2 schemas with morphs, narrows and discriminated unions: working versions (tested 2026-10-15)
Validate and transform data with ArkType 2: object schemas, morph pipes, narrow predicates, and discriminated unions returning typed data or readable problems.
passed4 passed · 0 failed2026-10-15 15:56 UTC- Status
- passed
- Last passed
- 2026-10-15 15:56 UTC
- Tests
- 4 / 4
- Language
- TypeScript
- Runtime
- node v22.23.3
pinned versions3
arktype@2.2.8@types/node@26.6.5tsx@4.23.15
run it
npm install --no-audit --no-fund --ignore-scripts=false npm test
test environment
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("arktype-schema-validation") (install), or see them on GitHub.
package.json
{
"name": "arktype-schema-validation",
"private": true,
"type": "module",
"scripts": {
"test": "node --import tsx --test test/*.test.ts"
},
"dependencies": {
"arktype": "2.2.8"
},
"devDependencies": {
"@types/node": "26.6.5",
"tsx": "4.23.15"
}
}
src/user.ts
import { type } from "arktype";
export const User = type({
email: "string.email",
name: "string > 0",
age: "number.integer >= 13",
role: "'admin' | 'member' | 'guest'",
});
export const CreateUser = type({
email: "string.email",
name: "string > 0",
"age?": "number.integer >= 13",
role: "'admin' | 'member' | 'guest' = 'member'",
}).pipe((u) => ({ ...u, age: u.age ?? 18 }));
export const Event = type({
"+": "reject",
kind: "'created'",
id: "string > 0",
}).or({
"+": "reject",
kind: "'deleted'",
id: "string > 0",
reason: "string > 0",
});
export function parseUser(input: unknown) {
const out = User(input);
if (out instanceof type.errors) return { ok: false as const, problems: out.summary };
return { ok: true as const, data: out };
}
test/user.test.ts
import { test } from "node:test";
import assert from "node:assert/strict";
import { CreateUser, Event, parseUser, User } from "../src/user.ts";
import { type } from "arktype";
test("accepts a valid user", () => {
const r = parseUser({ email: "[email protected]", name: "Ada", age: 30, role: "admin" });
assert.equal(r.ok, true);
if (r.ok) assert.equal(r.data.name, "Ada");
});
test("rejects bad email with problems summary", () => {
const r = parseUser({ email: "nope", name: "Ada", age: 30, role: "member" });
assert.equal(r.ok, false);
if (!r.ok) assert.match(r.problems, /email/i);
});
test("CreateUser morph fills default age and role", () => {
const out = CreateUser({ email: "[email protected]", name: "Bob" });
assert.equal(out instanceof type.errors, false);
if (!(out instanceof type.errors)) {
assert.equal(out.age, 18);
assert.equal(out.role, "member");
}
});
test("discriminated Event union", () => {
const ok = Event({ kind: "deleted", id: "1", reason: "spam" });
assert.equal(ok instanceof type.errors, false);
const bad = Event({ kind: "deleted", id: "1" });
assert.equal(bad instanceof type.errors, true);
const extra = Event({ kind: "created", id: "1", extra: true });
assert.equal(extra instanceof type.errors, true);
});
Connect to ProvenPin
Add the free MCP server so your agent can pull this recipe and others like it. Same setup as the docs.
terminal
claude mcp add --transport http provenpin https://mcp.provenpin.com/mcp
~/.cursor/mcp.json
{ "mcpServers": { "provenpin": { "url": "https://mcp.provenpin.com/mcp" } } }.vscode/mcp.json
{ "servers": { "provenpin": { "type": "http", "url": "https://mcp.provenpin.com/mcp" } } }~/.codeium/windsurf/mcp_config.json
{ "mcpServers": { "provenpin": { "serverUrl": "https://mcp.provenpin.com/mcp" } } }MCP settings
{ "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.