All Downloads are FREE. Search and download functionalities are using the official Maven repository.

.circumflex-orm.2.0.1.source-code.validation.scala Maven / Gradle / Ivy

There is a newer version: 2.5
Show newest version
package ru.circumflex.orm

import ru.circumflex.core._

class ValidationException(val errors: MsgGroup)
    extends ORMException("Record validation failed.")

class RecordValidator[PK, R <: Record[PK, R]] {

  protected var _validators: Seq[R => Option[Msg]] = Nil

  def validators = _validators

  def validate(record: R): Seq[Msg] =
    _validators.flatMap(_.apply(record)).toList.distinct

  def add(validator: R => Option[Msg]): this.type = {
    _validators ++= List(validator)
    return this
  }

  def addForTransient(validator: R => Option[Msg]): this.type =
    add(r => if (r.transient_?) validator(r) else None)

  def addForPersisted(validator: R => Option[Msg]): this.type =
    add(r => if (!r.transient_?) validator(r) else None)

  def notNull(f: R => Field[_, R]): this.type = add { r =>
    val field = f(r)
    if (field.null_?)
      Some(new Msg(field.uuid + ".null", "record" -> r, "field" -> field))
    else None
  }

  def notEmpty(f: R => TextField[R]): this.type = add { r =>
    val field = f(r)
    if (field.null_?)
      Some(new Msg(field.uuid + ".null", "record" -> r, "field" -> field))
    else if (field().trim == "")
      Some(new Msg(field.uuid + ".empty", "record" -> r, "field" -> field))
    else None
  }

  def pattern(f: R => TextField[R], regex: String, key: String = "pattern"): this.type = add { r =>
    val field = f(r)
    if (field.null_?)
      None
    else if (!field().matches(regex))
      Some(new Msg(field.uuid + "." + key, "regex" -> regex,
        "value" -> field(),
        "record" -> r,
        "field" -> field))
    else None
  }

  def unique[T](f: R => Field[T, R], key: String = "unique"): this.type = add { r =>
    val field = f(r)
    r.relation.criteria.add(field EQ field()).unique match {
      case Some(a) if (r.transient_? || a != r) =>
        Some(new Msg(field.uuid + "." + key, "record" -> r, "field" -> field))
      case _ => None
    }
  }

  def uniqueAll(f: R => Seq[Field[_, R]], key: String = "unique"): this.type = add { r =>
    val fields = f(r)
    val crit = r.relation.criteria
    fields.foreach {
      case f: Field[Any, R] => crit.add(f EQ f())
      case _ =>
    }
    crit.unique match {
      case Some(a: R) if (r.transient_? || a != r) =>
         Some(new Msg(r.uuid + "." + key, "record" -> r))
      case _ => None
    }
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy