我需要添加字母数字字段,因为我正在尝试这段代码
object TestValidation {
implicit val readTestUser: Reads[TestValidation] = (
(JsPath \ "firstName").read(minLength[String](1)) and
(JsPath \ "lastName").read(minLength[String](1)) and
(JsPath \ "email").read(email) and
(JsPath \ "password").read(minLength[String](1)))(TestValidation.apply _)
我希望“密码”字段是字母数字我已经添加了这个自定义验证约束现在我想在 json 的 Reads 方法中集成这个可能做这样的事情
(JsPath \ "password").read(minLength[String](1)).passwordCheckConstraint
我不知道这样做的正确方法是约束代码
val allNumbers = """\d*""".r
val allLetters = """[A-Za-z]*""".r
val passwordCheckConstraint: Constraint[String] = Constraint("constraints.passwordcheck")({
plainText =>
val errors = plainText match {
case allNumbers() => Seq(ValidationError("Password is all numbers"))
case allLetters() => Seq(ValidationError("Password is all letters"))
case _ => Nil
}
if (errors.isEmpty) {
Valid
} else {
Invalid(errors)
}
})
请帮忙