@encatch/schema 0.1.18 → 0.1.20

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,155 +586,196 @@ 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");
671
- var translationsSchema = z4.object({
672
- 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");
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");
690
+ var translationsSchema = questionCentricTranslationsSchema.describe("Question-centric translations at root level");
676
691
  var _TranslationProvider = class _TranslationProvider {
677
- constructor(translations) {
692
+ constructor(translations, defaultLanguage = "en") {
678
693
  this.translations = translations;
694
+ this.defaultLanguage = defaultLanguage;
679
695
  }
680
696
  /**
681
697
  * Get translations for a specific question in a specific language
682
698
  */
683
699
  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;
700
+ const questionTranslations = this.translations[questionId];
701
+ if (!questionTranslations) return null;
702
+ let translation = questionTranslations[languageCode];
703
+ if (!translation) {
704
+ translation = questionTranslations[this.defaultLanguage];
693
705
  }
694
- return languageData.questions[questionId] || null;
706
+ return translation || null;
695
707
  }
696
708
  /**
697
- * Get a specific translation text
709
+ * Get a specific translation text by field path
698
710
  */
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;
711
+ getTranslationText(questionId, languageCode, fieldPath) {
712
+ const questionTranslation = this.getQuestionTranslations(questionId, languageCode);
713
+ if (!questionTranslation) return null;
714
+ if (fieldPath in questionTranslation) {
715
+ const value = questionTranslation[fieldPath];
716
+ return typeof value === "string" ? value : null;
712
717
  }
713
718
  return null;
714
719
  }
715
720
  /**
716
- * Get all available languages
721
+ * Get all available languages for a specific question
722
+ */
723
+ getQuestionLanguages(questionId) {
724
+ const questionTranslations = this.translations[questionId];
725
+ if (!questionTranslations) return [];
726
+ return Object.keys(questionTranslations);
727
+ }
728
+ /**
729
+ * Get all available languages across all questions
717
730
  */
718
731
  getAvailableLanguages() {
719
- return this.translations.languages.map((lang) => lang.languageCode);
732
+ const allLanguages = /* @__PURE__ */ new Set();
733
+ Object.values(this.translations).forEach((questionTranslations) => {
734
+ Object.keys(questionTranslations).forEach((langCode) => {
735
+ allLanguages.add(langCode);
736
+ });
737
+ });
738
+ return Array.from(allLanguages);
720
739
  }
721
740
  /**
722
- * Check if a language is supported
741
+ * Check if a language is supported for a specific question
742
+ */
743
+ isLanguageSupportedForQuestion(questionId, languageCode) {
744
+ const questionTranslations = this.translations[questionId];
745
+ if (!questionTranslations) return false;
746
+ return languageCode in questionTranslations;
747
+ }
748
+ /**
749
+ * Check if a language is supported across all questions
723
750
  */
724
751
  isLanguageSupported(languageCode) {
725
- return this.translations.languages.some((lang) => lang.languageCode === languageCode);
752
+ return this.getAvailableLanguages().includes(languageCode);
726
753
  }
727
754
  /**
728
755
  * Get the default language
729
756
  */
730
757
  getDefaultLanguage() {
731
- return this.translations.defaultLanguage;
758
+ return this.defaultLanguage;
759
+ }
760
+ /**
761
+ * Get all questions that have translations
762
+ */
763
+ getQuestionIds() {
764
+ return Object.keys(this.translations);
765
+ }
766
+ /**
767
+ * Get question type for a specific question
768
+ */
769
+ getQuestionType(questionId, languageCode) {
770
+ const langCode = languageCode || this.defaultLanguage;
771
+ const translation = this.getQuestionTranslations(questionId, langCode);
772
+ return translation?.type || null;
732
773
  }
733
774
  };
734
775
  __name(_TranslationProvider, "TranslationProvider");
735
776
  var TranslationProvider = _TranslationProvider;
736
- function createTranslationProvider(translations) {
737
- return new TranslationProvider(translations);
777
+ function createTranslationProvider(translations, defaultLanguage = "en") {
778
+ return new TranslationProvider(translations, defaultLanguage);
738
779
  }
739
780
  __name(createTranslationProvider, "createTranslationProvider");
740
781
 
@@ -1195,7 +1236,7 @@ export {
1195
1236
  WelcomeScreenFieldsSchema,
1196
1237
  YesNoValues,
1197
1238
  annotationQuestionSchema,
1198
- annotationTranslationsSchema,
1239
+ annotationQuestionTranslationSchema,
1199
1240
  appearancePropertiesSchema,
1200
1241
  audienceSegmentSchema,
1201
1242
  audienceTriggerPropertiesSchema,
@@ -1230,43 +1271,43 @@ export {
1230
1271
  formConfigurationResponseSchema,
1231
1272
  formPropertiesSchema,
1232
1273
  frequencyAndSchedulingPropertiesSchema,
1233
- languageTranslationsSchema,
1234
1274
  longAnswerQuestionSchema,
1235
- longAnswerTranslationsSchema,
1275
+ longAnswerQuestionTranslationSchema,
1236
1276
  masterPropertiesSchema,
1237
1277
  matchedTriggerPropertiesSchema,
1238
1278
  multipleChoiceDisplayStyleSchema,
1239
1279
  multipleChoiceMultipleDisplayStyleSchema,
1240
1280
  multipleChoiceMultipleQuestionSchema,
1281
+ multipleChoiceQuestionTranslationSchema,
1241
1282
  multipleChoiceSingleQuestionSchema,
1242
- multipleChoiceTranslationsSchema,
1243
1283
  nestedDropdownQuestionSchema,
1244
1284
  nestedOptionSchema,
1245
- nestedSelectionTranslationsSchema,
1285
+ nestedSelectionQuestionTranslationSchema,
1246
1286
  npsQuestionSchema,
1247
- npsTranslationsSchema,
1287
+ npsQuestionTranslationSchema,
1248
1288
  otherConfigurationPropertiesSchema,
1249
1289
  positionSchema,
1250
1290
  publicationStatusSchema,
1251
1291
  queryOutputSchema,
1292
+ questionCentricTranslationsSchema,
1252
1293
  questionOptionSchema,
1253
1294
  questionResponseSchema,
1254
1295
  questionSchema,
1255
1296
  questionStatusSchema,
1256
- questionTranslationsSchema,
1297
+ questionTranslationSchema,
1257
1298
  questionTypeSchema,
1258
1299
  questionnaireFieldsResponseSchema,
1259
1300
  ratingDisplayStyleSchema,
1260
1301
  ratingQuestionSchema,
1302
+ ratingQuestionTranslationSchema,
1261
1303
  ratingRepresentationSizeSchema,
1262
- ratingTranslationsSchema,
1263
1304
  recurringUnitSchema,
1264
1305
  responseSchema,
1265
1306
  sectionSchema,
1266
1307
  sessionInfoSchema,
1267
1308
  shortAnswerQuestionSchema,
1268
- shortAnswerTranslationsSchema,
1269
- singleChoiceTranslationsSchema,
1309
+ shortAnswerQuestionTranslationSchema,
1310
+ singleChoiceQuestionTranslationSchema,
1270
1311
  submitFeedbackSchema,
1271
1312
  surveyTypeSchema,
1272
1313
  themeColorsSchema,