Skip to content

Commit 61bf936

Browse files
committed
Add In rule
1 parent 8927c3c commit 61bf936

File tree

4 files changed

+69
-3
lines changed

4 files changed

+69
-3
lines changed

src/Validator.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import { UppercaseRule } from './rules/UppercaseRule';
2323
import { LowercaseRule } from './rules/LowercaseRule';
2424
import { JsonRule } from './rules/JsonRule';
2525
import { AlphaRule } from './rules/AlphaRule';
26+
import { InRule } from './rules/InRule';
2627

2728

2829
export class Validator {
@@ -48,7 +49,8 @@ export class Validator {
4849
uppercase: new UppercaseRule(),
4950
lowercase: new LowercaseRule(),
5051
json: new JsonRule(),
51-
alpha: new AlphaRule()
52+
alpha: new AlphaRule(),
53+
in: new InRule()
5254
};
5355

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

src/locales/en.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,6 @@
1717
"uppercase": "The {field} field must be uppercase.",
1818
"lowercase": "The {field} field must be lowercase.",
1919
"json": "The {field} field must be a valid JSON string.",
20-
"alpha": "The {field} field must contain only alphabetic characters."
20+
"alpha": "The {field} field must contain only alphabetic characters.",
21+
"in": "The {field} field must be one of: {values}."
2122
}

src/locales/fa.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"uppercase": "فیلد {field} باید با حروف بزرگ باشد.",
1212
"lowercase": "فیلد {field} باید با حروف کوچک باشد.",
1313
"json": "فیلد {field} باید یک رشته JSON معتبر باشد.",
14-
"alpha": "فیلد {field} باید فقط شامل حروف الفبا باشد."
14+
"alpha": "فیلد {field} باید فقط شامل حروف الفبا باشد.",
15+
"in": "فیلد {field} باید یکی از مقادیر {values} باشد."
1516
}
1617

src/rules/InRule.ts

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import { ValidationRule } from './ValidationRule';
2+
3+
export class InRule 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, params: 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+
// Parse parameters
22+
let allowedValues: string[] = [];
23+
let strictMode = false;
24+
25+
if (typeof params === 'string') {
26+
// Format: "value1,value2,value3" or "value1,value2,value3,strict"
27+
const paramsArray = params.split(',');
28+
29+
// Check if the last parameter is "strict"
30+
if (paramsArray.length > 0 && paramsArray[paramsArray.length - 1].trim().toLowerCase() === 'strict') {
31+
strictMode = true;
32+
allowedValues = paramsArray.slice(0, -1);
33+
} else {
34+
allowedValues = paramsArray;
35+
}
36+
} else if (Array.isArray(params)) {
37+
// Format: ["value1", "value2", "value3"] or ["value1", "value2", "value3", "strict"]
38+
if (params.length > 0 && params[params.length - 1] === 'strict') {
39+
strictMode = true;
40+
allowedValues = params.slice(0, -1).map(String);
41+
} else {
42+
allowedValues = params.map(String);
43+
}
44+
} else {
45+
return 'Invalid parameters for in rule';
46+
}
47+
48+
// Check if the value is in the allowed values list
49+
const isValid = strictMode
50+
? allowedValues.some(allowedValue => allowedValue === String(value) && typeof allowedValue === typeof value)
51+
: allowedValues.some(allowedValue => String(allowedValue) === String(value));
52+
53+
if (!isValid) {
54+
const msg = this.customMessage || this.defaultMessage;
55+
return msg
56+
.replace('{field}', field)
57+
.replace('{values}', allowedValues.join(', '));
58+
}
59+
60+
return null;
61+
}
62+
}

0 commit comments

Comments
 (0)