Skip to content

Commit 72b9982

Browse files
committed
Add Not in rule
1 parent 61bf936 commit 72b9982

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
@@ -24,6 +24,7 @@ import { LowercaseRule } from './rules/LowercaseRule';
2424
import { JsonRule } from './rules/JsonRule';
2525
import { AlphaRule } from './rules/AlphaRule';
2626
import { InRule } from './rules/InRule';
27+
import { NotInRule } from './rules/NotInRule';
2728

2829

2930
export class Validator {
@@ -50,7 +51,8 @@ export class Validator {
5051
lowercase: new LowercaseRule(),
5152
json: new JsonRule(),
5253
alpha: new AlphaRule(),
53-
in: new InRule()
54+
in: new InRule(),
55+
not_in: new NotInRule()
5456
};
5557

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

src/locales/en.json

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

src/locales/fa.json

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

src/rules/NotInRule.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 NotInRule 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 disallowedValues: 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+
disallowedValues = paramsArray.slice(0, -1);
33+
} else {
34+
disallowedValues = 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+
disallowedValues = params.slice(0, -1).map(String);
41+
} else {
42+
disallowedValues = params.map(String);
43+
}
44+
} else {
45+
return 'Invalid parameters for not_in rule';
46+
}
47+
48+
// Check if the value is NOT in the disallowed values list
49+
const isInvalid = strictMode
50+
? disallowedValues.some(disallowedValue => disallowedValue === String(value) && typeof disallowedValue === typeof value)
51+
: disallowedValues.some(disallowedValue => String(disallowedValue) === String(value));
52+
53+
if (isInvalid) {
54+
const msg = this.customMessage || this.defaultMessage;
55+
return msg
56+
.replace('{field}', field)
57+
.replace('{values}', disallowedValues.join(', '));
58+
}
59+
60+
return null;
61+
}
62+
}

0 commit comments

Comments
 (0)