Skip to content

Commit f265436

Browse files
committed
Add required with all rule
1 parent 26127a0 commit f265436

File tree

4 files changed

+56
-4
lines changed

4 files changed

+56
-4
lines changed

src/Validator.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import { RequiredIfRule } from './rules/RequiredIfRule';
1717
import { RequiredUnlessRule } from './rules/RequiredUnlessRule';
1818
import { RequiredWithRule } from './rules/RequiredWithRule';
1919
import { RequiredWithoutRule } from './rules/RequiredWithoutRule';
20+
import { RequiredWithAllRule } from './rules/RequiredWithAllRule';
2021

2122

2223
export class Validator {
@@ -36,7 +37,8 @@ export class Validator {
3637
required_if: new RequiredIfRule(),
3738
required_unless: new RequiredUnlessRule(),
3839
required_with: new RequiredWithRule(),
39-
required_without: new RequiredWithoutRule()
40+
required_without: new RequiredWithoutRule(),
41+
required_with_all: new RequiredWithAllRule()
4042
};
4143

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

src/locales/en.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@
1111
"required_unless": "The {field} field is required unless {other} is {value}.",
1212
"required_if": "The {field} field is required when {other} is {value}.",
1313
"required_with": "The {field} field is required when {other} is present.",
14-
"required_without": "The {field} field is required when {other} is not present."
14+
"required_without": "The {field} field is required when {other} is not present.",
15+
"required_with_all": "The {field} field is required when all of {other} are present."
1516
}

src/locales/fa.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"required_unless": "فیلد {field} الزامی است مگر اینکه {other} برابر با {value} باشد.",
66
"required_if": "فیلد {field} هنگامی که {other} برابر با {value} است، الزامی می‌باشد.",
77
"required_with": "فیلد {field} هنگامی که {other} موجود باشد، الزامی است.",
8-
"required_without": "فیلد {field} هنگامی که {other} موجود نباشد، الزامی است."
9-
}
8+
"required_without": "فیلد {field} هنگامی که {other} موجود نباشد، الزامی است.",
9+
"required_with_all": "فیلد {field} هنگامی که تمام فیلدهای {other} موجود باشند، الزامی است."
10+
}
1011

src/rules/RequiredWithAllRule.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import { ValidationRule } from './ValidationRule';
2+
3+
export class RequiredWithAllRule 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+
// params should be in format: "field1,field2,field3,..."
17+
if (typeof params !== 'string') {
18+
return 'Invalid parameters for required_with_all rule';
19+
}
20+
21+
const otherFields = params.split(',');
22+
if (otherFields.length < 1) {
23+
return 'Invalid parameters for required_with_all rule';
24+
}
25+
26+
// Check if ALL of the other fields are present (not undefined, null, or empty string)
27+
const areAllOtherFieldsPresent = otherFields.every(otherField => {
28+
const otherValue = inputs[otherField];
29+
return !(otherValue === undefined || otherValue === null || otherValue === '');
30+
});
31+
32+
// If all other fields are present, this field is required
33+
if (areAllOtherFieldsPresent) {
34+
// Check if this field is empty
35+
const isEmpty = value === undefined || value === null || value === '';
36+
37+
if (isEmpty) {
38+
// Replace placeholders in the message
39+
const msg = this.customMessage || this.defaultMessage;
40+
return msg
41+
.replace('{field}', field)
42+
.replace('{other}', otherFields.join(', '));
43+
}
44+
}
45+
46+
return null;
47+
}
48+
}

0 commit comments

Comments
 (0)