Skip to content

Commit f6b57bb

Browse files
committed
Add uppercase rule
1 parent adfd02d commit f6b57bb

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
@@ -19,6 +19,7 @@ import { RequiredWithRule } from './rules/RequiredWithRule';
1919
import { RequiredWithoutRule } from './rules/RequiredWithoutRule';
2020
import { RequiredWithAllRule } from './rules/RequiredWithAllRule';
2121
import { RequiredWithoutAllRule } from './rules/RequiredWithoutAllRule';
22+
import { UppercaseRule } from './rules/UppercaseRule';
2223

2324

2425
export class Validator {
@@ -40,7 +41,8 @@ export class Validator {
4041
required_with: new RequiredWithRule(),
4142
required_without: new RequiredWithoutRule(),
4243
required_with_all: new RequiredWithAllRule(),
43-
required_without_all: new RequiredWithoutAllRule()
44+
required_without_all: new RequiredWithoutAllRule(),
45+
uppercase: new UppercaseRule()
4446
};
4547

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

src/locales/en.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,6 @@
1313
"required_with": "The {field} field is required when {other} is present.",
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.",
16-
"required_without_all": "The {field} field is required when none of {other} are present."
16+
"required_without_all": "The {field} field is required when none of {other} are present.",
17+
"uppercase": "The {field} field must be uppercase."
1718
}

src/locales/fa.json

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

src/rules/UppercaseRule.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 UppercaseRule 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 uppercase
25+
// A string is uppercase if it's equal to its uppercase version
26+
// and not equal to its lowercase version (to ensure it has at least one character)
27+
if (stringValue !== stringValue.toUpperCase() || stringValue === stringValue.toLowerCase()) {
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)