Skip to content

Commit ca684a3

Browse files
committed
refactor: validation structure for checkbox
1 parent b609d5f commit ca684a3

10 files changed

Lines changed: 46 additions & 27 deletions

File tree

source/js/conditions/condition/andCondition.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,19 @@ class AndCondition implements ConditionInterface {
33
}
44

55
public validate(): boolean {
6+
let isValid: boolean[] = [];
7+
this.conditions.forEach(condition => {
8+
if (!condition.class) {
9+
return;
10+
}
11+
12+
isValid.push(condition.class.getConditionValidator().validate(condition));
13+
});
14+
15+
if (isValid.includes(false)) {
16+
return false;
17+
}
18+
619
return true;
720
}
821

source/js/conditions/condition/orCondition.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ class OrCondition implements ConditionInterface {
33
}
44

55
public validate(): boolean {
6-
// Implement validation logic
7-
return true;
6+
return this.condition.class?.getConditionValidator().validate(this.condition) ?? true;
87
}
98

109
public getConditions(): Condition[] {

source/js/conditions/conditionsInterface.d.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,6 @@ interface ConditionInterface {
88
getConditionFieldNames(): string[];
99
}
1010

11-
interface ConditionValidatorInterface {
12-
validate(condition: Condition): boolean;
13-
}
14-
1511
type Condition = {
1612
field: string;
1713
class: FieldInterface|null;

source/js/conditions/validate/checkboxValidator.ts

Lines changed: 0 additions & 13 deletions
This file was deleted.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class CheckboxConditionValidator implements ConditionValidatorInterface {
2+
public validate(condition: Condition): boolean {
3+
4+
return false;
5+
}
6+
}
7+
8+
export default CheckboxConditionValidator;

source/js/fields/field/checkbox/checkboxConditionsHandler.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,6 @@ class CheckboxConditionsHandler implements ConditionsHandlerInterface {
1212
this.setValueChangeListener();
1313
}
1414

15-
public validateCondition(conditions: Condition[]): boolean {
16-
console.log(conditions);
17-
return true;
18-
}
19-
2015
public validate(): boolean {
2116
this.getConditions().forEach((condition) => {
2217
condition.validate();

source/js/fields/field/nullField/nullField.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ class NullField implements FieldInterface {
33
private field: HTMLElement,
44
type: string,
55
private name: string,
6-
private conditionsHandler: ConditionsHandlerInterface
6+
private nullFieldConditionValidator: ConditionValidatorInterface,
7+
private conditionsHandler: ConditionsHandlerInterface,
8+
79
) {
810
console.error(`Field type "${type}" is not implemented.`);
911
}
@@ -23,6 +25,10 @@ class NullField implements FieldInterface {
2325
public getConditionsHandler(): ConditionsHandlerInterface {
2426
return this.conditionsHandler;
2527
}
28+
29+
public getConditionValidator(): ConditionValidatorInterface {
30+
return this.nullFieldConditionValidator;
31+
}
2632
}
2733

2834
export default NullField;
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class NullFieldConditionValidator implements ConditionValidatorInterface {
2+
public validate(condition: Condition): boolean {
3+
4+
return false;
5+
}
6+
}
7+
8+
export default NullFieldConditionValidator;

source/js/fields/fieldBuilder.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import Checkbox from "./field/checkbox/checkbox";
22
import CheckboxConditionsHandler from "./field/checkbox/checkboxConditionsHandler";
3+
import CheckboxConditionValidator from "./field/checkbox/checkboxConditionValidator";
34
import NullFieldConditionsHandler from "./field/nullField/nullFieldConditionsHandler";
45
import NullField from "./field/nullField/nullField";
5-
import CheckboxValidator from "../conditions/validate/checkboxValidator";
6+
import NullFieldConditionValidator from "./field/nullField/nullFieldConditionValidator";
67

78
class FieldBuilder implements FieldBuilderInterface {
89
private name: string = 'data-js-field-name';
@@ -27,6 +28,7 @@ class FieldBuilder implements FieldBuilderInterface {
2728
field,
2829
type,
2930
this.getFieldName(field),
31+
new NullFieldConditionValidator(),
3032
new NullFieldConditionsHandler(field, this.getFieldCondition(field))
3133
);
3234
}
@@ -38,7 +40,7 @@ class FieldBuilder implements FieldBuilderInterface {
3840
field,
3941
choices,
4042
this.getFieldName(field),
41-
new CheckboxValidator(),
43+
new CheckboxConditionValidator(),
4244
new CheckboxConditionsHandler(this.getFieldCondition(field))
4345
);
4446
}

source/js/fields/fieldsInterface.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ interface FieldInterface {
66
init(conditionBuilder: ConditionBuilderInterface): void;
77
getName(): string;
88
getConditionsHandler(): ConditionsHandlerInterface;
9+
getConditionValidator(): ConditionValidatorInterface;
910
}
1011

1112
interface CheckboxInterface extends FieldInterface {
@@ -24,6 +25,10 @@ interface ConditionsHandlerInterface {
2425
addValueChangeListener(field: FieldInterface): void;
2526
}
2627

28+
interface ConditionValidatorInterface {
29+
validate(condition: Condition): boolean;
30+
}
31+
2732
type FieldsObject = {
2833
[key: string]: FieldInterface;
2934
}

0 commit comments

Comments
 (0)