I am trying to validate a config in scala. First I convert the config json to the respective case class and then I validate it. As I want to fail slow I collect all the validations that fail rather than returning after the first validation that fails. I plan to use applicative functors provided by cats library Cats validation.
The problem I face is the form validation shown in the link works for simple case class
final case class RegistrationData(username: String, password: String, firstName: String, lastName: String, age: Int)
// Below is the code snippet for applying validation from the link itself
{
(validateUserName(username),
validatePassword(password),
validateFirstName(firstName),
validateLastName(lastName),
validateAge(age)).mapN(RegistrationData)}
// A more complex case for validations
final case class User(name:String,adds:List[Addresses])
final case class Address(street:String,lds:List[LandMark])
final case class LandMark(wellKnown:Boolean,street:String)
In this case validation on field 'username' is independent of validation on say 'firstName'. But what if the
- I had to impose some validation of kind that took both 'firstName' and 'userName' ( say hypothetically Levenstein distance of two should be <= some number ).
- case class was not made of simple primitives (Int,String) but had other case classes as its members. eg User case class as mentioned above.
In general is the approach of applicative functors suitable for this case ? Should I even collect all failed validations ?
PS: forgive me if mentioned something incorrectly, I am new to scala.