氏名は必須
メアドはオプショナル
開始日と終了日は必須
全てのバリデーションが通ったら Subscription(name: String, mail: Option[String], interval: Interval)
というオブジェクトにする
氏名とメアドと開始日と終了日のvalidationはどれががこけても全て実施して全てのエラーを返す必要がある
case class Subscription (name : String , mail : Option [String ], interval : Interval )
val validator =
required[String ](" name" )
|@| oprional[String ](" mail" ).verifying(" mail" , " error.mailFormat" ) {_.matches(""" ^.*@.*$""" )}
|@| (
required[LocalDate ](" start" )
|@| required[LocalDate ](" end" )
apply ((_,_))
).verifying(" end" , " error.beforeStart" ) {
case (s, e) => s < e
}.map {
case (s, e) => Interval (s, e)
}
apply Subscription .apply
type FieldName = String
type Message = String
type Error = (FieldName , Message )
result: Either [Seq [Error ], Subscription ] = validator(httpRequest)
Haskell like Applicative Stype
val validator = Subscription .apply
< $> required[String ](" name" )
< * > oprional[String ](" mail" ).verifying(" mail" , " error.mailFormat" ) {_.matches(""" ^.*@.*$""" )}
< * > (
Tuple2 .apply[LocalDate , LocalDate ]
< $> required[LocalDate ](" start" )
< * > required[LocalDate ](" end" )
).verifying(" end" , " error.beforeStart" ) {
case (s, e) => s < e
}.map {
case (s, e) => Interval (s, e)
}