Prototype of validation library with fluent interface
Simple property validator:
var validator = FluentValidator.<Customer>validator()
.property(Customer::name).notNull().notEmpty()
.property(Customer::age).greaterThan(18);Inner objects property validator:
var validator = FluentValidator.<Account>validator()
.property(Account::amount).greaterThan(200)
.property(Account::customer,
customerValidator -> customerValidator.property(Customer::name).notEmpty());Creating custom validator with context in spring
@Component
@RequiredArgsConstructor
public class AccountAlreadyExistsValidator {
private final AccountRepository repository;
public ValidationResult validate(CreateAccountCommand command) {
boolean isExists = repository.existsById(command.getAccountId());
if (isExists) {
return ValidationResult.error("Account already exists");
} else {
return ValidationResult.VALID_RESULT;
}
}
}@Component
public class AccountValidator {
private final FluentValidator<CreateAccountCommand> validator;
public AccountValidator(AccountAlreadyExistsValidator accountAlreadyExistsValidator) {
this.validator = FluentValidator.<CreateAccountCommand>validator()
.property(CreateAccountCommand::getAccountId).notNull()
.addValidator(command -> accountAlreadyExistsValidator.validate(command));
}
public ValidationResult validate(CreateAccountCommand command) {
return validator.validate(command);
}
}