@@ -15,7 +15,7 @@ npm install zod-from-json-schema
1515## Zod 3 vs 4
1616
1717- If you need Zod 4, use the latest version of this package.
18- - If you need Zod 3, use the latest version that's less than 0.4.0 (at the of writing that's 0.0.5)
18+ - If you need Zod 3, use the latest version that's less than 0.4.0 (at the of writing that's 0.0.5). It supports a smaller subsets of JSON Schema.
1919
2020## Usage
2121
@@ -26,9 +26,9 @@ This package supports both ESM and CommonJS formats.
2626``` typescript
2727import { convertJsonSchemaToZod } from ' zod-from-json-schema' ;
2828
29- // Define a JSON Schema
29+ // Define a JSON Schema with advanced features
3030const jsonSchema = {
31- $schema: " http ://json-schema.org/draft-07 /schema# " ,
31+ $schema: " https ://json-schema.org/draft/2020-12 /schema" ,
3232 type: " object" ,
3333 properties: {
3434 name: { type: " string" , minLength: 2 , maxLength: 50 },
@@ -38,11 +38,24 @@ const jsonSchema = {
3838 type: " array" ,
3939 items: { type: " string" },
4040 uniqueItems: true ,
41- minItems: 1
42- }
41+ minItems: 1 ,
42+ maxItems: 10 ,
43+ contains: { enum: [" user" , " admin" , " guest" ] }
44+ },
45+ coordinates: {
46+ type: " array" ,
47+ prefixItems: [
48+ { type: " number" , minimum: - 90 , maximum: 90 }, // latitude
49+ { type: " number" , minimum: - 180 , maximum: 180 } // longitude
50+ ],
51+ items: false // No additional items allowed
52+ },
53+ score: { type: " number" , multipleOf: 0.5 , minimum: 0 , maximum: 100 }
4354 },
4455 required: [" name" , " email" ],
45- additionalProperties: false
56+ additionalProperties: false ,
57+ minProperties: 2 ,
58+ maxProperties: 10
4659};
4760
4861// Convert JSON Schema to Zod schema
5467 name: " John Doe" ,
55685669 age: 30 ,
57- tags: [" user" , " premium" ]
70+ tags: [" user" , " premium" , " admin" ], // Contains required "admin" role
71+ coordinates: [37.7749 , - 122.4194 ], // San Francisco lat/lng
72+ score: 87.5 // Multiple of 0.5
5873 });
5974 console .log (" Valid data:" , validData );
6075} catch (error ) {
@@ -69,14 +84,21 @@ const { convertJsonSchemaToZod } = require('zod-from-json-schema');
6984
7085// Define a JSON Schema
7186const jsonSchema = {
72- $schema: " http ://json-schema.org/draft-07 /schema# " ,
87+ $schema: " https ://json-schema.org/draft/2020-12 /schema" ,
7388 type: " object" ,
7489 properties: {
7590 name: { type: " string" , minLength: 2 , maxLength: 50 },
76- age: { type: " integer" , minimum: 0 , maximum: 120 }
91+ age: { type: " integer" , minimum: 0 , maximum: 120 },
92+ hobbies: {
93+ type: " array" ,
94+ items: { type: " string" },
95+ minItems: 1 ,
96+ maxItems: 5
97+ }
7798 },
7899 required: [" name" ],
79- additionalProperties: false
100+ additionalProperties: false ,
101+ minProperties: 1
80102};
81103
82104// Convert JSON Schema to Zod schema
@@ -86,7 +108,8 @@ const zodSchema = convertJsonSchemaToZod(jsonSchema);
86108try {
87109 const validData = zodSchema .parse ({
88110 name: " John Doe" ,
89- age: 30
111+ age: 30 ,
112+ hobbies: [" reading" , " coding" , " gaming" ]
90113 });
91114 console .log (" Valid data:" , validData);
92115} catch (error) {
@@ -143,49 +166,108 @@ const customSchema = z.object({
143166
144167## Supported JSON Schema Features
145168
146- This library supports the following JSON Schema features:
169+ This library provides comprehensive support for JSON Schema Draft 2020-12 features with ** 100% code coverage ** and extensive test validation against the official JSON Schema Test Suite.
147170
148171### Basic Types
149- - ` string `
150- - ` number `
151- - ` integer `
152- - ` boolean `
153- - ` null `
154- - ` object ` (with properties and required fields)
155- - ` array `
172+ - ` string ` - Basic string validation
173+ - ` number ` - Numeric values (including integers)
174+ - ` integer ` - Integer-only numeric values
175+ - ` boolean ` - Boolean true/false values
176+ - ` null ` - Null values
177+ - ` object ` - Object validation with property definitions
178+ - ` array ` - Array validation with item constraints
156179
157180### String Validations
158- - ` minLength `
159- - ` maxLength `
160- - ` pattern ` (regular expressions)
181+ - ` minLength ` - Minimum string length (Unicode grapheme-aware)
182+ - ` maxLength ` - Maximum string length (Unicode grapheme-aware)
183+ - ` pattern ` - Regular expression pattern matching
184+
185+ ** Unicode Support** : String length validation correctly counts Unicode grapheme clusters (user-perceived characters) rather than UTF-16 code units, ensuring proper validation of emoji and international text.
161186
162187### Number Validations
163- - ` minimum `
164- - ` maximum `
165- - ` exclusiveMinimum `
166- - ` exclusiveMaximum `
167- - ` multipleOf `
188+ - ` minimum ` - Minimum numeric value
189+ - ` maximum ` - Maximum numeric value
190+ - ` exclusiveMinimum ` - Exclusive minimum (greater than)
191+ - ` exclusiveMaximum ` - Exclusive maximum (less than)
192+ - ` multipleOf ` - Multiple validation with floating-point precision handling
168193
169194### Array Validations
170- - ` items `
171- - ` prefixItems `
172- - ` minItems `
173- - ` maxItems `
174- - ` uniqueItems `
195+ - ` items ` - Item schema validation (supports schemas, boolean values, and arrays)
196+ - ` prefixItems ` - Tuple-style positional item validation (Draft 2020-12)
197+ - ` minItems ` - Minimum array length
198+ - ` maxItems ` - Maximum array length
199+ - ` uniqueItems ` - Ensures all array items are unique
200+ - ` contains ` - Validates that array contains items matching a schema
201+ - ` minContains ` - Minimum number of items matching the contains schema
202+ - ` maxContains ` - Maximum number of items matching the contains schema
203+
204+ ** Advanced Array Features** :
205+ - Boolean ` items ` schemas (` items: false ` = empty arrays only, ` items: true ` = any items allowed)
206+ - Complex tuple validation with ` prefixItems ` and additional items control
207+ - Sophisticated contains validation with count constraints
175208
176209### Object Validations
177- - ` required ` (required properties)
178- - ` additionalProperties ` (controls passthrough behavior)
210+ - ` properties ` - Property schema definitions
211+ - ` required ` - Required property validation (supports special JavaScript property names)
212+ - ` additionalProperties ` - Controls whether additional properties are allowed
213+ - ` minProperties ` - Minimum number of object properties
214+ - ` maxProperties ` - Maximum number of object properties
215+
216+ ** Special Property Support** : Correctly handles JavaScript reserved property names like ` constructor ` , ` toString ` , and ` __proto__ ` .
179217
180218### Schema Composition
181- - ` const ` (literal values)
182- - ` enum ` (enumerated values)
183- - ` anyOf ` (union)
184- - ` allOf ` (intersection)
185- - ` oneOf ` (union)
186-
187- ### Additional
188- - ` description ` (carried over to Zod schemas)
219+ - ` const ` - Literal value constraints
220+ - ` enum ` - Enumerated value validation
221+ - ` anyOf ` - Union type validation (basic cases)
222+ - ` allOf ` - Intersection validation (basic cases)
223+ - ` oneOf ` - Exclusive union validation (exactly one schema must match)
224+ - ` not ` - Negation validation
225+
226+ ### Additional Features
227+ - ` title ` - Schema titles (carried over to Zod schemas)
228+ - ` description ` - Schema descriptions (carried over to Zod schemas)
229+ - Boolean schemas (` true ` = allow anything, ` false ` = allow nothing)
230+ - Implicit type detection from constraints
231+ - Comprehensive error messages
232+
233+ ## Currently Unsupported Features
234+
235+ The following JSON Schema features are ** not yet implemented** :
236+
237+ ### References and Definitions
238+ - ` $ref ` - JSON Pointer references (basic Zod v4 support exists but complex cases fail)
239+ - ` $defs ` / ` definitions ` - Schema definitions for reuse
240+ - Remote references (` $id ` resolution)
241+ - ` $dynamicRef ` / ` $dynamicAnchor ` - Dynamic references
242+
243+ ### Advanced Object Validation
244+ - ` patternProperties ` - Property validation based on regex patterns
245+ - ` additionalProperties ` - Fine-grained control over additional properties (basic support exists)
246+ - ` dependentSchemas ` - Schema dependencies based on property presence
247+ - ` dependentRequired ` - Required properties based on other property presence
248+ - ` propertyNames ` - Validation of property names themselves
249+ - ` unevaluatedProperties ` - Properties not covered by schema evaluation
250+
251+ ### Advanced Array Validation
252+ - ` unevaluatedItems ` - Items not covered by schema evaluation
253+ - Complex ` prefixItems ` scenarios with additional item control
254+
255+ ### Conditional Schemas
256+ - ` if ` / ` then ` / ` else ` - Conditional schema application
257+
258+ ### Meta-Schema Features
259+ - Custom vocabularies and meta-schema validation
260+ - Annotation collection and processing
261+
262+ ## Standards Compliance
263+
264+ - ** JSON Schema Draft 2020-12** - Partial support for core features of the latest JSON Schema standard
265+ - ** Official Test Suite** - Passes 1160+ tests from the official JSON Schema Test Suite (258 tests currently skipped for unsupported features)
266+ - ** 100% Code Coverage** - Complete test coverage for implemented features ensures reliability
267+ - ** Edge Case Handling** - Robust handling of supported validation scenarios including:
268+ - Unicode text validation with proper grapheme cluster counting
269+ - Basic object/array validation (complex ` $ref ` scenarios not supported)
270+ - JavaScript-specific property name handling (` constructor ` , ` toString ` , etc.)
189271
190272## License
191273
0 commit comments