Skip to content

Commit c1b7c33

Browse files
committed
Add Json rule
1 parent 9c326fa commit c1b7c33

File tree

4 files changed

+40
-3
lines changed

4 files changed

+40
-3
lines changed

src/Validator.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import { RequiredWithAllRule } from './rules/RequiredWithAllRule';
2121
import { RequiredWithoutAllRule } from './rules/RequiredWithoutAllRule';
2222
import { UppercaseRule } from './rules/UppercaseRule';
2323
import { LowercaseRule } from './rules/LowercaseRule';
24+
import { JsonRule } from './rules/JsonRule';
2425

2526

2627
export class Validator {
@@ -44,7 +45,8 @@ export class Validator {
4445
required_with_all: new RequiredWithAllRule(),
4546
required_without_all: new RequiredWithoutAllRule(),
4647
uppercase: new UppercaseRule(),
47-
lowercase: new LowercaseRule()
48+
lowercase: new LowercaseRule(),
49+
json: new JsonRule()
4850
};
4951

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

src/locales/en.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,6 @@
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.",
1717
"uppercase": "The {field} field must be uppercase.",
18-
"lowercase": "The {field} field must be lowercase."
18+
"lowercase": "The {field} field must be lowercase.",
19+
"json": "The {field} field must be a valid JSON string."
1920
}

src/locales/fa.json

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

src/rules/JsonRule.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { ValidationRule } from './ValidationRule';
2+
3+
export class JsonRule 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+
// Try to parse the string as JSON
25+
try {
26+
JSON.parse(stringValue);
27+
return null; // Valid JSON
28+
} catch (e) {
29+
const msg = this.customMessage || this.defaultMessage;
30+
return msg.replace('{field}', field);
31+
}
32+
}
33+
}

0 commit comments

Comments
 (0)