@@ -43,7 +43,56 @@ export function inferSchema(obj: unknown): JSONSchema {
43
43
) ;
44
44
45
45
if ( allSameType && itemSchemas . length > 0 ) {
46
- // For consistent arrays, use the first item's schema
46
+ if ( asObjectSchema ( itemSchemas [ 0 ] ) . type === "object" ) {
47
+ // For arrays of objects, merge all properties
48
+ const properties : Record < string , JSONSchema > = { } ;
49
+ const requiredProps = new Set < string > ( ) ;
50
+ const seenProps = new Set < string > ( ) ;
51
+
52
+ for ( const schema of itemSchemas ) {
53
+ const objSchema = asObjectSchema ( schema ) ;
54
+ if ( ! objSchema . properties ) continue ;
55
+
56
+ // Track which properties we've seen
57
+ for ( const [ key , value ] of Object . entries ( objSchema . properties ) ) {
58
+ if ( ! seenProps . has ( key ) ) {
59
+ properties [ key ] = value ;
60
+ seenProps . add ( key ) ;
61
+ }
62
+ }
63
+
64
+ // Track which properties are in all objects
65
+ if ( objSchema . required ) {
66
+ if ( requiredProps . size === 0 ) {
67
+ for ( const prop of objSchema . required ) {
68
+ requiredProps . add ( prop ) ;
69
+ }
70
+ } else {
71
+ const newRequired = new Set < string > ( ) ;
72
+ for ( const prop of objSchema . required ) {
73
+ if ( requiredProps . has ( prop ) ) newRequired . add ( prop ) ;
74
+ }
75
+ requiredProps . clear ( ) ;
76
+ for ( const prop of newRequired ) {
77
+ requiredProps . add ( prop ) ;
78
+ }
79
+ }
80
+ }
81
+ }
82
+
83
+ return {
84
+ type : "array" ,
85
+ items : {
86
+ type : "object" ,
87
+ properties,
88
+ required :
89
+ requiredProps . size > 0 ? Array . from ( requiredProps ) : undefined ,
90
+ } ,
91
+ minItems : 0 ,
92
+ } ;
93
+ }
94
+
95
+ // For other consistent arrays, use the first item's schema
47
96
return {
48
97
type : "array" ,
49
98
items : itemSchemas [ 0 ] ,
@@ -53,15 +102,15 @@ export function inferSchema(obj: unknown): JSONSchema {
53
102
54
103
// For mixed type arrays, create a oneOf schema
55
104
if ( itemSchemas . length > 0 ) {
105
+ const uniqueSchemas = [
106
+ ...new Map ( itemSchemas . map ( ( s ) => [ JSON . stringify ( s ) , s ] ) ) . values ( ) ,
107
+ ] ;
56
108
return {
57
109
type : "array" ,
58
- items : {
59
- oneOf : [
60
- ...new Map (
61
- itemSchemas . map ( ( s ) => [ JSON . stringify ( s ) , s ] ) ,
62
- ) . values ( ) ,
63
- ] ,
64
- } ,
110
+ items :
111
+ uniqueSchemas . length === 1
112
+ ? uniqueSchemas [ 0 ]
113
+ : { oneOf : uniqueSchemas } ,
65
114
minItems : 0 ,
66
115
} ;
67
116
}
@@ -112,7 +161,7 @@ export function createSchemaFromJson(jsonObject: unknown): JSONSchema {
112
161
const inferredSchema = inferSchema ( jsonObject ) ;
113
162
114
163
return {
115
- $schema : "http ://json-schema.org/draft-07/schema# " ,
164
+ $schema : "https ://json-schema.org/draft-07/schema" ,
116
165
...asObjectSchema ( inferredSchema ) ,
117
166
title : "Generated Schema" ,
118
167
description : "Generated from JSON data" ,
0 commit comments