<commit_before>package org.hyperscala.ui.widgets.visual.validation

/**
 * @author <NAME> <<EMAIL>>
 */
trait Validation[T] extends Function1[T, Either[Option[T], String]]

object Validation {
  val StringEmpty = error[String]((s: String) => s != null && !s.isEmpty, "This is a required field")
  def StringMinimumLength(minimum: Int) = error[String]((s: String) => s.length >= minimum, "Value must be at least %s characters".format(minimum))
  def StringMaximumLength(maximum: Int) = error[String]((s: String) => s.length <= maximum, "Value must be no more than %s characters".format(maximum))

  def error[T](validator: T => Boolean, message: String) = new Validation[T] {
    def apply(v: T) = if (validator(v)) {
      Left(Some(v))
    } else {
      Right(message)
    }
  }

  def conversion[T](converter: T => T) = new Validation[T] {
    def apply(v: T) = Left(Some(converter(v)))
  }

  def ignore[T](validator: T => Boolean) = new Validation[T] {
    def apply(v: T) = if (validator(v)) {
      Left(Some(v))
    } else {
      Left(None)
    }
  }
}<commit_msg>Updated string validations to properly handle null.<commit_after>package org.hyperscala.ui.widgets.visual.validation

/**
 * @author <NAME> <<EMAIL>>
 */
trait Validation[T] extends Function1[T, Either[Option[T], String]]

object Validation {
  val StringEmpty = error[String]((s: String) => s != null && !s.isEmpty, "This is a required field")
  def StringMinimumLength(minimum: Int) = error[String]((s: String) => s != null && s.length >= minimum, "Value must be at least %s characters".format(minimum))
  def StringMaximumLength(maximum: Int) = error[String]((s: String) => s == null || s.length <= maximum, "Value must be no more than %s characters".format(maximum))

  def error[T](validator: T => Boolean, message: String) = new Validation[T] {
    def apply(v: T) = if (validator(v)) {
      Left(Some(v))
    } else {
      Right(message)
    }
  }

  def conversion[T](converter: T => T) = new Validation[T] {
    def apply(v: T) = Left(Some(converter(v)))
  }

  def ignore[T](validator: T => Boolean) = new Validation[T] {
    def apply(v: T) = if (validator(v)) {
      Left(Some(v))
    } else {
      Left(None)
    }
  }
}
{'fraction_non_alphanumeric': 0.16877828054298644, 'fraction_numerical': 0.0009049773755656109, 'mean_word_length': 3.848684210526316, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1796188', 'n_tokens_mistral': 758, 'n_tokens_neox': 732, 'n_words': 198}