Skip to content

Commit 8927c3c

Browse files
committed
Add Alpha rule
1 parent c1b7c33 commit 8927c3c

File tree

4 files changed

+42
-3
lines changed

4 files changed

+42
-3
lines changed

src/Validator.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import { RequiredWithoutAllRule } from './rules/RequiredWithoutAllRule';
2222
import { UppercaseRule } from './rules/UppercaseRule';
2323
import { LowercaseRule } from './rules/LowercaseRule';
2424
import { JsonRule } from './rules/JsonRule';
25+
import { AlphaRule } from './rules/AlphaRule';
2526

2627

2728
export class Validator {
@@ -46,7 +47,8 @@ export class Validator {
4647
required_without_all: new RequiredWithoutAllRule(),
4748
uppercase: new UppercaseRule(),
4849
lowercase: new LowercaseRule(),
49-
json: new JsonRule()
50+
json: new JsonRule(),
51+
alpha: new AlphaRule()
5052
};
5153

5254
constructor(private locale: string = 'en') {}

src/locales/en.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,6 @@
1616
"required_without_all": "The {field} field is required when none of {other} are present.",
1717
"uppercase": "The {field} field must be uppercase.",
1818
"lowercase": "The {field} field must be lowercase.",
19-
"json": "The {field} field must be a valid JSON string."
19+
"json": "The {field} field must be a valid JSON string.",
20+
"alpha": "The {field} field must contain only alphabetic characters."
2021
}

src/locales/fa.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"required_without_all": "فیلد {field} هنگامی که هیچ یک از فیلدهای {other} موجود نباشند، الزامی است.",
1111
"uppercase": "فیلد {field} باید با حروف بزرگ باشد.",
1212
"lowercase": "فیلد {field} باید با حروف کوچک باشد.",
13-
"json": "فیلد {field} باید یک رشته JSON معتبر باشد."
13+
"json": "فیلد {field} باید یک رشته JSON معتبر باشد.",
14+
"alpha": "فیلد {field} باید فقط شامل حروف الفبا باشد."
1415
}
1516

src/rules/AlphaRule.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { ValidationRule } from './ValidationRule';
2+
3+
export class AlphaRule implements ValidationRule {
4+
private defaultMessage = '';
5+
private customMessage?: string;
6+
7+
setDefaultMessage(message: string) {
8+
this.defaultMessage = message;
9+
}
10+
11+
setCustomMessage(message: string) {
12+
this.customMessage = message;
13+
}
14+
15+
validate(value: any, ruleValue: any, field: string, inputs: Record<string, any>): string | null {
16+
// Skip validation if value is empty
17+
if (value === undefined || value === null || value === '') {
18+
return null;
19+
}
20+
21+
// Convert value to string if it's not already
22+
const stringValue = String(value);
23+
24+
// Check if the string contains only alphabetic characters
25+
// Using a regular expression to match only letters (a-z, A-Z)
26+
const alphaRegex = /^[a-zA-Z]+$/;
27+
28+
if (!alphaRegex.test(stringValue)) {
29+
const msg = this.customMessage || this.defaultMessage;
30+
return msg.replace('{field}', field);
31+
}
32+
33+
return null;
34+
}
35+
}

0 commit comments

Comments
 (0)