在Scala中对输入值运行多个验证规则的正确方法?
在Scala中验证输入值的正确方法是什么?假设我有一个密码要用以下规则验证在Scala中对输入值运行多个验证规则的正确方法?,scala,Scala,在Scala中验证输入值的正确方法是什么?假设我有一个密码要用以下规则验证 密码不应为空 密码长度应大于8个字符 可以有更多的验证规则。但目标是运行所有规则并返回错误列表(list[String]) 以下是我能想到的两种方法。但是有更好的办法吗 代码 import scala.collection.mutable.ListBuffer //Validate password using mutable list (Feels like Java style coding) //Uses mut
list[String]
)
以下是我能想到的两种方法。但是有更好的办法吗
代码
import scala.collection.mutable.ListBuffer
//Validate password using mutable list (Feels like Java style coding)
//Uses mutable list but seems a bit simpler
def validatePasswordMutableList(password: String): List[String] = {
val errors = ListBuffer.empty[String]
if (password == "")
errors += "Is empty."
if (password.length < 8)
errors += "Not long enough."
errors.toList
}
validatePasswordMutableList("") foreach println
//Validate password using Immutable list
//But has to run filter and map on the error list to get desired result.
def validatePasswordImmutableList(password: String): List[String] = {
def isEmpty = if (password == "") Some("Is empty.") else None
def isLengthValid = if (password.length < 8) Some("Not long enough.") else None
val errors = isEmpty :: isLengthValid :: Nil
errors.filter(_.isDefined).map(_.get)
}
validatePasswordImmutableList("") foreach println
导入scala.collection.mutable.ListBuffer
//使用可变列表验证密码(感觉像Java风格的编码)
//使用可变列表,但似乎更简单
def validatePasswordMutableList(密码:字符串):列表[字符串]={
val errors=ListBuffer.empty[字符串]
如果(密码==“”)
错误+=“为空。”
如果(密码长度<8)
错误+=“时间不够长。”
托利斯特酒店
}
每个println的validatePasswordMutableList(“”)
//使用不可变列表验证密码
//但必须在错误列表上运行过滤器和映射才能获得所需的结果。
def validatePasswordImmutableList(密码:字符串):列表[字符串]={
def isEmpty=如果(密码==“”)有些(“为空”)其他则无
def isLengthValid=如果(password.length<8)有一些(“不够长”),则没有
val errors=isEmpty::isLengthValid::Nil
errors.filter(u.isDefined).map(u.get)
}
每个println的validatePasswordImmutableList(“”)
<>代码> 一种方法,你应该考虑处理一个无限的动态规则选择,作为函数的列表(或序列)传递。通过这种方式,您可以回收部分或全部这些规则,并在将来轻松更新新规则
使用只返回布尔值的函数会简单得多,但是下面的代码有一个优点,即返回所有密码冲突,这与现在大多数网站和应用程序使用的方法类似
object ValidateExample extends App {
def applyFilters(password:String, rules:Seq[Function[String,Option[String]]]):Seq[String] = {
//You could use a while loop here if you had many rules, and wanted to stop early
val temp = rules.flatMap{f=> f(password) } //seq of items that converted to Some(string) only
temp
}
val rule1:(String)=>Option[String] = { s=>
if (s.length<8) Some("Error in password length") else None
}
//same idea different declaration syntax
val rule2:Function[String,Option[String]] = { s=>
if (s.contains("X")) Some("Error cant contain capitol X") else None
}
//example of a partial function, lifted below
val rule3:PartialFunction[String,String]={case s:String if s.isEmpty =>"Password can't be empty"}
val test1 = applyFilters("helloX",Seq(rule1,rule2,rule3.lift))
println(test1)
val test2 = applyFilters("",Seq(rule1,rule3.lift))
println(test2)
//example passed
val passed = test2.isEmpty
}
你应该考虑的一种方法是处理无限的动态规则选择,作为函数的列表(或序列)传递。通过这种方式,您可以回收部分或全部这些规则,并在将来轻松更新新规则
使用只返回布尔值的函数会简单得多,但是下面的代码有一个优点,即返回所有密码冲突,这与现在大多数网站和应用程序使用的方法类似
object ValidateExample extends App {
def applyFilters(password:String, rules:Seq[Function[String,Option[String]]]):Seq[String] = {
//You could use a while loop here if you had many rules, and wanted to stop early
val temp = rules.flatMap{f=> f(password) } //seq of items that converted to Some(string) only
temp
}
val rule1:(String)=>Option[String] = { s=>
if (s.length<8) Some("Error in password length") else None
}
//same idea different declaration syntax
val rule2:Function[String,Option[String]] = { s=>
if (s.contains("X")) Some("Error cant contain capitol X") else None
}
//example of a partial function, lifted below
val rule3:PartialFunction[String,String]={case s:String if s.isEmpty =>"Password can't be empty"}
val test1 = applyFilters("helloX",Seq(rule1,rule2,rule3.lift))
println(test1)
val test2 = applyFilters("",Seq(rule1,rule3.lift))
println(test2)
//example passed
val passed = test2.isEmpty
}
在此上下文中定义“更好”,因为它是一个相对术语。我所说的更好是指构建结果错误:以更有效的方式列出[String],而不使用可变列表或多次迭代列表。在空间或时间上更有效?尝试错误。展平而不是错误。过滤器(u.isDefined)。映射(u.get)
@captain it is,但是有一个从选项
的隐式转换。在这个上下文中定义“better”,因为它是一个相对术语。better是指构建结果错误:List[String]在不使用可变列表或多次迭代列表的情况下,以更高效的方式。在空间或时间上更高效?尝试错误。展平而不是错误。筛选(u.isDefined)。映射(u.get)
@captain它是,但是有一个来自选项的隐式转换。