Validation on more than 8 fields

55 views
Skip to first unread message

Jan Bols

unread,
Sep 7, 2015, 10:18:39 AM9/7/15
to Functional Java
Hi,

I have a dto called UnvalidatedPerson that needs to be validated and turned into a valid ValidatedPerson value object:

class UnvalidatedPerson {
 
String id
 
String firstName
 
String lastName
 
String middleName
 
String address
 
... 7 more fields
}


class ValidatedPerson{
 
PersonId id
 
String firstName
 
String lastName
 
String middleName
 
Address address
 
... 7 more fields
}

I thought about using fj's Validation object for that: create Validation objects for all fields like ...
  • Validation<Error, PersonId> idValidation
  • Validation<Error, String> firstName
  • Validation<Error, String> lastName
  • ...
.. and finally accumulate them and return Validation<Error, ValidatedPerson> using some function F12<PersonId, String, String, ..., ValidatedPerson>

The problem is, there's more than 8 fields and the accumulate method that takes 8 validations maximum (and functions only go till F8).

How do you handle this - I assume - classic problem? Do you not have classes with more than 8 fields that need to be validated? Do you split them up into products? Am I not using Validation correctly?

Best regards
Jan

jbgir...@gmail.com

unread,
Sep 7, 2015, 12:33:46 PM9/7/15
to functio...@googlegroups.com
not really answering to the problem at hand, but I do think that grouping fields by concerns is good design. In your case, firstName, lastName, middleName look like good candidates to be grouped together in a product type. You can also use lenses to abstract over the fact that those fields are now in a nested structure.

Jb

--
You received this message because you are subscribed to the Google Groups "Functional Java" group.
To unsubscribe from this group and stop receiving emails from it, send an email to functionaljav...@googlegroups.com.
To post to this group, send email to functio...@googlegroups.com.
Visit this group at http://groups.google.com/group/functionaljava.
For more options, visit https://groups.google.com/d/optout.

Clinton Selke

unread,
Sep 9, 2015, 7:28:09 PM9/9/15
to Functional Java
You can use accumulate for the first 8 parameters, have the function parameter return a curried function for the rest of the parameters. Then use accumapply to apply the rest one by one.
Unfortunately the rest of the parameters (after the 8) get applied backwards in the code (from right to left, instead of left to right).

Jan Bols

unread,
Sep 22, 2015, 4:45:09 AM9/22/15
to Functional Java
Thanks all for answering.

I wasn't (and my team certainly wasn't) ready yet for introducing an extra concept like lenses. I'm taking baby steps into the world of FP. 

So I ended up mapping all successful validations to some builder object, called PartialPerson, sequencing all validations and finally creating my ValidatedPerson by merging all those builders like here:

Validation<Error, PartialPerson> partialPersonIdValidation = idValidation.map(persionIdToPartialPerson)
Validation<Error, PartialPerson> partialPersonFirstNameValidation = firstNameValidation.map(firstNameToPartialPerson)
Validation<Error, PartialPerson> partialPersonLastNameValidation = lastNameValidation.map(lastNameToPartialPerson)
...
Validation<Error, List<PartialPerson>> sequencedValidations = Validation.sequence(errorSemiGroup, list(partialPersonIdValidation , partialPersonFirstNameValidation , partialPersonLastNameValidation, ...)
Validation<Error, ValidatedPerson> endValidation = sequencedValidations.map(partialPersonsToValidatedPerson)

It's a bit of an overhead to define the partialPerson object but I guess defining lenses are too.
I'll try lenses later. 
greetings
Jan


Op maandag 7 september 2015 18:33:46 UTC+2 schreef Jean-Baptiste Giraudeau:
Reply all
Reply to author
Forward
0 new messages