json-machete 0.11.2 → 0.12.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +19 -11
- package/index.mjs +19 -11
- package/package.json +3 -3
package/index.js
CHANGED
|
@@ -388,6 +388,7 @@ const JSONSchemaStringFormats = [
|
|
|
388
388
|
'ipv4',
|
|
389
389
|
'ipv6',
|
|
390
390
|
'uri',
|
|
391
|
+
'uuid',
|
|
391
392
|
];
|
|
392
393
|
const AnySchema = {
|
|
393
394
|
title: 'Any',
|
|
@@ -527,7 +528,9 @@ async function healJSONSchema(schema, { noDeduplication = false, logger = new ut
|
|
|
527
528
|
if (typeof subSchema.additionalProperties === 'object') {
|
|
528
529
|
delete subSchema.additionalProperties.readOnly;
|
|
529
530
|
delete subSchema.additionalProperties.writeOnly;
|
|
530
|
-
|
|
531
|
+
const additionalPropertiesKeys = Object.keys(subSchema.additionalProperties);
|
|
532
|
+
if (additionalPropertiesKeys.length === 0 ||
|
|
533
|
+
(additionalPropertiesKeys.length === 1 && subSchema.additionalProperties.type === 'string')) {
|
|
531
534
|
logger.debug(`${path} has an empty additionalProperties object. So this is invalid. Replacing it with 'true'`);
|
|
532
535
|
subSchema.additionalProperties = true;
|
|
533
536
|
}
|
|
@@ -536,19 +539,19 @@ async function healJSONSchema(schema, { noDeduplication = false, logger = new ut
|
|
|
536
539
|
logger.debug(`${path} has an "allOf" definition with only one element. Removing it.`);
|
|
537
540
|
const realSubschema = subSchema.allOf[0];
|
|
538
541
|
delete subSchema.allOf;
|
|
539
|
-
|
|
542
|
+
subSchema = realSubschema;
|
|
540
543
|
}
|
|
541
544
|
if (subSchema.anyOf != null && subSchema.anyOf.length === 1 && !subSchema.properties) {
|
|
542
545
|
logger.debug(`${path} has an "anyOf" definition with only one element. Removing it.`);
|
|
543
546
|
const realSubschema = subSchema.anyOf[0];
|
|
544
547
|
delete subSchema.anyOf;
|
|
545
|
-
|
|
548
|
+
subSchema = realSubschema;
|
|
546
549
|
}
|
|
547
550
|
if (subSchema.oneOf != null && subSchema.oneOf.length === 1 && !subSchema.properties) {
|
|
548
551
|
logger.debug(`${path} has an "oneOf" definition with only one element. Removing it.`);
|
|
549
552
|
const realSubschema = subSchema.oneOf[0];
|
|
550
553
|
delete subSchema.oneOf;
|
|
551
|
-
|
|
554
|
+
subSchema = realSubschema;
|
|
552
555
|
}
|
|
553
556
|
if (subSchema.description != null) {
|
|
554
557
|
subSchema.description = subSchema.description.trim();
|
|
@@ -563,7 +566,7 @@ async function healJSONSchema(schema, { noDeduplication = false, logger = new ut
|
|
|
563
566
|
logger.debug(`${path} has an object definition but with "items" which is not valid. So setting "items" to the actual definition.`);
|
|
564
567
|
const realSubschema = subSchema.items;
|
|
565
568
|
delete subSchema.items;
|
|
566
|
-
|
|
569
|
+
subSchema = realSubschema;
|
|
567
570
|
}
|
|
568
571
|
if (subSchema.properties && subSchema.type !== 'object') {
|
|
569
572
|
logger.debug(`${path} has "properties" with no type defined. Adding a type property with "object" value.`);
|
|
@@ -652,8 +655,10 @@ async function healJSONSchema(schema, { noDeduplication = false, logger = new ut
|
|
|
652
655
|
}
|
|
653
656
|
}
|
|
654
657
|
// If it is an object type but no properties given while example is available
|
|
655
|
-
if (((subSchema.type === 'object' && !subSchema.properties) || !subSchema.type) &&
|
|
656
|
-
|
|
658
|
+
if (((subSchema.type === 'object' && !subSchema.properties) || !subSchema.type) &&
|
|
659
|
+
(subSchema.example || subSchema.examples)) {
|
|
660
|
+
const examples = asArray(subSchema.examples || subSchema.example || []);
|
|
661
|
+
const generatedSchema = toJsonSchema(examples[0], {
|
|
657
662
|
required: false,
|
|
658
663
|
objects: {
|
|
659
664
|
additionalProperties: false,
|
|
@@ -664,11 +669,14 @@ async function healJSONSchema(schema, { noDeduplication = false, logger = new ut
|
|
|
664
669
|
arrays: {
|
|
665
670
|
mode: 'first',
|
|
666
671
|
},
|
|
672
|
+
postProcessFnc(type, schema, value, defaultFunc) {
|
|
673
|
+
if (schema.type === 'object' && !schema.properties && Object.keys(value).length === 0) {
|
|
674
|
+
return AnySchema;
|
|
675
|
+
}
|
|
676
|
+
return defaultFunc(type, schema, value);
|
|
677
|
+
},
|
|
667
678
|
});
|
|
668
|
-
const healedGeneratedSchema =
|
|
669
|
-
noDeduplication,
|
|
670
|
-
logger: logger.child('toJsonSchema'),
|
|
671
|
-
});
|
|
679
|
+
const healedGeneratedSchema = generatedSchema;
|
|
672
680
|
subSchema.type = asArray(healedGeneratedSchema.type)[0];
|
|
673
681
|
subSchema.properties = healedGeneratedSchema.properties;
|
|
674
682
|
// If type for properties is already given, use it
|
package/index.mjs
CHANGED
|
@@ -382,6 +382,7 @@ const JSONSchemaStringFormats = [
|
|
|
382
382
|
'ipv4',
|
|
383
383
|
'ipv6',
|
|
384
384
|
'uri',
|
|
385
|
+
'uuid',
|
|
385
386
|
];
|
|
386
387
|
const AnySchema = {
|
|
387
388
|
title: 'Any',
|
|
@@ -521,7 +522,9 @@ async function healJSONSchema(schema, { noDeduplication = false, logger = new De
|
|
|
521
522
|
if (typeof subSchema.additionalProperties === 'object') {
|
|
522
523
|
delete subSchema.additionalProperties.readOnly;
|
|
523
524
|
delete subSchema.additionalProperties.writeOnly;
|
|
524
|
-
|
|
525
|
+
const additionalPropertiesKeys = Object.keys(subSchema.additionalProperties);
|
|
526
|
+
if (additionalPropertiesKeys.length === 0 ||
|
|
527
|
+
(additionalPropertiesKeys.length === 1 && subSchema.additionalProperties.type === 'string')) {
|
|
525
528
|
logger.debug(`${path} has an empty additionalProperties object. So this is invalid. Replacing it with 'true'`);
|
|
526
529
|
subSchema.additionalProperties = true;
|
|
527
530
|
}
|
|
@@ -530,19 +533,19 @@ async function healJSONSchema(schema, { noDeduplication = false, logger = new De
|
|
|
530
533
|
logger.debug(`${path} has an "allOf" definition with only one element. Removing it.`);
|
|
531
534
|
const realSubschema = subSchema.allOf[0];
|
|
532
535
|
delete subSchema.allOf;
|
|
533
|
-
|
|
536
|
+
subSchema = realSubschema;
|
|
534
537
|
}
|
|
535
538
|
if (subSchema.anyOf != null && subSchema.anyOf.length === 1 && !subSchema.properties) {
|
|
536
539
|
logger.debug(`${path} has an "anyOf" definition with only one element. Removing it.`);
|
|
537
540
|
const realSubschema = subSchema.anyOf[0];
|
|
538
541
|
delete subSchema.anyOf;
|
|
539
|
-
|
|
542
|
+
subSchema = realSubschema;
|
|
540
543
|
}
|
|
541
544
|
if (subSchema.oneOf != null && subSchema.oneOf.length === 1 && !subSchema.properties) {
|
|
542
545
|
logger.debug(`${path} has an "oneOf" definition with only one element. Removing it.`);
|
|
543
546
|
const realSubschema = subSchema.oneOf[0];
|
|
544
547
|
delete subSchema.oneOf;
|
|
545
|
-
|
|
548
|
+
subSchema = realSubschema;
|
|
546
549
|
}
|
|
547
550
|
if (subSchema.description != null) {
|
|
548
551
|
subSchema.description = subSchema.description.trim();
|
|
@@ -557,7 +560,7 @@ async function healJSONSchema(schema, { noDeduplication = false, logger = new De
|
|
|
557
560
|
logger.debug(`${path} has an object definition but with "items" which is not valid. So setting "items" to the actual definition.`);
|
|
558
561
|
const realSubschema = subSchema.items;
|
|
559
562
|
delete subSchema.items;
|
|
560
|
-
|
|
563
|
+
subSchema = realSubschema;
|
|
561
564
|
}
|
|
562
565
|
if (subSchema.properties && subSchema.type !== 'object') {
|
|
563
566
|
logger.debug(`${path} has "properties" with no type defined. Adding a type property with "object" value.`);
|
|
@@ -646,8 +649,10 @@ async function healJSONSchema(schema, { noDeduplication = false, logger = new De
|
|
|
646
649
|
}
|
|
647
650
|
}
|
|
648
651
|
// If it is an object type but no properties given while example is available
|
|
649
|
-
if (((subSchema.type === 'object' && !subSchema.properties) || !subSchema.type) &&
|
|
650
|
-
|
|
652
|
+
if (((subSchema.type === 'object' && !subSchema.properties) || !subSchema.type) &&
|
|
653
|
+
(subSchema.example || subSchema.examples)) {
|
|
654
|
+
const examples = asArray(subSchema.examples || subSchema.example || []);
|
|
655
|
+
const generatedSchema = toJsonSchema(examples[0], {
|
|
651
656
|
required: false,
|
|
652
657
|
objects: {
|
|
653
658
|
additionalProperties: false,
|
|
@@ -658,11 +663,14 @@ async function healJSONSchema(schema, { noDeduplication = false, logger = new De
|
|
|
658
663
|
arrays: {
|
|
659
664
|
mode: 'first',
|
|
660
665
|
},
|
|
666
|
+
postProcessFnc(type, schema, value, defaultFunc) {
|
|
667
|
+
if (schema.type === 'object' && !schema.properties && Object.keys(value).length === 0) {
|
|
668
|
+
return AnySchema;
|
|
669
|
+
}
|
|
670
|
+
return defaultFunc(type, schema, value);
|
|
671
|
+
},
|
|
661
672
|
});
|
|
662
|
-
const healedGeneratedSchema =
|
|
663
|
-
noDeduplication,
|
|
664
|
-
logger: logger.child('toJsonSchema'),
|
|
665
|
-
});
|
|
673
|
+
const healedGeneratedSchema = generatedSchema;
|
|
666
674
|
subSchema.type = asArray(healedGeneratedSchema.type)[0];
|
|
667
675
|
subSchema.properties = healedGeneratedSchema.properties;
|
|
668
676
|
// If type for properties is already given, use it
|
package/package.json
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "json-machete",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.12.0",
|
|
4
4
|
"sideEffects": false,
|
|
5
5
|
"dependencies": {
|
|
6
6
|
"@graphql-mesh/cross-helpers": "0.2.2",
|
|
7
|
-
"@graphql-mesh/types": "0.
|
|
8
|
-
"@graphql-mesh/utils": "0.
|
|
7
|
+
"@graphql-mesh/types": "0.79.0",
|
|
8
|
+
"@graphql-mesh/utils": "0.38.0",
|
|
9
9
|
"@graphql-tools/utils": "8.10.0",
|
|
10
10
|
"@json-schema-tools/meta-schema": "1.6.19",
|
|
11
11
|
"@whatwg-node/fetch": "^0.2.7",
|