Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- trait Validable[R <: BrewerRecord]
- {
- val validator: RecordValidator
- def newFromValidator(v: Command with ValidationSupport): R
- trait Validated {
- type RV = RecordValidator
- val validated: RV
- trait ValidatedOrElse {
- def |[A](f:RV => A)
- }
- def fold[A](f:RV => A)(g:RV => A) = (validated.fieldErrors.size > 0).
- fold(f(validated), g(validated))
- def ?[A](f:RV => A) = new ValidatedOrElse {
- def |(g:RV => A):A = fold(f)(g)
- }
- }
- def apply(params:Params) = new Validated {
- val validated = validator.doBinding(params)
- }
- }
- trait BrewerValidator extends Command with ValidationSupport {
- def notNull[T]: FieldValidator[T] = {
- case None => RejectField[T](None, "Field can't be null")
- }
- }
- object Client {
- implicit val clientValidable: Validable[Client] = new Validable[Client] {
- type ClientValidator = BrewerValidator
- with BrewerPasswordValidator
- with BrewerEmailValidator
- val validator = new BrewerValidator
- with BrewerPasswordValidator
- with BrewerEmailValidator {
- import Command._
- val userEmail = bind[String]("umail") validate
- (notNull[String] orElse isEmail)
- val userPassword = bind[String]("upw") validate
- (notNull[String] orElse securePassword)
- }
- def newFromValidator(v:ClientValidator):Client = Client(
- userName = v.userEmail.value.get,
- userPassword = v.userPassword.value.get)
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment