Registry / validation / json-schema-to-zod-v3

json-schema-to-zod-v3

JSON →
library1.0.4jsnpmunverified

A TypeScript library for converting JSON Schema (Draft 7+) to Zod v3 validation schemas. Current version 1.0.4, compatible with Zod ^3.0.0. Differentiators include built-in ReDoS protection for regex patterns, full type safety, zero runtime dependencies (only peer dependency on Zod), and coverage of all major JSON Schema features. The library uses a security configuration system with default protection against malicious patterns, and provides both high-level conversion and low-level utility functions.

npm install json-schema-to-zod-v3
INSTALL
IMPORT
SIG · JSON-SCHEMA-TO-ZOD
J
json-schema-to-zod-v3
validationjavascriptv1.0.4
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

convertJsonSchemaToZod
import { convertJsonSchemaToZod } from 'json-schema-to-zod-v3';
import convertJsonSchemaToZod from 'json-schema-to-zod-v3';
This is a named export, not a default export. Using default import will fail.
setSecurityConfig
import { setSecurityConfig } from 'json-schema-to-zod-v3';
Available as a named export; no default export for this function.
validatePattern
import { validatePattern } from 'json-schema-to-zod-v3';
Utility function for manual pattern validation; also a named export.
createSafeRegExp
import { createSafeRegExp } from 'json-schema-to-zod-v3';
Creates a RegExp with timeout; named export.

Converts a JSON Schema object to a Zod v3 schema and validates sample data, showing full type safety and usage of safeParse.

import { convertJsonSchemaToZod } from 'json-schema-to-zod-v3'; import { z } from 'zod'; const jsonSchema = { type: 'object', properties: { name: { type: 'string', minLength: 1 }, age: { type: 'number', minimum: 0, maximum: 120 }, email: { type: 'string', pattern: '^[^@]+@[^@]+\.[^@]+$' }, }, required: ['name', 'email'], }; const zodSchema = convertJsonSchemaToZod(jsonSchema); const result = zodSchema.safeParse({ name: 'John Doe', email: 'john@example.com', age: 30, }); console.log(result.success); // true
Debug
Known issues
gotchaThe library only supports JSON Schema Draft 7+. Earlier drafts may produce incorrect results.
fix
Ensure your JSON Schema conforms to Draft 7+.
affects: <1.0.0
deprecatedThe `setSecurityConfig` function modifies global state. Future versions may remove this in favor of per-call configuration.
fix
Track release notes for migration; consider wrapping calls in a configuration object.
affects: >=1.0.0
gotchaReDoS protection is enabled by default. Patterns exceeding 1000 characters or with nested quantifiers will cause conversion to fail without explicit configuration.
fix
Disable or customize protection using `setSecurityConfig({ enableProtection: false })` if you trust your input.
affects: >=1.0.0
gotchaThe `createSafeRegExp` function returns `null` for unsafe patterns, unlike standard RegExp constructor which throws. Code assuming it always returns a RegExp will break.
fix
Always check the return value: `const regex = createSafeRegExp(pattern); if (!regex) { /* handle unsafe */ }`
affects: >=1.0.0
breakingVersion 1.0.0 changed from default export to named exports. Code using `import J2Z from 'json-schema-to-zod-v3'` will break.
fix
Switch to named imports: `import { convertJsonSchemaToZod } from 'json-schema-to-zod-v3'`
affects: <1.0.0
Errors
Common errors & fixes
TypeError: convertJsonSchemaToZod is not a function
Attempted to import the default export, but the library only provides named exports.
fix
Use named import: `import { convertJsonSchemaToZod } from 'json-schema-to-zod-v3'`
Error: Pattern is unsafe: nested quantifier detected
The JSON Schema contains a regex pattern with nested quantifiers (e.g., `(a+)+`) that fails the built-in ReDoS protection.
fix
Either simplify the pattern or disable protection: `setSecurityConfig({ enableProtection: false })`
Error: Cannot find module 'zod'
The peer dependency `zod` is not installed.
fix
Install Zod: `npm install zod@^3.0.0`
TypeError: JSON Schema is not an object
Passed a non-object (e.g., string, number) as the JSON Schema input.
fix
Ensure the input is a valid JSON Schema object: `convertJsonSchemaToZod({ type: 'string' })`
Warning: Pattern exceeds max length...
Pattern length exceeds 1000 characters (default limit).
fix
Increase the limit: `setSecurityConfig({ maxPatternLength: 2000 })` or reduce pattern length.
Upgrade
Version history
1.0.4latest on npm
Audit
Dependencies
zodrequiredPeer dependency – generates Zod schemas; requires Zod v3 installed alongside.
Agent activity
27 hits · last 30 days
node
20
OpenAI (training)
2
Resources
json-schema-to-zod-v3 — npm install json-schema-to-zod-v3 · libregistry