@scayle/storefront-nuxt
Version:
Nuxt integration for the SCAYLE Commerce Engine and Storefront API
69 lines (68 loc) • 1.79 kB
JavaScript
export const validateDomainConfig = (shops, i18nConfig) => {
const errors = [];
const createError = (type, shopId, locale, message, domain) => ({
type,
shopId,
locale,
message,
...domain && { domain }
});
for (const [shopId, shop] of Object.entries(shops)) {
const numericShopId = Number(shopId);
if (!shop.domain) {
errors.push(
createError(
"missing_domain",
numericShopId,
shop.locale,
"Domain is required in shop config"
)
);
continue;
}
if (!Object.values(i18nConfig.domainLocales).some(
({ domain }) => domain === shop.domain
)) {
errors.push(
createError(
"missing_domain",
numericShopId,
shop.locale,
`'${shop.domain}' was not found in i18n config`,
shop.domain
)
);
}
}
const seenDomains = /* @__PURE__ */ new Set();
for (const [locale, config] of Object.entries(i18nConfig.domainLocales)) {
if (!config?.domain) {
continue;
}
if (!Object.values(shops).some((shop) => shop.domain === config.domain)) {
errors.push(
createError(
"missing_domain",
void 0,
locale,
`'${config.domain}' is configured in i18nConfig but not in shop config`
)
);
}
if (seenDomains.has(config?.domain)) {
const shop = Object.values(shops).find((shop2) => {
return shop2.domain === config.domain;
});
errors.push(
createError(
"duplicate_domain",
Number(shop?.shopId),
locale,
`Duplicate domain found in i18nConfig: ${config.domain}`
)
);
}
seenDomains.add(config.domain);
}
return errors;
};