Skip to content

Commit

Permalink
feat: add lang for regex rule
Browse files Browse the repository at this point in the history
  • Loading branch information
AliN11 committed May 26, 2022
1 parent 572f4eb commit c6e2183
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/locales/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const enLang: Record<LangKeys, string> = {
[rules.MIN_LENGTH]: 'Min length is $1',
[rules.NUM_DASH]: 'Please enter numbers with dashes and underscores',
[rules.NUMBER]: 'Please enter a valid number',
[rules.REGEX]: "The value doesn't match the pattern",
[rules.REQUIRED]: 'This field is required',
[rules.STARTS_WITH]: 'The value must start with "$1"',
[rules.WITHIN]: 'The value is incorrect',
Expand Down
1 change: 1 addition & 0 deletions src/locales/fa.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const faLang: Record<LangKeys, string> = {
[rules.LESS_EQUAL]: 'لطفا یک عدد کوچکتر یا مساوی $1 وارد کنید',
[rules.MAX_LENGTH]: 'حداکثر طول مجاز این فیلد $1 است',
[rules.MIN_LENGTH]: 'حداقل طول مجاز این فیلد $1 است',
[rules.REGEX]: 'مقدار وارد شده با الگوی مشخص شده همخوانی ندارد',
[rules.REQUIRED]: 'این فیلد الزامی است',
[rules.NUM_DASH]: 'لطفاً فقط اعداد با زیرخط و خط فاصله وارد کنید',
[rules.NUMBER]: 'لطفا یک عدد معتبر وارد کنید',
Expand Down
7 changes: 4 additions & 3 deletions src/rules/regex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Rule } from '@/types';
import { RuleError } from '@/modules/rule-error';
import { when } from '@/utils/helpers';
import { ARGUMENT_MUST_BE_PROVIDED, INVALID_PATTERN } from '@/types/error-dev';
import { REGEX } from '@/types/rules';

const isValidPattern = (pattern: string) => {
try {
Expand All @@ -23,13 +24,13 @@ const stringToRegex = (str: string) => {
return new RegExp(main, options);
};

function required(value: string, pattern: string): true | RuleError {
function regex(value: string, pattern: string): true | RuleError {
when(!pattern).throwError(ARGUMENT_MUST_BE_PROVIDED);
when(isValidPattern(pattern) === false).throwError(INVALID_PATTERN);

const regExp = stringToRegex(pattern);

return regExp.test(value) || new RuleError("The value doesn't match the pattern");
return regExp.test(value) || new RuleError(REGEX);
}

export default required as Rule;
export default regex as Rule;
1 change: 0 additions & 1 deletion tests/rules/regex.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ describe('rules: regex', () => {
it('should throw error on invalid argument', () => {
expect(() => regex('...')).toThrowError(ARGUMENT_MUST_BE_PROVIDED);
expect(() => regex('...', '')).toThrowError(ARGUMENT_MUST_BE_PROVIDED);
expect(() => regex()).toThrowError(ARGUMENT_MUST_BE_PROVIDED);

// Invalid Patterns
expect(() => regex('...', '^/[a-z')).toThrowError(INVALID_PATTERN);
Expand Down

0 comments on commit c6e2183

Please sign in to comment.