3
3
import dev .ditsche .validator .error .ErrorBag ;
4
4
import dev .ditsche .validator .error .FieldNotAccessibleException ;
5
5
import dev .ditsche .validator .error .ValidationException ;
6
- import dev .ditsche .validator .rule .Rule ;
7
- import dev .ditsche .validator .rule .RuleInfo ;
8
- import dev .ditsche .validator .rule .RuleParser ;
9
6
import dev .ditsche .validator .rule .builder .Builder ;
10
7
import dev .ditsche .validator .validation .Validatable ;
11
8
import dev .ditsche .validator .validation .ValidationResult ;
@@ -32,50 +29,102 @@ public class Validator {
32
29
*/
33
30
private List <Validatable > fields ;
34
31
35
- /**
36
- * Parses a pattern and creates a Rule instance dynamically if it
37
- * is registered inside the RuleMap.
38
- */
39
- private RuleParser ruleParser ;
40
-
41
32
/**
42
33
* Create a new validator instance based on a given type.
43
34
*/
44
35
private Validator () {
45
36
this .errorBag = new ErrorBag ();
46
- this .ruleParser = new RuleParser ();
47
37
this .fields = new ArrayList <>();
48
38
}
49
39
40
+ /**
41
+ * Creates a validator from a given set of rules.
42
+ *
43
+ * @param builders The rule builders defining the fields of
44
+ * the validation object.
45
+ * @return A validator instance containing the provided rules.
46
+ */
50
47
public static Validator fromRules (Builder ...builders ) {
51
48
Validator validator = new Validator ();
52
49
for (Builder builder : builders ) {
53
- validator .add (builder );
50
+ validator .addField (builder );
54
51
}
55
52
return validator ;
56
53
}
57
54
58
- public static Validator fromRules (Validatable ...rules ) {
55
+ /**
56
+ * Creates a validator from a given set of rules.
57
+ *
58
+ * @param validatable The rule builders defining the fields of
59
+ * the validation object.
60
+ * @return A validator instance containing the provided rules.
61
+ */
62
+ public static Validator fromRules (Validatable ...validatable ) {
59
63
Validator validator = new Validator ();
60
- for (Validatable validatable : rules ) {
61
- validator .add ( validatable );
64
+ for (Validatable val : validatable ) {
65
+ validator .addField ( val );
62
66
}
63
67
return validator ;
64
68
}
65
69
70
+ /**
71
+ * Creates a new and empty Validator without any fields and rules.
72
+ *
73
+ * @return A fresh and empty validator instance.
74
+ */
66
75
public static Validator empty () {
67
76
return new Validator ();
68
77
}
69
78
70
- public Validator add (Builder builder ) {
71
- return add (builder .build ());
79
+ /**
80
+ * Adds a field and the provided rules to the validator.
81
+ * If the field already exists, the rules will be added.
82
+ *
83
+ * @param builder The rules of the field as a builder.
84
+ * @return The updated validator instance.
85
+ */
86
+ public Validator addField (Builder builder ) {
87
+ return addField (builder .build ());
72
88
}
73
89
74
- public Validator add (Validatable validatable ) {
90
+ /**
91
+ * Adds a field and the provided rules to the validator.
92
+ * If the field already exists, the rules will be added.
93
+ *
94
+ * @param validatable The rules of the field as a validatable object.
95
+ * @return The updated validator instance.
96
+ */
97
+ public Validator addField (Validatable validatable ) {
75
98
this .fields .add (validatable );
76
99
return this ;
77
100
}
78
101
102
+ /**
103
+ * Adds multiple fields and the provided rules to the validator.
104
+ * If a field already exists, the rules will be added.
105
+ *
106
+ * @param builders The rules of the fields as builders.
107
+ * @return The updated validator instance.
108
+ */
109
+ public Validator addFields (Builder ...builders ) {
110
+ for (Builder builder : builders ) {
111
+ this .addField (builder .build ());
112
+ }
113
+ return this ;
114
+ }
115
+
116
+ /**
117
+ * Adds multiple fields and the provided rules to the validator.
118
+ * If a field already exists, the rules will be added.
119
+ *
120
+ * @param validatable The rules of the fields as validatable objects.
121
+ * @return The updated validator instance.
122
+ */
123
+ public Validator addFields (Validatable ...validatable ) {
124
+ this .fields .addAll (List .of (validatable ));
125
+ return this ;
126
+ }
127
+
79
128
/**
80
129
* Validates an object against a schema and returns an error bag.
81
130
* Sets abort early to false.
0 commit comments