scala-hamsters / hamsters

A mini Scala utility library
Apache License 2.0
291 stars 23 forks source link

Build Validation from List #21

Closed Fabszn closed 7 years ago

Fabszn commented 7 years ago

Hello,

Is there a reason why we can not build a Validation from list through apply() method as

Validation(List(Ok, KO, KO))

Currently, I have to use Traditional approach with the new operator. We could add the apply(l:List) in companion object of Validation class, what do you think about it ?

Many thanks for you help,

Fabrice

dgouyette commented 7 years ago

Did you take a look to unit tests ?

https://github.com/scala-hamsters/hamsters/blob/master/src/test/scala/ValidationSpec.scala

You can do something like


    val e1 = OK(1)
    val e2 = KO("nan")
    val e3 = KO("nan2")

    val validation = Validation(e1, e2, e3)
    val failures: List[String] = validation.failures
    validation.hasFailures should be(true)
    failures should be(List("nan", "nan2"))
  }
Fabszn commented 7 years ago

Many thanks Damien for your answer. My use case is little bit different. I have one list of element as List(E1, E2, E3, etc) I map my list to validate each element then I have one list of OK(E1)/KO(Error) And now I want to know which are right and which are not right In my case, I didn't know the exact number of element I have to validate

But my use case may be doen't fit with the Validation type finally ? What do you think about it?

dgouyette commented 7 years ago

Can you provide a unit test please ?

Fabszn commented 7 years ago

Sure, I will post it tonight.

loicdescotte commented 7 years ago

Hi @Fabszn and @dgouyette ,

I think you can use the ":_* " syntax, that converts a list to varargs :

val eithers = List(Ok, KO, KO)
val validation = Validation(eithers :_*)
Fabszn commented 7 years ago

Aaah yes!! You right! :+1: I forgot this operator and it fully answer to my need

Many thanks!