Skip to content

Commit a8db0f0

Browse files
committed
fix: problem in the resolveProperty method
1 parent 097b0bf commit a8db0f0

File tree

2 files changed

+126
-54
lines changed

2 files changed

+126
-54
lines changed

src/jsonld-schema-resolver.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,9 +244,19 @@ export class JsonLdSchemaResolver {
244244
};
245245
}
246246
if (typeof value === "object" && value !== null) {
247+
// Check if this is a JSON-LD schema that needs resolving
247248
if (this.isJsonLdSchema(value)) {
248249
return this.resolveJsonLdSchema(value);
249250
}
251+
252+
// Check if this is already a valid JSON Schema definition
253+
// (has a "type" property or other JSON Schema keywords)
254+
if (this.isJsonSchema(value)) {
255+
// Preserve the original JSON Schema definition
256+
return value;
257+
}
258+
259+
// Otherwise treat as a generic object
250260
return {
251261
type: "object",
252262
additionalProperties: true,
@@ -256,6 +266,50 @@ export class JsonLdSchemaResolver {
256266
return { type: "string" };
257267
}
258268

269+
/**
270+
* Checks if an object is a valid JSON Schema definition
271+
*/
272+
private isJsonSchema(obj: any): boolean {
273+
if (!obj || typeof obj !== "object") return false;
274+
275+
// Check for common JSON Schema keywords
276+
const schemaKeywords = [
277+
"type",
278+
"properties",
279+
"items",
280+
"required",
281+
"enum",
282+
"const",
283+
"pattern",
284+
"format",
285+
"minimum",
286+
"maximum",
287+
"minLength",
288+
"maxLength",
289+
"minItems",
290+
"maxItems",
291+
"uniqueItems",
292+
"multipleOf",
293+
"minProperties",
294+
"maxProperties",
295+
"additionalProperties",
296+
"additionalItems",
297+
"allOf",
298+
"anyOf",
299+
"oneOf",
300+
"not",
301+
"$ref",
302+
"definitions",
303+
"$schema",
304+
"title",
305+
"description",
306+
"default",
307+
"examples",
308+
];
309+
310+
return Object.keys(obj).some((key) => schemaKeywords.includes(key));
311+
}
312+
259313
/**
260314
* Extracts JSON-LD entities from a schema
261315
*/

tests/spec/jsonld-basic/__snapshots__/basic.test.ts.snap

Lines changed: 72 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,21 @@ exports[`JSON-LD basic test > should generate JSON-LD types correctly 1`] = `
77
export interface Person {
88
/**
99
* name
10-
* Property: name
10+
* Person's name
1111
*/
12-
name: Record<string, any>;
12+
name: string;
1313
/**
1414
* email
15-
* Property: email
15+
* Person's email
16+
* @format email
1617
*/
17-
email?: Record<string, any>;
18+
email?: string;
1819
/**
1920
* birthDate
20-
* Property: birthDate
21+
* Person's birth date
22+
* @format date
2123
*/
22-
birthDate?: Record<string, any>;
24+
birthDate?: string;
2325
}
2426
2527
/** JSON-LD Entity: Organization */
@@ -28,17 +30,18 @@ export interface Organization {
2830
* name
2931
* Property: name
3032
*/
31-
name: Record<string, any>;
33+
name: string;
3234
/**
3335
* url
3436
* Property: url
37+
* @format uri
3538
*/
36-
url?: Record<string, any>;
39+
url?: string;
3740
/**
3841
* employees
3942
* Property: employees
4043
*/
41-
employees?: Record<string, any>;
44+
employees?: Person[];
4245
}
4346
4447
/** JSON-LD Entity: JsonLdEntity */
@@ -66,19 +69,21 @@ export interface JsonLdEntity {
6669
export interface Person extends JsonLdEntity {
6770
/**
6871
* name
69-
* Property: name
72+
* Person's name
7073
*/
71-
name: Record<string, any>;
74+
name: string;
7275
/**
7376
* email
74-
* Property: email
77+
* Person's email
78+
* @format email
7579
*/
76-
email?: Record<string, any>;
80+
email?: string;
7781
/**
7882
* birthDate
79-
* Property: birthDate
83+
* Person's birth date
84+
* @format date
8085
*/
81-
birthDate?: Record<string, any>;
86+
birthDate?: string;
8287
}
8388
8489
/**
@@ -89,17 +94,18 @@ export interface Organization extends JsonLdEntity {
8994
* name
9095
* Property: name
9196
*/
92-
name: Record<string, any>;
97+
name: string;
9398
/**
9499
* url
95100
* Property: url
101+
* @format uri
96102
*/
97-
url?: Record<string, any>;
103+
url?: string;
98104
/**
99105
* employees
100106
* Property: employees
101107
*/
102-
employees?: Record<string, any>;
108+
employees?: Person[];
103109
}
104110
105111
/**
@@ -217,19 +223,21 @@ exports[`JSON-LD basic test > should generate JSON-LD types with strict typing 1
217223
export interface Person {
218224
/**
219225
* name
220-
* Property: name
226+
* Person's name
221227
*/
222-
name: Record<string, any>;
228+
name: string;
223229
/**
224230
* email
225-
* Property: email
231+
* Person's email
232+
* @format email
226233
*/
227-
email?: Record<string, any>;
234+
email?: string;
228235
/**
229236
* birthDate
230-
* Property: birthDate
237+
* Person's birth date
238+
* @format date
231239
*/
232-
birthDate?: Record<string, any>;
240+
birthDate?: string;
233241
}
234242
235243
/** JSON-LD Entity: Organization */
@@ -238,17 +246,18 @@ export interface Organization {
238246
* name
239247
* Property: name
240248
*/
241-
name: Record<string, any>;
249+
name: string;
242250
/**
243251
* url
244252
* Property: url
253+
* @format uri
245254
*/
246-
url?: Record<string, any>;
255+
url?: string;
247256
/**
248257
* employees
249258
* Property: employees
250259
*/
251-
employees?: Record<string, any>;
260+
employees?: Person[];
252261
}
253262
254263
/** JSON-LD Entity: JsonLdEntity */
@@ -276,19 +285,21 @@ export interface JsonLdEntity {
276285
export interface Person extends JsonLdEntity {
277286
/**
278287
* name
279-
* Property: name
288+
* Person's name
280289
*/
281-
name: Record<string, any>;
290+
name: string;
282291
/**
283292
* email
284-
* Property: email
293+
* Person's email
294+
* @format email
285295
*/
286-
email?: Record<string, any>;
296+
email?: string;
287297
/**
288298
* birthDate
289-
* Property: birthDate
299+
* Person's birth date
300+
* @format date
290301
*/
291-
birthDate?: Record<string, any>;
302+
birthDate?: string;
292303
}
293304
294305
/**
@@ -299,17 +310,18 @@ export interface Organization extends JsonLdEntity {
299310
* name
300311
* Property: name
301312
*/
302-
name: Record<string, any>;
313+
name: string;
303314
/**
304315
* url
305316
* Property: url
317+
* @format uri
306318
*/
307-
url?: Record<string, any>;
319+
url?: string;
308320
/**
309321
* employees
310322
* Property: employees
311323
*/
312-
employees?: Record<string, any>;
324+
employees?: Person[];
313325
}
314326
315327
/**
@@ -427,19 +439,21 @@ exports[`JSON-LD basic test > should handle JSON-LD entities with context 1`] =
427439
export interface Person {
428440
/**
429441
* name
430-
* Property: name
442+
* Person's name
431443
*/
432-
name: Record<string, any>;
444+
name: string;
433445
/**
434446
* email
435-
* Property: email
447+
* Person's email
448+
* @format email
436449
*/
437-
email?: Record<string, any>;
450+
email?: string;
438451
/**
439452
* birthDate
440-
* Property: birthDate
453+
* Person's birth date
454+
* @format date
441455
*/
442-
birthDate?: Record<string, any>;
456+
birthDate?: string;
443457
}
444458
445459
/** JSON-LD Entity: Organization */
@@ -448,17 +462,18 @@ export interface Organization {
448462
* name
449463
* Property: name
450464
*/
451-
name: Record<string, any>;
465+
name: string;
452466
/**
453467
* url
454468
* Property: url
469+
* @format uri
455470
*/
456-
url?: Record<string, any>;
471+
url?: string;
457472
/**
458473
* employees
459474
* Property: employees
460475
*/
461-
employees?: Record<string, any>;
476+
employees?: Person[];
462477
}
463478
464479
/** JSON-LD Entity: JsonLdEntity */
@@ -486,19 +501,21 @@ export interface JsonLdEntity {
486501
export interface Person extends JsonLdEntity {
487502
/**
488503
* name
489-
* Property: name
504+
* Person's name
490505
*/
491-
name: Record<string, any>;
506+
name: string;
492507
/**
493508
* email
494-
* Property: email
509+
* Person's email
510+
* @format email
495511
*/
496-
email?: Record<string, any>;
512+
email?: string;
497513
/**
498514
* birthDate
499-
* Property: birthDate
515+
* Person's birth date
516+
* @format date
500517
*/
501-
birthDate?: Record<string, any>;
518+
birthDate?: string;
502519
}
503520
504521
/**
@@ -509,17 +526,18 @@ export interface Organization extends JsonLdEntity {
509526
* name
510527
* Property: name
511528
*/
512-
name: Record<string, any>;
529+
name: string;
513530
/**
514531
* url
515532
* Property: url
533+
* @format uri
516534
*/
517-
url?: Record<string, any>;
535+
url?: string;
518536
/**
519537
* employees
520538
* Property: employees
521539
*/
522-
employees?: Record<string, any>;
540+
employees?: Person[];
523541
}
524542
525543
/**

0 commit comments

Comments
 (0)