@encatch/schema 0.1.18 → 0.1.19

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/esm/index.js CHANGED
@@ -586,93 +586,111 @@ import { z as z8 } from "zod";
586
586
 
587
587
  // src/schemas/fields/translations-schema.ts
588
588
  import { z as z4 } from "zod";
589
- var translationEntrySchema = z4.object({
590
- text: z4.string().min(1).describe("Translated text content"),
591
- context: z4.string().optional().describe("Optional context for translators")
592
- }).describe("Translation entry with translated text");
593
- var ratingTranslationsSchema = z4.object({
589
+ var translationEntrySchema = z4.string().min(1).describe("Translated text content");
590
+ var ratingQuestionTranslationSchema = z4.object({
591
+ type: z4.literal("rating").describe("Question type identifier"),
594
592
  title: translationEntrySchema.describe("Question title translation"),
595
593
  description: translationEntrySchema.optional().describe("Question description translation"),
596
594
  errorMessage: translationEntrySchema.optional().describe("Error message translation"),
597
595
  minLabel: translationEntrySchema.optional().describe("Minimum rating label translation"),
598
596
  maxLabel: translationEntrySchema.optional().describe("Maximum rating label translation")
599
597
  }).describe("Translation schema for rating questions");
600
- var singleChoiceTranslationsSchema = z4.object({
598
+ var singleChoiceQuestionTranslationSchema = z4.object({
599
+ type: z4.literal("single_choice").describe("Question type identifier"),
601
600
  title: translationEntrySchema.describe("Question title translation"),
602
601
  description: translationEntrySchema.optional().describe("Question description translation"),
603
602
  errorMessage: translationEntrySchema.optional().describe("Error message translation"),
604
- placeholder: translationEntrySchema.optional().describe("Input placeholder translation"),
605
- options: z4.record(z4.string(), z4.object({
606
- label: translationEntrySchema.describe("Option label translation"),
607
- hint: translationEntrySchema.optional().describe("Option hint translation")
608
- })).describe("Option translations keyed by option ID")
603
+ placeholder: translationEntrySchema.optional().describe("Input placeholder translation")
604
+ }).catchall(translationEntrySchema).refine((data) => {
605
+ const additionalKeys = Object.keys(data).filter(
606
+ (key) => !["type", "title", "description", "errorMessage", "placeholder"].includes(key)
607
+ );
608
+ return additionalKeys.every((key) => /^option\..+\.(label|value)$/.test(key));
609
+ }, {
610
+ message: "Additional keys must follow the pattern 'option.{id}.{label|value}'"
609
611
  }).describe("Translation schema for single choice questions");
610
- var multipleChoiceTranslationsSchema = z4.object({
612
+ var multipleChoiceQuestionTranslationSchema = z4.object({
613
+ type: z4.literal("multiple_choice_multiple").describe("Question type identifier"),
611
614
  title: translationEntrySchema.describe("Question title translation"),
612
615
  description: translationEntrySchema.optional().describe("Question description translation"),
613
616
  errorMessage: translationEntrySchema.optional().describe("Error message translation"),
614
- placeholder: translationEntrySchema.optional().describe("Input placeholder translation"),
615
- options: z4.record(z4.string(), z4.object({
616
- label: translationEntrySchema.describe("Option label translation"),
617
- hint: translationEntrySchema.optional().describe("Option hint translation")
618
- })).describe("Option translations keyed by option ID")
617
+ placeholder: translationEntrySchema.optional().describe("Input placeholder translation")
618
+ }).catchall(translationEntrySchema).refine((data) => {
619
+ const additionalKeys = Object.keys(data).filter(
620
+ (key) => !["type", "title", "description", "errorMessage", "placeholder"].includes(key)
621
+ );
622
+ return additionalKeys.every((key) => /^option\..+\.(label|value)$/.test(key));
623
+ }, {
624
+ message: "Additional keys must follow the pattern 'option.{id}.{label|value}'"
619
625
  }).describe("Translation schema for multiple choice questions");
620
- var npsTranslationsSchema = z4.object({
626
+ var npsQuestionTranslationSchema = z4.object({
627
+ type: z4.literal("nps").describe("Question type identifier"),
621
628
  title: translationEntrySchema.describe("Question title translation"),
622
629
  description: translationEntrySchema.optional().describe("Question description translation"),
623
630
  errorMessage: translationEntrySchema.optional().describe("Error message translation"),
624
631
  minLabel: translationEntrySchema.optional().describe("Minimum NPS label (0) translation"),
625
- maxLabel: translationEntrySchema.optional().describe("Maximum NPS label (10) translation"),
626
- scaleLabels: z4.record(z4.number().int().min(0).max(10), translationEntrySchema).optional().describe("Scale label translations for specific NPS values (0-10)")
632
+ maxLabel: translationEntrySchema.optional().describe("Maximum NPS label (10) translation")
633
+ }).catchall(translationEntrySchema).refine((data) => {
634
+ const additionalKeys = Object.keys(data).filter(
635
+ (key) => !["type", "title", "description", "errorMessage", "minLabel", "maxLabel"].includes(key)
636
+ );
637
+ return additionalKeys.every((key) => /^scaleLabel\.\d+$/.test(key));
638
+ }, {
639
+ message: "Additional keys must follow the pattern 'scaleLabel.{number}'"
627
640
  }).describe("Translation schema for NPS questions");
628
- var shortAnswerTranslationsSchema = z4.object({
641
+ var shortAnswerQuestionTranslationSchema = z4.object({
642
+ type: z4.literal("short_answer").describe("Question type identifier"),
629
643
  title: translationEntrySchema.describe("Question title translation"),
630
644
  description: translationEntrySchema.optional().describe("Question description translation"),
631
645
  errorMessage: translationEntrySchema.optional().describe("Error message translation"),
632
646
  placeholder: translationEntrySchema.optional().describe("Input placeholder translation")
633
647
  }).describe("Translation schema for short answer questions");
634
- var longAnswerTranslationsSchema = z4.object({
648
+ var longAnswerQuestionTranslationSchema = z4.object({
649
+ type: z4.literal("long_text").describe("Question type identifier"),
635
650
  title: translationEntrySchema.describe("Question title translation"),
636
651
  description: translationEntrySchema.optional().describe("Question description translation"),
637
652
  errorMessage: translationEntrySchema.optional().describe("Error message translation"),
638
653
  placeholder: translationEntrySchema.optional().describe("Textarea placeholder translation")
639
654
  }).describe("Translation schema for long answer questions");
640
- var nestedSelectionTranslationsSchema = z4.object({
655
+ var nestedSelectionQuestionTranslationSchema = z4.object({
656
+ type: z4.literal("nested_selection").describe("Question type identifier"),
641
657
  title: translationEntrySchema.describe("Question title translation"),
642
658
  description: translationEntrySchema.optional().describe("Question description translation"),
643
659
  errorMessage: translationEntrySchema.optional().describe("Error message translation"),
644
- placeholder: translationEntrySchema.optional().describe("Dropdown placeholder translation"),
645
- nestedOptions: z4.record(z4.string(), z4.object({
646
- label: translationEntrySchema.describe("Nested option label translation"),
647
- hint: translationEntrySchema.optional().describe("Nested option hint translation")
648
- })).describe("Nested option translations keyed by option ID")
660
+ placeholder: translationEntrySchema.optional().describe("Dropdown placeholder translation")
661
+ }).catchall(translationEntrySchema).refine((data) => {
662
+ const additionalKeys = Object.keys(data).filter(
663
+ (key) => !["type", "title", "description", "errorMessage", "placeholder"].includes(key)
664
+ );
665
+ return additionalKeys.every((key) => /^nestedOption\..+\.(label|hint)$/.test(key));
666
+ }, {
667
+ message: "Additional keys must follow the pattern 'nestedOption.{id}.{label|hint}'"
649
668
  }).describe("Translation schema for nested selection questions");
650
- var annotationTranslationsSchema = z4.object({
669
+ var annotationQuestionTranslationSchema = z4.object({
670
+ type: z4.literal("annotation").describe("Question type identifier"),
651
671
  title: translationEntrySchema.describe("Question title translation"),
652
672
  description: translationEntrySchema.optional().describe("Question description translation"),
653
673
  errorMessage: translationEntrySchema.optional().describe("Error message translation"),
654
674
  annotationText: translationEntrySchema.optional().describe("Annotation display text translation"),
655
675
  noAnnotationText: translationEntrySchema.optional().describe("No annotation display text translation")
656
676
  }).describe("Translation schema for annotation questions");
657
- var questionTranslationsSchema = z4.discriminatedUnion("type", [
658
- z4.object({ type: z4.literal("rating"), translations: ratingTranslationsSchema }),
659
- z4.object({ type: z4.literal("single_choice"), translations: singleChoiceTranslationsSchema }),
660
- z4.object({ type: z4.literal("multiple_choice_multiple"), translations: multipleChoiceTranslationsSchema }),
661
- z4.object({ type: z4.literal("nps"), translations: npsTranslationsSchema }),
662
- z4.object({ type: z4.literal("short_answer"), translations: shortAnswerTranslationsSchema }),
663
- z4.object({ type: z4.literal("long_text"), translations: longAnswerTranslationsSchema }),
664
- z4.object({ type: z4.literal("nested_selection"), translations: nestedSelectionTranslationsSchema }),
665
- z4.object({ type: z4.literal("annotation"), translations: annotationTranslationsSchema })
666
- ]).describe("Discriminated union of all question type translation schemas");
667
- var languageTranslationsSchema = z4.object({
668
- languageCode: z4.string().describe("Language code matching LanguagesSchema values"),
669
- questions: z4.record(z4.string(), questionTranslationsSchema).describe("Question translations keyed by question ID")
670
- }).describe("Language-specific translation set keyed by question IDs");
677
+ var questionTranslationSchema = z4.union([
678
+ ratingQuestionTranslationSchema,
679
+ singleChoiceQuestionTranslationSchema,
680
+ multipleChoiceQuestionTranslationSchema,
681
+ npsQuestionTranslationSchema,
682
+ shortAnswerQuestionTranslationSchema,
683
+ longAnswerQuestionTranslationSchema,
684
+ nestedSelectionQuestionTranslationSchema,
685
+ annotationQuestionTranslationSchema
686
+ ]).describe("Union of all question type translation schemas");
687
+ var languageCodeSchema = z4.string().min(2).max(10).describe("Language code (e.g., 'en', 'es', 'hi')");
688
+ var questionTranslationsByLanguageSchema = z4.record(languageCodeSchema, questionTranslationSchema).describe("Question translations keyed by language code");
689
+ var questionCentricTranslationsSchema = z4.record(z4.string().uuid(), questionTranslationsByLanguageSchema).describe("Question-centric translations keyed by question ID, then by language code");
671
690
  var translationsSchema = z4.object({
672
691
  defaultLanguage: z4.string().describe("Default/fallback language code"),
673
- languages: z4.array(languageTranslationsSchema).min(1).describe("Available language translations"),
674
- version: z4.string().optional().describe("Translation version for cache management")
675
- }).describe("Complete translation schema keyed by question IDs");
692
+ questions: questionCentricTranslationsSchema.describe("Question-centric translations")
693
+ }).describe("Complete question-centric translation schema");
676
694
  var _TranslationProvider = class _TranslationProvider {
677
695
  constructor(translations) {
678
696
  this.translations = translations;
@@ -681,48 +699,59 @@ var _TranslationProvider = class _TranslationProvider {
681
699
  * Get translations for a specific question in a specific language
682
700
  */
683
701
  getQuestionTranslations(questionId, languageCode) {
684
- const languageData = this.translations.languages.find(
685
- (lang) => lang.languageCode === languageCode
686
- );
687
- if (!languageData) {
688
- const defaultLang = this.translations.languages.find(
689
- (lang) => lang.languageCode === this.translations.defaultLanguage
690
- );
691
- if (!defaultLang) return null;
692
- return defaultLang.questions[questionId] || null;
702
+ const questionTranslations = this.translations.questions[questionId];
703
+ if (!questionTranslations) return null;
704
+ let translation = questionTranslations[languageCode];
705
+ if (!translation) {
706
+ translation = questionTranslations[this.translations.defaultLanguage];
693
707
  }
694
- return languageData.questions[questionId] || null;
708
+ return translation || null;
695
709
  }
696
710
  /**
697
- * Get a specific translation text
711
+ * Get a specific translation text by field path
698
712
  */
699
- getTranslationText(questionId, languageCode, path) {
700
- const questionTranslations = this.getQuestionTranslations(questionId, languageCode);
701
- if (!questionTranslations) return null;
702
- let current = questionTranslations.translations;
703
- for (const key of path) {
704
- if (current && typeof current === "object" && key in current) {
705
- current = current[key];
706
- } else {
707
- return null;
708
- }
709
- }
710
- if (current && typeof current === "object" && "text" in current) {
711
- return current.text;
713
+ getTranslationText(questionId, languageCode, fieldPath) {
714
+ const questionTranslation = this.getQuestionTranslations(questionId, languageCode);
715
+ if (!questionTranslation) return null;
716
+ if (fieldPath in questionTranslation) {
717
+ const value = questionTranslation[fieldPath];
718
+ return typeof value === "string" ? value : null;
712
719
  }
713
720
  return null;
714
721
  }
715
722
  /**
716
- * Get all available languages
723
+ * Get all available languages for a specific question
724
+ */
725
+ getQuestionLanguages(questionId) {
726
+ const questionTranslations = this.translations.questions[questionId];
727
+ if (!questionTranslations) return [];
728
+ return Object.keys(questionTranslations);
729
+ }
730
+ /**
731
+ * Get all available languages across all questions
717
732
  */
718
733
  getAvailableLanguages() {
719
- return this.translations.languages.map((lang) => lang.languageCode);
734
+ const allLanguages = /* @__PURE__ */ new Set();
735
+ Object.values(this.translations.questions).forEach((questionTranslations) => {
736
+ Object.keys(questionTranslations).forEach((langCode) => {
737
+ allLanguages.add(langCode);
738
+ });
739
+ });
740
+ return Array.from(allLanguages);
720
741
  }
721
742
  /**
722
- * Check if a language is supported
743
+ * Check if a language is supported for a specific question
744
+ */
745
+ isLanguageSupportedForQuestion(questionId, languageCode) {
746
+ const questionTranslations = this.translations.questions[questionId];
747
+ if (!questionTranslations) return false;
748
+ return languageCode in questionTranslations;
749
+ }
750
+ /**
751
+ * Check if a language is supported across all questions
723
752
  */
724
753
  isLanguageSupported(languageCode) {
725
- return this.translations.languages.some((lang) => lang.languageCode === languageCode);
754
+ return this.getAvailableLanguages().includes(languageCode);
726
755
  }
727
756
  /**
728
757
  * Get the default language
@@ -730,6 +759,20 @@ var _TranslationProvider = class _TranslationProvider {
730
759
  getDefaultLanguage() {
731
760
  return this.translations.defaultLanguage;
732
761
  }
762
+ /**
763
+ * Get all questions that have translations
764
+ */
765
+ getQuestionIds() {
766
+ return Object.keys(this.translations.questions);
767
+ }
768
+ /**
769
+ * Get question type for a specific question
770
+ */
771
+ getQuestionType(questionId, languageCode) {
772
+ const langCode = languageCode || this.translations.defaultLanguage;
773
+ const translation = this.getQuestionTranslations(questionId, langCode);
774
+ return translation?.type || null;
775
+ }
733
776
  };
734
777
  __name(_TranslationProvider, "TranslationProvider");
735
778
  var TranslationProvider = _TranslationProvider;
@@ -1195,7 +1238,7 @@ export {
1195
1238
  WelcomeScreenFieldsSchema,
1196
1239
  YesNoValues,
1197
1240
  annotationQuestionSchema,
1198
- annotationTranslationsSchema,
1241
+ annotationQuestionTranslationSchema,
1199
1242
  appearancePropertiesSchema,
1200
1243
  audienceSegmentSchema,
1201
1244
  audienceTriggerPropertiesSchema,
@@ -1230,43 +1273,43 @@ export {
1230
1273
  formConfigurationResponseSchema,
1231
1274
  formPropertiesSchema,
1232
1275
  frequencyAndSchedulingPropertiesSchema,
1233
- languageTranslationsSchema,
1234
1276
  longAnswerQuestionSchema,
1235
- longAnswerTranslationsSchema,
1277
+ longAnswerQuestionTranslationSchema,
1236
1278
  masterPropertiesSchema,
1237
1279
  matchedTriggerPropertiesSchema,
1238
1280
  multipleChoiceDisplayStyleSchema,
1239
1281
  multipleChoiceMultipleDisplayStyleSchema,
1240
1282
  multipleChoiceMultipleQuestionSchema,
1283
+ multipleChoiceQuestionTranslationSchema,
1241
1284
  multipleChoiceSingleQuestionSchema,
1242
- multipleChoiceTranslationsSchema,
1243
1285
  nestedDropdownQuestionSchema,
1244
1286
  nestedOptionSchema,
1245
- nestedSelectionTranslationsSchema,
1287
+ nestedSelectionQuestionTranslationSchema,
1246
1288
  npsQuestionSchema,
1247
- npsTranslationsSchema,
1289
+ npsQuestionTranslationSchema,
1248
1290
  otherConfigurationPropertiesSchema,
1249
1291
  positionSchema,
1250
1292
  publicationStatusSchema,
1251
1293
  queryOutputSchema,
1294
+ questionCentricTranslationsSchema,
1252
1295
  questionOptionSchema,
1253
1296
  questionResponseSchema,
1254
1297
  questionSchema,
1255
1298
  questionStatusSchema,
1256
- questionTranslationsSchema,
1299
+ questionTranslationSchema,
1257
1300
  questionTypeSchema,
1258
1301
  questionnaireFieldsResponseSchema,
1259
1302
  ratingDisplayStyleSchema,
1260
1303
  ratingQuestionSchema,
1304
+ ratingQuestionTranslationSchema,
1261
1305
  ratingRepresentationSizeSchema,
1262
- ratingTranslationsSchema,
1263
1306
  recurringUnitSchema,
1264
1307
  responseSchema,
1265
1308
  sectionSchema,
1266
1309
  sessionInfoSchema,
1267
1310
  shortAnswerQuestionSchema,
1268
- shortAnswerTranslationsSchema,
1269
- singleChoiceTranslationsSchema,
1311
+ shortAnswerQuestionTranslationSchema,
1312
+ singleChoiceQuestionTranslationSchema,
1270
1313
  submitFeedbackSchema,
1271
1314
  surveyTypeSchema,
1272
1315
  themeColorsSchema,