Skip to content

Commit 9c326fa

Browse files
committed
Add lowercase rule
1 parent ae57e7b commit 9c326fa

File tree

4 files changed

+41
-3
lines changed

4 files changed

+41
-3
lines changed

src/Validator.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import { RequiredWithoutRule } from './rules/RequiredWithoutRule';
2020
import { RequiredWithAllRule } from './rules/RequiredWithAllRule';
2121
import { RequiredWithoutAllRule } from './rules/RequiredWithoutAllRule';
2222
import { UppercaseRule } from './rules/UppercaseRule';
23+
import { LowercaseRule } from './rules/LowercaseRule';
2324

2425

2526
export class Validator {
@@ -42,7 +43,8 @@ export class Validator {
4243
required_without: new RequiredWithoutRule(),
4344
required_with_all: new RequiredWithAllRule(),
4445
required_without_all: new RequiredWithoutAllRule(),
45-
uppercase: new UppercaseRule()
46+
uppercase: new UppercaseRule(),
47+
lowercase: new LowercaseRule()
4648
};
4749

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

src/locales/en.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,6 @@
1414
"required_without": "The {field} field is required when {other} is not present.",
1515
"required_with_all": "The {field} field is required when all of {other} are present.",
1616
"required_without_all": "The {field} field is required when none of {other} are present.",
17-
"uppercase": "The {field} field must be uppercase."
17+
"uppercase": "The {field} field must be uppercase.",
18+
"lowercase": "The {field} field must be lowercase."
1819
}

src/locales/fa.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"required_without": "فیلد {field} هنگامی که {other} موجود نباشد، الزامی است.",
99
"required_with_all": "فیلد {field} هنگامی که تمام فیلدهای {other} موجود باشند، الزامی است.",
1010
"required_without_all": "فیلد {field} هنگامی که هیچ یک از فیلدهای {other} موجود نباشند، الزامی است.",
11-
"uppercase": "فیلد {field} باید با حروف بزرگ باشد."
11+
"uppercase": "فیلد {field} باید با حروف بزرگ باشد.",
12+
"lowercase": "فیلد {field} باید با حروف کوچک باشد."
1213
}
1314

src/rules/LowercaseRule.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { ValidationRule } from './ValidationRule';
2+
3+
export class LowercaseRule 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 is lowercase
25+
// A string is lowercase if it's equal to its lowercase version
26+
// and not equal to its uppercase version (to ensure it has at least one character)
27+
if (stringValue !== stringValue.toLowerCase() || stringValue === stringValue.toUpperCase()) {
28+
const msg = this.customMessage || this.defaultMessage;
29+
return msg.replace('{field}', field);
30+
}
31+
32+
return null;
33+
}
34+
}

0 commit comments

Comments
 (0)