json-machete 0.5.20 → 0.6.0-alpha-63f903dfd.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.
Files changed (3) hide show
  1. package/index.js +10 -0
  2. package/index.mjs +10 -0
  3. package/package.json +1 -1
package/index.js CHANGED
@@ -482,6 +482,7 @@ async function dereferenceObject(obj, { cwd = process.cwd(), externalFileCache =
482
482
  return obj;
483
483
  }
484
484
 
485
+ const asArray = (value) => (Array.isArray(value) ? value : [value]);
485
486
  const reservedTypeNames = ['Query', 'Mutation', 'Subscription'];
486
487
  function deduplicateJSONSchema(schema, seenMap = new Map()) {
487
488
  if (typeof schema === 'object' && schema != null) {
@@ -570,6 +571,15 @@ async function healJSONSchema(schema) {
570
571
  subSchema.type = 'array';
571
572
  }
572
573
  }
574
+ if (subSchema.type === 'string' && !subSchema.format && (subSchema.examples || subSchema.example)) {
575
+ const examples = asArray(subSchema.examples || subSchema.example || []);
576
+ if (examples === null || examples === void 0 ? void 0 : examples.length) {
577
+ const { format } = toJsonSchema(examples[0]);
578
+ if (format) {
579
+ subSchema.format = format;
580
+ }
581
+ }
582
+ }
573
583
  // If it is an object type but no properties given while example is available
574
584
  if (subSchema.type === 'object' && !subSchema.properties && subSchema.example) {
575
585
  const generatedSchema = toJsonSchema(subSchema.example, {
package/index.mjs CHANGED
@@ -477,6 +477,7 @@ async function dereferenceObject(obj, { cwd = process.cwd(), externalFileCache =
477
477
  return obj;
478
478
  }
479
479
 
480
+ const asArray = (value) => (Array.isArray(value) ? value : [value]);
480
481
  const reservedTypeNames = ['Query', 'Mutation', 'Subscription'];
481
482
  function deduplicateJSONSchema(schema, seenMap = new Map()) {
482
483
  if (typeof schema === 'object' && schema != null) {
@@ -565,6 +566,15 @@ async function healJSONSchema(schema) {
565
566
  subSchema.type = 'array';
566
567
  }
567
568
  }
569
+ if (subSchema.type === 'string' && !subSchema.format && (subSchema.examples || subSchema.example)) {
570
+ const examples = asArray(subSchema.examples || subSchema.example || []);
571
+ if (examples === null || examples === void 0 ? void 0 : examples.length) {
572
+ const { format } = toJsonSchema(examples[0]);
573
+ if (format) {
574
+ subSchema.format = format;
575
+ }
576
+ }
577
+ }
568
578
  // If it is an object type but no properties given while example is available
569
579
  if (subSchema.type === 'object' && !subSchema.properties && subSchema.example) {
570
580
  const generatedSchema = toJsonSchema(subSchema.example, {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "json-machete",
3
- "version": "0.5.20",
3
+ "version": "0.6.0-alpha-63f903dfd.0",
4
4
  "sideEffects": false,
5
5
  "dependencies": {
6
6
  "@graphql-mesh/utils": "0.30.2",