Skip to content

Commit

Permalink
Fixed #43
Browse files Browse the repository at this point in the history
  • Loading branch information
ozziest committed Feb 17, 2024
1 parent a0cc789 commit b54b8a8
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 9 deletions.
27 changes: 21 additions & 6 deletions src/Locale.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,25 @@ import { RuleType, LanguageType } from "./Types";

const TRANSLATIONS: Partial<Record<LanguageType, Record<string, string>>> = {};

export const setLocales = (json: any) => {
if (Array.isArray(json)) {
const locales = json as ILocale[];
export const setLocales = (values: ILocale[] | ILocale) => {
if (Array.isArray(values)) {
const locales = values as ILocale[];
for (const item of locales) {
TRANSLATIONS[item.key] = item.values;
mergeTranslations(item);
}
} else {
const locale = json as ILocale;
const locale = values as ILocale;
mergeTranslations(locale);
}
};

const mergeTranslations = (locale: ILocale) => {
if (TRANSLATIONS[locale.key]) {
TRANSLATIONS[locale.key] = {
...TRANSLATIONS[locale.key],
...locale.values,
};
} else {
TRANSLATIONS[locale.key] = locale.values;
}
};
Expand All @@ -24,6 +35,10 @@ export const addCustomLocale = (
ruleName: string,
translation: string
) => {
if (!TRANSLATIONS[locale]) {
TRANSLATIONS[locale] = {};
}

const root = TRANSLATIONS[locale];

if (root) {
Expand All @@ -37,7 +52,7 @@ export const getMessage = (
rule: RuleType,
params: any[],
language: LanguageType,
customTranslations: Record<string, string>
customTranslations: Record<string, string> = {}
) => {
const defaultTranslations = TRANSLATIONS[language];
if (defaultTranslations === undefined) {
Expand Down
5 changes: 2 additions & 3 deletions src/ruleManager.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { RULE_FUNCTION_MAPS } from "./Constants";
import { addCustomLocale, getLoadedLocales } from "./Locale";
import { addCustomLocale } from "./Locale";
import { LanguageType, RuleFunction } from "./Types";

export const DEFINED_RULES: Record<string, RuleFunction> = {
Expand All @@ -15,8 +15,7 @@ export const register = (
throw new Error(`The rule name is already defined: ${name}`);
}

const activeLocales = getLoadedLocales();
for (const locale of activeLocales) {
for (const locale of Object.keys(translations)) {
const message = translations[locale as LanguageType];
if (message === undefined) {
throw new Error(
Expand Down
31 changes: 31 additions & 0 deletions tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
register,
en,
tr,
az,
} from "../index";

const EXISTS_RULE_TRANSLATIONS = {
Expand Down Expand Up @@ -245,6 +246,36 @@ describe("validate() function ", () => {
);
});

test("should be able to register a rule before setLocales", async () => {
const isNullable = (value: any) => {
if (value === null || value === "null") {
return true;
}
return false;
};

register("nullable", isNullable, {
en: "The {0} field must be null or empty.",
tr: "The {0} field must be null or empty.",
az: "AZ: The {0} field must be null or empty!",
});

setLocales(az);

const data = {
email: "user@example.com",
};
const rules = {
email: "nullable",
};

const results = await validate(data, rules, { language: "az" });
expect(results.isValid).toBe(false);
expect(results.errors.email[0].message).toBe(
"AZ: The {0} field must be null or empty!"
);
});

test("should not be able to register the same rule twice", async () => {
expect(() =>
register("exists", () => false, EXISTS_RULE_TRANSLATIONS)
Expand Down

0 comments on commit b54b8a8

Please sign in to comment.