Hey everyone, I'm currently trying to validate a s...
# arrow
s
Hey everyone, I'm currently trying to validate a series of sub-objects and then create a domain object out of it. What I'd like to implement is something akin to this
Copy code
personalData.validate().zip(
    Semigroup.nonEmptyList<ValidationError>(),
    taxData.validate(),
    productData.validate()
) { personData, taxData, productData -> Person(...) }
However, all of the sub-objects have a different type: •
personalData.validate() -> ValidatedNel<ValidationError, PersonalData>
taxData.validate() -> ValidatedNel<ValidationError, TaxData>
Which means that instead of the above I'm currently doing a bunch of manual checks like so:
Copy code
val validatedPersonalData = when (val result = personalData.validate()) {
    is Valid -> result.value
    is Invalid -> validationErrors.addAll(result.value)
}
Over and over again until finally
Copy code
return if (validationErrors.isNotEmpty()) 
    validationErrors.invalidNel()
else
    Person(validatedPersonalData, validatedTaxData, validatedProductData)
Is there a better way of doing this?
m
@soulbeaver did you mean we have these types?
Copy code
data class Person(val personalData: PersonalData, val taxData: TaxData, val productData: ProductData)

val validatedPersonalData: ValidatedNel<ValidationError, PersonalData> = personalData.validate()
val validatedTaxData: ValidatedNel<ValidationError, TaxData> = taxData.validate()
val validatedProductData: ValidatedNel<ValidationError, ProductData> = productData.validate()

val validatedPerson: ValidatedNel<ValidationError, Person> = ...
I wonder how come zip doesn’t work?
Copy code
val validatedPerson: ValidatedNel<ValidationError, Person> = 
  personalData.validate().zip(
    taxData.validate(),
    productData.validate()
  ) { personData, taxData, productData -> 
    Person(personData, taxData, productData)
  }
s
Oh geez, now I feel really silly. It works exactly as you described, I got tripped up because I never bothered to finish the zip statement
Copy code
personalData.validate().zip(
    Semigroup.nonEmptyList<ValidationError>(),
    taxData.validate(),
    productData.validate()
)
I saw a bunch of errors relating to TypeVariable<A> and such in the code and assumed it was because of the ValidatedNel response types I had declared. Of course it was complaining that I was missing the final lambda parameter.
I'm sorry for taking up your time and thank you for the help!
m
Ahh awesome! 🙌