Commit 62452d2a authored by Bhargavi Ghanta's avatar Bhargavi Ghanta

Update User, Validator, and ValidationException classes

parent 69f76ef2
package PACKAGE_NAME;
public record User(String name, String email, String password) {
public class User {
}
package PACKAGE_NAME;
import java.util.List;
public class ValidationException {
public class ValidationException extends RuntimeException {
private final List<String> errors;
public ValidationException(List<String> errors) {
super("Validation failed with " + errors.size() + " error(s)");
this.errors = errors;
}
public List<String> getErrors() {
return errors;
}
}
package PACKAGE_NAME;
import java.util.ArrayList;
import java.util.List;
public class Validator<T> {
private final List<ValidationRule<T>> rules = new ArrayList<>();
public Validator<T> addRule(ValidationRule<T> rule) {
rules.add(rule);
return this;
}
public void validate(T object) {
List<String> errors = new ArrayList<>();
for (ValidationRule<T> rule : rules) {
String error = rule.validate(object);
if (error != null) {
errors.add(error);
}
}
if (!errors.isEmpty()) {
throw new ValidationException(errors);
}
}
}
public class Validator {
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment