I'm playing with vavr library in order to handle validation.
My question is how to combine a large number of field related validation to a single one.
I mean, here my Reference class:
public class Reference {
private final ReferenceUniqueIdentifier referenceUniqueIdentifier;
private final TransactionId transactionId;
private final DocumentId documentId;
private final String field4;
private final String field5;
private final String field6;
private final String field7;
private final String field8;
private final String field9;
private final String field10;
}
I've also implemented a ReferenceValidator class like this:
public class ReferenceValidator {
public static Validation<String, ReferenceUniqueIdentifier> uniqueIdentifierNonNull(
ReferenceUniqueIdentifier referenceUniqueIdentifier) {
return Optional.ofNullable(referenceUniqueIdentifier)
.map(Validation::<String, ReferenceUniqueIdentifier>valid)
.orElse(Validation.invalid("Reference unique Identifier must be not null"));
}
public static Validation<String, TransactionId> transactionIdentifierNonNull(TransactionId transactionId) {
return Optional.ofNullable(transactionId)
.map(Validation::<String, TransactionId>valid)
.orElse(Validation.invalid("Transaction identifier must be not null"));
}
...
}
From my Reference class I've implemented a factory method like this:
public Validation<Seq<String>, Reference> of(ReferenceUniqueIdentifier referenceUniqueIdentifier,
TransactionId transactionId) {
Validation<String, ReferenceUniqueIdentifier> uniqueIdentifierNonNullValidation = ReferenceValidator
.uniqueIdentifierNonNull(referenceUniqueIdentifier);
Validation<String, TransactionId> transactionIdNonNullValidation = ReferenceValidator
.transactionIdentifierNonNull(transactionId);
//...How to combine 10 Validation<String, TField> into a single one?
}
My question is, how could I combine my 10 or more relation Validation<Error, ?> into a single Validation<Error, Reference>?
Abocve
Normally...
when combining
Validationobjects into a newValidationobject you would useValidation.combinemethod in combination with theapmethod. However, this combining allows for up to a max of8Validationobjects in one go.So for combining just 2 validations for example, you could do something like:
While in this case...
the question is about combinding more than
8validations, a bit more creativity might be required.One way could be to first combine the first 5 validations into a
Tuple5and then the next 5 validations into anotherTuple5(or some other split if that makes more sense). And after that, combine the the validatedTuple5into the desiredReferenceobject you were looking for, using a similar method as described above.Something along these lines:
Alternative
An alternative option could be to look into the constructor arguments of the
Referenceobjects to check whether some variables could be grouped into nested objects, which could bundle a few of these fields. These nested objects could then be assembled first bycombine(...).ap(..)and then be combined into the final object.For some classes it totally make sense to have some nested structure and for others it doesn't. You should check for yourself whether it's viable for this specific case.