Created
August 10, 2016 02:45
-
-
Save roundrop/21344777eff91b6ed0e0039ce1b26eb1 to your computer and use it in GitHub Desktop.
Skinny Framework の multiParams をバリデーションする
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// tags が multiParams | |
// まず xxxxParams に Seq で入れてしまう | |
def xxxxParams: Params = { | |
val title = params.getOrElse("title", "") | |
val body = params.getOrElse("body", "") | |
val tags = multiParams.get("tags").getOrElse(Seq()).map(_.trim).distinct.filter(_.nonEmpty) | |
Params(Map("title" -> title, "body" -> body, "tags" -> tags)) | |
} | |
// tags についてはカスタムな Seq 用のバリデータ(maxLengths)を適用する | |
def xxxxForm: MapValidator = validation(xxxxParams, | |
paramKey("title") is required & maxLength(100), | |
paramKey("body") is required, | |
paramKey("tags") is maxLengths(30) | |
) | |
def xxxx = { | |
if (xxxxForm.validate()) { | |
// : | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Seq の全要素に対して skinny.validator.maxLength と同じバリデーションをする | |
// https://github.com/skinny-framework/skinny-framework/blob/master/validator/src/main/scala/skinny/validator/BuiltinValidationRules.scala | |
// BuiltinValidationRules を Seq にも対応させるといいのかもしれない | |
case class maxLengths(max: Int) extends ValidationRule { | |
def name = "maxLength" | |
override def messageParams = Seq(max.toString) | |
def isValid(s: Any): Boolean = { | |
s match { | |
case seq: Seq[String] => | |
seq.forall { v => | |
isEmpty(v) || { | |
toHasSize(v).map(x => x.size <= max) | |
.getOrElse(v.toString.length <= max) | |
} | |
} | |
case _ => | |
false | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment