@tinacms/schema-tools 0.1.1 → 0.1.2

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/dist/index.es.js CHANGED
@@ -105,12 +105,14 @@ const NAMER = {
105
105
  return generateNamespacedFieldName(namespace, "ConnectionEdges");
106
106
  }
107
107
  };
108
- function hasDuplicates(array) {
109
- if (!array) {
110
- return false;
111
- } else {
112
- return new Set(array).size !== array.length;
113
- }
108
+ function findDuplicates(array = []) {
109
+ const duplicates = [
110
+ ...new Set(array.filter((item, index) => array.indexOf(item) !== index))
111
+ ].map((x) => `"${x}"`);
112
+ if (duplicates.length) {
113
+ return duplicates.join(", ");
114
+ } else
115
+ return void 0;
114
116
  }
115
117
  class TinaSchema {
116
118
  constructor(config) {
@@ -457,7 +459,7 @@ const nameProp = z.string({
457
459
  }).superRefine((val, ctx) => {
458
460
  if (val.includes(" "))
459
461
  ctx.addIssue({
460
- message: "name cannot contain spaces",
462
+ message: `name "${val}" cannot contain spaces`,
461
463
  code: z.ZodIssueCode.custom,
462
464
  fatal: true
463
465
  });
@@ -527,22 +529,37 @@ const TinaFieldZod = z.lazy(() => {
527
529
  start: z.string(),
528
530
  end: z.string()
529
531
  }).optional()
530
- }).refine((val) => {
531
- var _a;
532
- return !hasDuplicates((_a = val.fields) == null ? void 0 : _a.map((x) => x.name));
533
- }, {
534
- message: "Fields must have a unique name"
532
+ }).superRefine((val, ctx) => {
533
+ const dups = findDuplicates(val == null ? void 0 : val.fields.map((x) => x.name));
534
+ if (dups) {
535
+ ctx.addIssue({
536
+ code: z.ZodIssueCode.custom,
537
+ message: `Fields must have a unique name, duplicate field names: ${dups}`
538
+ });
539
+ }
535
540
  });
536
541
  const ObjectField = FieldWithList.extend({
537
542
  type: z.literal("object", {
538
543
  invalid_type_error: typeTypeError,
539
544
  required_error: typeRequiredError
540
545
  }),
541
- fields: z.array(TinaFieldZod).min(1).optional().refine((val) => !hasDuplicates(val == null ? void 0 : val.map((x) => x.name)), {
542
- message: "Fields must have a unique name"
546
+ fields: z.array(TinaFieldZod).min(1).optional().superRefine((val, ctx) => {
547
+ const dups = findDuplicates(val == null ? void 0 : val.map((x) => x.name));
548
+ if (dups) {
549
+ ctx.addIssue({
550
+ code: z.ZodIssueCode.custom,
551
+ message: `Fields must have a unique name, duplicate field names: ${dups}`
552
+ });
553
+ }
543
554
  }),
544
- templates: z.array(TemplateTemp).min(1).optional().refine((val) => !hasDuplicates(val == null ? void 0 : val.map((x) => x.name)), {
545
- message: "Templates must have a unique name"
555
+ templates: z.array(TemplateTemp).min(1).optional().superRefine((val, ctx) => {
556
+ const dups = findDuplicates(val == null ? void 0 : val.map((x) => x.name));
557
+ if (dups) {
558
+ ctx.addIssue({
559
+ code: z.ZodIssueCode.custom,
560
+ message: `Templates must have a unique name, duplicate template names: ${dups}`
561
+ });
562
+ }
546
563
  })
547
564
  });
548
565
  const RichTextField = FieldWithList.extend({
@@ -550,8 +567,14 @@ const TinaFieldZod = z.lazy(() => {
550
567
  invalid_type_error: typeTypeError,
551
568
  required_error: typeRequiredError
552
569
  }),
553
- templates: z.array(TemplateTemp).optional().refine((val) => !hasDuplicates(val == null ? void 0 : val.map((x) => x.name)), {
554
- message: "Templates must have a unique name"
570
+ templates: z.array(TemplateTemp).optional().superRefine((val, ctx) => {
571
+ const dups = findDuplicates(val == null ? void 0 : val.map((x) => x.name));
572
+ if (dups) {
573
+ ctx.addIssue({
574
+ code: z.ZodIssueCode.custom,
575
+ message: `Templates must have a unique name, duplicate template names: ${dups}`
576
+ });
577
+ }
555
578
  })
556
579
  });
557
580
  return z.discriminatedUnion("type", [
@@ -581,13 +604,17 @@ const TinaFieldZod = z.lazy(() => {
581
604
  if (val.list) {
582
605
  ctx.addIssue({
583
606
  code: z.ZodIssueCode.custom,
584
- message: "You can not have `list: true` when using `isTitle`"
607
+ message: `Can not have \`list: true\` when using \`isTitle\`. Error in value
608
+ ${JSON.stringify(val, null, 2)}
609
+ `
585
610
  });
586
611
  }
587
612
  if (!val.required) {
588
613
  ctx.addIssue({
589
614
  code: z.ZodIssueCode.custom,
590
- message: "You must have { required: true } when using `isTitle`"
615
+ message: `Must have { required: true } when using \`isTitle\` Error in value
616
+ ${JSON.stringify(val, null, 2)}
617
+ `
591
618
  });
592
619
  }
593
620
  }
@@ -638,11 +665,15 @@ const Template = z.object({
638
665
  }),
639
666
  name,
640
667
  fields: z.array(TinaFieldZod)
641
- }).refine((val) => {
668
+ }).superRefine((val, ctx) => {
642
669
  var _a;
643
- return !hasDuplicates((_a = val.fields) == null ? void 0 : _a.map((x) => x.name));
644
- }, {
645
- message: "Fields must have a unique name"
670
+ const dups = findDuplicates((_a = val.fields) == null ? void 0 : _a.map((x) => x.name));
671
+ if (dups) {
672
+ ctx.addIssue({
673
+ code: z.ZodIssueCode.custom,
674
+ message: `Fields must have a unique name, duplicate field names: ${dups}`
675
+ });
676
+ }
646
677
  });
647
678
  const TinaCloudCollectionBase = z.object({
648
679
  label: z.string().optional(),
@@ -650,16 +681,28 @@ const TinaCloudCollectionBase = z.object({
650
681
  format: z.enum(FORMATS).optional()
651
682
  });
652
683
  const TinaCloudCollection = TinaCloudCollectionBase.extend({
653
- fields: z.array(TinaFieldZod).min(1).optional().refine((val) => !hasDuplicates(val == null ? void 0 : val.map((x) => x.name)), {
654
- message: "Fields must have a unique name"
684
+ fields: z.array(TinaFieldZod).min(1).optional().superRefine((val, ctx) => {
685
+ const dups = findDuplicates(val == null ? void 0 : val.map((x) => x.name));
686
+ if (dups) {
687
+ ctx.addIssue({
688
+ code: z.ZodIssueCode.custom,
689
+ message: `Fields must have a unique name, duplicate field names: ${dups}`
690
+ });
691
+ }
655
692
  }).refine((val) => {
656
693
  const arr = (val == null ? void 0 : val.filter((x) => x.type === "string" && x.isTitle)) || [];
657
694
  return arr.length < 2;
658
695
  }, {
659
696
  message: "Fields can only have one use of `isTitle`"
660
697
  }),
661
- templates: z.array(Template).min(1).optional().refine((val) => !hasDuplicates(val == null ? void 0 : val.map((x) => x.name)), {
662
- message: "Templates must have a unique name"
698
+ templates: z.array(Template).min(1).optional().superRefine((val, ctx) => {
699
+ const dups = findDuplicates(val == null ? void 0 : val.map((x) => x.name));
700
+ if (dups) {
701
+ ctx.addIssue({
702
+ code: z.ZodIssueCode.custom,
703
+ message: `Templates must have a unique name, duplicate template names: ${dups}`
704
+ });
705
+ }
663
706
  })
664
707
  }).refine((val) => {
665
708
  let isValid = Boolean(val == null ? void 0 : val.templates) || Boolean(val == null ? void 0 : val.fields);
@@ -674,20 +717,21 @@ const TinaCloudSchemaZod = z.object({
674
717
  collections: z.array(TinaCloudCollection),
675
718
  config: tinaConfigZod.optional()
676
719
  }).superRefine((val, ctx) => {
677
- var _a, _b;
678
- if (hasDuplicates(val.collections.map((x) => x.name))) {
720
+ var _a, _b, _c;
721
+ const dups = findDuplicates((_a = val.collections) == null ? void 0 : _a.map((x) => x.name));
722
+ if (dups) {
679
723
  ctx.addIssue({
680
724
  code: z.ZodIssueCode.custom,
681
- message: "can not have two collections with the same name",
725
+ message: `${dups} are duplicate names in your collections. Collection names must be unique.`,
682
726
  fatal: true
683
727
  });
684
728
  }
685
- (_a = val == null ? void 0 : val.collections) == null ? void 0 : _a.map((x) => {
729
+ (_b = val == null ? void 0 : val.collections) == null ? void 0 : _b.map((x) => {
686
730
  if (!x.format) {
687
731
  console.warn(`No format provided for collection ${x.name}, defaulting to .md`);
688
732
  }
689
733
  });
690
- const media = (_b = val == null ? void 0 : val.config) == null ? void 0 : _b.media;
734
+ const media = (_c = val == null ? void 0 : val.config) == null ? void 0 : _c.media;
691
735
  if (media && media.tina && media.loadCustomStore) {
692
736
  ctx.addIssue({
693
737
  code: z.ZodIssueCode.custom,
package/dist/index.js CHANGED
@@ -132,12 +132,14 @@
132
132
  return generateNamespacedFieldName(namespace, "ConnectionEdges");
133
133
  }
134
134
  };
135
- function hasDuplicates(array) {
136
- if (!array) {
137
- return false;
138
- } else {
139
- return new Set(array).size !== array.length;
140
- }
135
+ function findDuplicates(array = []) {
136
+ const duplicates = [
137
+ ...new Set(array.filter((item, index) => array.indexOf(item) !== index))
138
+ ].map((x) => `"${x}"`);
139
+ if (duplicates.length) {
140
+ return duplicates.join(", ");
141
+ } else
142
+ return void 0;
141
143
  }
142
144
  class TinaSchema {
143
145
  constructor(config) {
@@ -484,7 +486,7 @@
484
486
  }).superRefine((val, ctx) => {
485
487
  if (val.includes(" "))
486
488
  ctx.addIssue({
487
- message: "name cannot contain spaces",
489
+ message: `name "${val}" cannot contain spaces`,
488
490
  code: z.z.ZodIssueCode.custom,
489
491
  fatal: true
490
492
  });
@@ -554,22 +556,37 @@
554
556
  start: z.z.string(),
555
557
  end: z.z.string()
556
558
  }).optional()
557
- }).refine((val) => {
558
- var _a;
559
- return !hasDuplicates((_a = val.fields) == null ? void 0 : _a.map((x) => x.name));
560
- }, {
561
- message: "Fields must have a unique name"
559
+ }).superRefine((val, ctx) => {
560
+ const dups = findDuplicates(val == null ? void 0 : val.fields.map((x) => x.name));
561
+ if (dups) {
562
+ ctx.addIssue({
563
+ code: z.z.ZodIssueCode.custom,
564
+ message: `Fields must have a unique name, duplicate field names: ${dups}`
565
+ });
566
+ }
562
567
  });
563
568
  const ObjectField = FieldWithList.extend({
564
569
  type: z.z.literal("object", {
565
570
  invalid_type_error: typeTypeError,
566
571
  required_error: typeRequiredError
567
572
  }),
568
- fields: z.z.array(TinaFieldZod).min(1).optional().refine((val) => !hasDuplicates(val == null ? void 0 : val.map((x) => x.name)), {
569
- message: "Fields must have a unique name"
573
+ fields: z.z.array(TinaFieldZod).min(1).optional().superRefine((val, ctx) => {
574
+ const dups = findDuplicates(val == null ? void 0 : val.map((x) => x.name));
575
+ if (dups) {
576
+ ctx.addIssue({
577
+ code: z.z.ZodIssueCode.custom,
578
+ message: `Fields must have a unique name, duplicate field names: ${dups}`
579
+ });
580
+ }
570
581
  }),
571
- templates: z.z.array(TemplateTemp).min(1).optional().refine((val) => !hasDuplicates(val == null ? void 0 : val.map((x) => x.name)), {
572
- message: "Templates must have a unique name"
582
+ templates: z.z.array(TemplateTemp).min(1).optional().superRefine((val, ctx) => {
583
+ const dups = findDuplicates(val == null ? void 0 : val.map((x) => x.name));
584
+ if (dups) {
585
+ ctx.addIssue({
586
+ code: z.z.ZodIssueCode.custom,
587
+ message: `Templates must have a unique name, duplicate template names: ${dups}`
588
+ });
589
+ }
573
590
  })
574
591
  });
575
592
  const RichTextField = FieldWithList.extend({
@@ -577,8 +594,14 @@
577
594
  invalid_type_error: typeTypeError,
578
595
  required_error: typeRequiredError
579
596
  }),
580
- templates: z.z.array(TemplateTemp).optional().refine((val) => !hasDuplicates(val == null ? void 0 : val.map((x) => x.name)), {
581
- message: "Templates must have a unique name"
597
+ templates: z.z.array(TemplateTemp).optional().superRefine((val, ctx) => {
598
+ const dups = findDuplicates(val == null ? void 0 : val.map((x) => x.name));
599
+ if (dups) {
600
+ ctx.addIssue({
601
+ code: z.z.ZodIssueCode.custom,
602
+ message: `Templates must have a unique name, duplicate template names: ${dups}`
603
+ });
604
+ }
582
605
  })
583
606
  });
584
607
  return z.z.discriminatedUnion("type", [
@@ -608,13 +631,17 @@
608
631
  if (val.list) {
609
632
  ctx.addIssue({
610
633
  code: z.z.ZodIssueCode.custom,
611
- message: "You can not have `list: true` when using `isTitle`"
634
+ message: `Can not have \`list: true\` when using \`isTitle\`. Error in value
635
+ ${JSON.stringify(val, null, 2)}
636
+ `
612
637
  });
613
638
  }
614
639
  if (!val.required) {
615
640
  ctx.addIssue({
616
641
  code: z.z.ZodIssueCode.custom,
617
- message: "You must have { required: true } when using `isTitle`"
642
+ message: `Must have { required: true } when using \`isTitle\` Error in value
643
+ ${JSON.stringify(val, null, 2)}
644
+ `
618
645
  });
619
646
  }
620
647
  }
@@ -665,11 +692,15 @@
665
692
  }),
666
693
  name,
667
694
  fields: z.z.array(TinaFieldZod)
668
- }).refine((val) => {
695
+ }).superRefine((val, ctx) => {
669
696
  var _a;
670
- return !hasDuplicates((_a = val.fields) == null ? void 0 : _a.map((x) => x.name));
671
- }, {
672
- message: "Fields must have a unique name"
697
+ const dups = findDuplicates((_a = val.fields) == null ? void 0 : _a.map((x) => x.name));
698
+ if (dups) {
699
+ ctx.addIssue({
700
+ code: z.z.ZodIssueCode.custom,
701
+ message: `Fields must have a unique name, duplicate field names: ${dups}`
702
+ });
703
+ }
673
704
  });
674
705
  const TinaCloudCollectionBase = z.z.object({
675
706
  label: z.z.string().optional(),
@@ -677,16 +708,28 @@
677
708
  format: z.z.enum(FORMATS).optional()
678
709
  });
679
710
  const TinaCloudCollection = TinaCloudCollectionBase.extend({
680
- fields: z.z.array(TinaFieldZod).min(1).optional().refine((val) => !hasDuplicates(val == null ? void 0 : val.map((x) => x.name)), {
681
- message: "Fields must have a unique name"
711
+ fields: z.z.array(TinaFieldZod).min(1).optional().superRefine((val, ctx) => {
712
+ const dups = findDuplicates(val == null ? void 0 : val.map((x) => x.name));
713
+ if (dups) {
714
+ ctx.addIssue({
715
+ code: z.z.ZodIssueCode.custom,
716
+ message: `Fields must have a unique name, duplicate field names: ${dups}`
717
+ });
718
+ }
682
719
  }).refine((val) => {
683
720
  const arr = (val == null ? void 0 : val.filter((x) => x.type === "string" && x.isTitle)) || [];
684
721
  return arr.length < 2;
685
722
  }, {
686
723
  message: "Fields can only have one use of `isTitle`"
687
724
  }),
688
- templates: z.z.array(Template).min(1).optional().refine((val) => !hasDuplicates(val == null ? void 0 : val.map((x) => x.name)), {
689
- message: "Templates must have a unique name"
725
+ templates: z.z.array(Template).min(1).optional().superRefine((val, ctx) => {
726
+ const dups = findDuplicates(val == null ? void 0 : val.map((x) => x.name));
727
+ if (dups) {
728
+ ctx.addIssue({
729
+ code: z.z.ZodIssueCode.custom,
730
+ message: `Templates must have a unique name, duplicate template names: ${dups}`
731
+ });
732
+ }
690
733
  })
691
734
  }).refine((val) => {
692
735
  let isValid = Boolean(val == null ? void 0 : val.templates) || Boolean(val == null ? void 0 : val.fields);
@@ -701,20 +744,21 @@
701
744
  collections: z.z.array(TinaCloudCollection),
702
745
  config: tinaConfigZod.optional()
703
746
  }).superRefine((val, ctx) => {
704
- var _a, _b;
705
- if (hasDuplicates(val.collections.map((x) => x.name))) {
747
+ var _a, _b, _c;
748
+ const dups = findDuplicates((_a = val.collections) == null ? void 0 : _a.map((x) => x.name));
749
+ if (dups) {
706
750
  ctx.addIssue({
707
751
  code: z.z.ZodIssueCode.custom,
708
- message: "can not have two collections with the same name",
752
+ message: `${dups} are duplicate names in your collections. Collection names must be unique.`,
709
753
  fatal: true
710
754
  });
711
755
  }
712
- (_a = val == null ? void 0 : val.collections) == null ? void 0 : _a.map((x) => {
756
+ (_b = val == null ? void 0 : val.collections) == null ? void 0 : _b.map((x) => {
713
757
  if (!x.format) {
714
758
  console.warn(`No format provided for collection ${x.name}, defaulting to .md`);
715
759
  }
716
760
  });
717
- const media = (_b = val == null ? void 0 : val.config) == null ? void 0 : _b.media;
761
+ const media = (_c = val == null ? void 0 : val.config) == null ? void 0 : _c.media;
718
762
  if (media && media.tina && media.loadCustomStore) {
719
763
  ctx.addIssue({
720
764
  code: z.z.ZodIssueCode.custom,
@@ -10,4 +10,10 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10
10
  See the License for the specific language governing permissions and
11
11
  limitations under the License.
12
12
  */
13
- export declare function hasDuplicates<T = any>(array: T[]): boolean;
13
+ export declare function hasDuplicates<T = any>(array?: T[]): boolean;
14
+ /**
15
+ *
16
+ * @param array
17
+ * @returns False if the array is undefined or has no duplicates.
18
+ */
19
+ export declare function findDuplicates<T = any>(array?: T[] | undefined): undefined | string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tinacms/schema-tools",
3
- "version": "0.1.1",
3
+ "version": "0.1.2",
4
4
  "main": "dist/index.js",
5
5
  "module": "./dist/index.es.js",
6
6
  "exports": {