@@ -100,11 +100,12 @@ public Validator<T> addField(String field, String rulesString) {
100
100
101
101
/**
102
102
* Validates an object against a schema and returns an error bag.
103
- *
103
+ * @deprecated since 1.0.4
104
104
* @param object The object that need to be validated.
105
105
* @throws ValidationException Thrown when at least one rule fails.
106
106
* @throws IllegalAccessException Thrown when the field is not public.
107
107
*/
108
+ @ Deprecated
108
109
public void validate (T object ) throws ValidationException , IllegalAccessException {
109
110
errorBag .clear ();
110
111
List <Field > fieldSet = new ArrayList <>();
@@ -125,6 +126,33 @@ public void validate(T object) throws ValidationException, IllegalAccessExceptio
125
126
throw new ValidationException (errorBag );
126
127
}
127
128
129
+ /**
130
+ * Validates an object against a schema and returns an error bag.
131
+ *
132
+ * @param object The object that need to be validated.
133
+ * @throws ValidationException Thrown when at least one rule fails.
134
+ * @throws IllegalAccessException Thrown when the field is not public.
135
+ */
136
+ public void tryValidate (T object ) throws ValidationException , IllegalAccessException {
137
+ errorBag .clear ();
138
+ List <Field > fieldSet = new ArrayList <>();
139
+ for (Class <?> c = object .getClass (); c != null ; c = c .getSuperclass ())
140
+ {
141
+ Field [] fields = c .getDeclaredFields ();
142
+ fieldSet .addAll (Arrays .asList (fields ));
143
+ }
144
+ for (ValidationField vf : this .fields ) {
145
+ Field field = fieldSet .stream ().filter (f -> f .getName ().equals (vf .getField ())).findFirst ().orElse (null );
146
+ if (field == null ) continue ;
147
+ Object value = getValue (field , object );
148
+ for (Rule rule : vf .getRules ()) {
149
+ if (!rule .passes (value )) errorBag .add (vf .getField (), rule .message (vf .getField ()));
150
+ }
151
+ }
152
+ if (!errorBag .isEmpty ())
153
+ throw new ValidationException (errorBag );
154
+ }
155
+
128
156
/**
129
157
* Uses reflection to invoke a getter of the validation target.
130
158
* Falls back to the fields default getter. Will fail if the variable
0 commit comments