@forge/manifest
13.8.1-next.0-experimental-1e9569613.9.0-next.3
out/validators/translations-validator.js~
out/validators/translations-validator.jsModified+42−21
Index: package/out/validators/translations-validator.js
===================================================================
--- package/out/validators/translations-validator.js
+++ package/out/validators/translations-validator.js
@@ -13,21 +13,27 @@
const utils_1 = require("../utils");
class TranslationsValidator {
validateCache = new Map();
ajv = new ajv_1.default({ allErrors: true, verbose: true, strict: false });
- ensureValidResourcesDefinition(validationErrors, manifest) {
- const { resources } = manifest.yamlContent.translations;
+ ensureValidResourcesDefinition(validationErrors, manifest, resources) {
const resourcesMap = new Map();
+ const seenKeys = new Set();
for (const { key, path } of resources) {
- if (resourcesMap.has(key)) {
+ if (typeof key !== 'string') {
+ continue;
+ }
+ if (seenKeys.has(key)) {
validationErrors.push({
message: text_1.errors.translations.duplicateResourceKey(key),
reference: text_2.References.SchemaError,
level: 'error',
...(0, utils_1.findPosition)(`key: ${key}`, manifest.yamlContentByLine)
});
}
else {
+ seenKeys.add(key);
+ }
+ if (typeof path === 'string' && !resourcesMap.has(key)) {
resourcesMap.set(key, path);
}
}
const allLanguageLookup = {};
@@ -49,15 +55,14 @@
}
}
return allLanguageLookup;
}
- ensureValidFallbackDefinition(validationErrors, manifest) {
- const { resources, fallback } = manifest.yamlContent.translations;
- const resourcesSet = new Set(resources.map((resource) => resource.key));
- const fallbackLocales = new Set(Object.values(fallback).flatMap((locales) => Array.isArray(locales) ? locales : [locales]));
+ ensureValidFallbackDefinition(validationErrors, manifest, resources, fallback) {
+ const resourcesSet = new Set(resources.map(({ key }) => key).filter((key) => typeof key === 'string'));
+ const fallbackLocales = new Set(Object.values(fallback).flatMap((locales) => (Array.isArray(locales) ? locales : [locales])));
for (const fallbackLocale of fallbackLocales) {
if (!resourcesSet.has(fallbackLocale)) {
- const targetLocale = Object.entries(fallback).find(([, locales]) => locales === fallbackLocale || locales.includes(fallbackLocale))?.[0];
+ const targetLocale = Object.entries(fallback).find(([, locales]) => locales === fallbackLocale || (Array.isArray(locales) && locales.includes(fallbackLocale)))?.[0];
validationErrors.push({
message: text_1.errors.translations.missingTranslationsJsonFile(fallbackLocale),
reference: text_2.References.SchemaError,
level: 'error',
@@ -76,19 +81,19 @@
});
}
}
}
- getAllLocalesLookup(validationErrors, i18nKeys, translationsLookup, manifest) {
- const { resources, fallback } = manifest.yamlContent.translations;
- const defaultLocaleCode = fallback.default;
- return resources
- .map((resource) => resource.key)
- .reduce((allLocalesLookup, locale) => {
- const i18nMap = this.getI18nMap(i18nKeys, translationsLookup, locale);
- if (locale === defaultLocaleCode) {
- this.ensureI18nKeysExistInDefaultJson(validationErrors, i18nKeys, translationsLookup, locale, manifest);
+ getAllLocalesLookup(validationErrors, i18nKeys, translationsLookup, manifest, resources, fallback) {
+ const defaultLocale = fallback.default;
+ const defaultLocaleCode = typeof defaultLocale === 'string' ? defaultLocale : undefined;
+ const resourceList = resources.map(({ key }) => key).filter((key) => typeof key === 'string');
+ return resourceList.reduce((allLocalesLookup, locale) => {
+ const forgeLocale = locale;
+ const i18nMap = this.getI18nMap(i18nKeys, translationsLookup, forgeLocale);
+ if (forgeLocale === defaultLocaleCode) {
+ this.ensureI18nKeysExistInDefaultJson(validationErrors, i18nKeys, translationsLookup, forgeLocale, manifest);
}
- allLocalesLookup.set(locale, i18nMap);
+ allLocalesLookup.set(forgeLocale, i18nMap);
return allLocalesLookup;
}, new Map());
}
ensureI18nKeysExistInDefaultJson(validationErrors, i18nKeys, translationsLookup, locale, manifest) {
@@ -222,11 +227,27 @@
};
}
validateManifestI18nConfig(manifest, i18nKeys, moduleI18nProperties) {
const validationErrors = [];
- const allLanguageLookup = this.ensureValidResourcesDefinition(validationErrors, manifest);
- this.ensureValidFallbackDefinition(validationErrors, manifest);
- const i18nMap = this.getAllLocalesLookup(validationErrors, i18nKeys, allLanguageLookup, manifest);
+ const rawResources = manifest.yamlContent.translations.resources;
+ const resources = Array.isArray(rawResources)
+ ? rawResources.filter((resource) => resource !== null && typeof resource === 'object' && !Array.isArray(resource))
+ : [];
+ const rawFallback = manifest.yamlContent.translations.fallback;
+ const fallback = rawFallback !== null && typeof rawFallback === 'object' && !Array.isArray(rawFallback)
+ ? Object.entries(rawFallback).reduce((normalizedFallback, [locale, value]) => {
+ if (typeof value === 'string') {
+ normalizedFallback[locale] = value;
+ }
+ else if (Array.isArray(value) && value.every((entry) => typeof entry === 'string')) {
+ normalizedFallback[locale] = value;
+ }
+ return normalizedFallback;
+ }, {})
+ : {};
+ const allLanguageLookup = this.ensureValidResourcesDefinition(validationErrors, manifest, resources);
+ this.ensureValidFallbackDefinition(validationErrors, manifest, resources, fallback);
+ const i18nMap = this.getAllLocalesLookup(validationErrors, i18nKeys, allLanguageLookup, manifest, resources, fallback);
this.ensureAllValidI18nValue(validationErrors, i18nMap, moduleI18nProperties, manifest);
return validationErrors;
}
validateInternalI18nPropertyKeysNotInModules(manifest) {