<commit_before>/*
 * Copyright (C) 2009-2015 Typesafe Inc. <http://www.typesafe.com>
 */
package play.api.libs.json

import play.api.data.validation.ValidationError
import Json._

trait ConstraintFormat {
  def of[A](implicit fmt: Format[A]): Format[A] = fmt

  /** deleted because useless and troublesome (better to use nullable anyway) */
  //def optional[A](implicit fmt: Format[A]): Format[Option[A]] = Format[Option[A]]( Reads.optional(fmt), Writes.optional(fmt) )

  def optionWithNull[A](implicit fmt: Format[A]): Format[Option[A]] = Format[Option[A]](Reads.optionWithNull(fmt), Writes.optionWithNull(fmt))
}

trait PathFormat {
  def at[A](path: JsPath)(implicit f: Format[A]): OFormat[A] =
    OFormat[A](Reads.at(path)(f), Writes.at(path)(f))

  def nullable[A](path: JsPath)(implicit f: Format[A]): OFormat[Option[A]] =
    OFormat(Reads.nullable(path)(f), Writes.nullable(path)(f))

}

trait PathReads {

  def required(path: JsPath)(implicit reads: Reads[JsValue]): Reads[JsValue] = at(path)(reads)

  def at[A](path: JsPath)(implicit reads: Reads[A]): Reads[A] =
    Reads[A](js => path.asSingleJsResult(js).flatMap(reads.reads(_).repath(path)))

  /**
   * Reads a Option[T] search optional or nullable field at JsPath (field not found or null is None
   * and other cases are Error).
   *
   * It runs through JsValue following all JsPath nodes on JsValue except last node:
   * - If one node in JsPath is not found before last node => returns JsError( "missing-path" )
   * - If all nodes are found till last node, it runs through JsValue with last node =>
   *   - If last node if not found => returns None
   *   - If last node is found with value "null" => returns None
   *   - If last node is found => applies implicit Reads[T]
   */
  def nullable[A](path: JsPath)(implicit reads: Reads[A]) = Reads[Option[A]] { json =>
    path.applyTillLast(json).fold(
      jserr => jserr,
      jsres => jsres.fold(
        _ => JsSuccess(None),
        a => a match {
          case JsNull => JsSuccess(None)
          case js => reads.reads(js).repath(path).map(Some(_))
        }
      )
    )
  }

  def jsPick[A <: JsValue](path: JsPath)(implicit reads: Reads[A]): Reads[A] = at(path)(reads)

  def jsPickBranch[A <: JsValue](path: JsPath)(implicit reads: Reads[A]): Reads[JsObject] =
    Reads[JsObject](js => path.asSingleJsResult(js).flatMap { jsv => reads.reads(jsv).repath(path) }.map(jsv => JsPath.createObj(path -> jsv)))

  def jsPut(path: JsPath, a: => JsValue) = Reads[JsObject](json => JsSuccess(JsPath.createObj(path -> a)))

  def jsCopyTo[A <: JsValue](path: JsPath)(reads: Reads[A]) =
    Reads[JsObject](js => reads.reads(js).map(js => JsPath.createObj(path -> js)))

  def jsUpdate[A <: JsValue](path: JsPath)(reads: Reads[A]) =
    Reads[JsObject](js => js match {
      case o: JsObject =>
        path.asSingleJsResult(o)
          .flatMap(js => reads.reads(js).repath(path))
          .map(jsv => JsPath.createObj(path -> jsv))
          .map(opath => o.deepMerge(opath))
      case _ =>
        JsError(JsPath(), ValidationError("error.expected.jsobject"))
    })

  def jsPrune(path: JsPath) = Reads[JsObject](js => path.prune(js))
}

trait ConstraintReads {
  /** The simpler of all Reads that just finds an implicit Reads[A] of the expected type */
  def of[A](implicit r: Reads[A]) = r

  /** deleted because useless and troublesome (better to use nullable anyway) */
  //def optional[A](implicit reads:Reads[A]):Reads[Option[A]] =
  //  Reads[Option[A]](js => JsSuccess(reads.reads(js).asOpt))

  /** very simple optional field Reads that maps "null" to None */
  def optionWithNull[T](implicit rds: Reads[T]): Reads[Option[T]] = Reads(js => js match {
    case JsNull => JsSuccess(None)
    case js => rds.reads(js).map(Some(_))
  })

  /** Stupidly reads a field as an Option mapping any error (format or missing field) to None */
  def optionNoError[A](implicit reads: Reads[A]): Reads[Option[A]] =
    Reads[Option[A]](js => JsSuccess(reads.reads(js).asOpt))

  def list[A](implicit reads: Reads[A]): Reads[List[A]] = Reads.traversableReads[List, A]
  def set[A](implicit reads: Reads[A]): Reads[Set[A]] = Reads.traversableReads[Set, A]
  def seq[A](implicit reads: Reads[A]): Reads[Seq[A]] = Reads.traversableReads[Seq, A]
  def map[A](implicit reads: Reads[A]): Reads[collection.immutable.Map[String, A]] = Reads.mapReads[A]

  /**
   * Defines a minimum value for a numeric Reads. Combine with `max` using `or`, e.g.
   * `.read(Reads.min(0) or Reads.max(100))`.
   */
  def min[N](m: N)(implicit reads: Reads[N], num: Numeric[N]) =
    filterNot[N](ValidationError("error.min", m))(num.lt(_, m))(reads)

  /**
   * Defines a maximum value for a numeric Reads. Combine with `min` using `or`, e.g.
   * `.read(Reads.min(0.1) or Reads.max(1.0))`.
   */
  def max[N](m: N)(implicit reads: Reads[N], num: Numeric[N]) =
    filterNot[N](ValidationError("error.max", m))(num.gt(_, m))(reads)

  def filterNot[A](error: ValidationError)(p: A => Boolean)(implicit reads: Reads[A]) =
    Reads[A](js => reads.reads(js).filterNot(error)(p))

  def filter[A](otherwise: ValidationError)(p: A => Boolean)(implicit reads: Reads[A]) =
    Reads[A](js => reads.reads(js).filter(otherwise)(p))

  def minLength[M](m: Int)(implicit reads: Reads[M], p: M => scala.collection.TraversableLike[_, M]) =
    filterNot[M](ValidationError("error.minLength", m))(_.size < m)

  def maxLength[M](m: Int)(implicit reads: Reads[M], p: M => scala.collection.TraversableLike[_, M]) =
    filterNot[M](ValidationError("error.maxLength", m))(_.size > m)

  /**
   * Defines a regular expression constraint for `String` values, i.e. the string must match the regular expression pattern
   */
  def pattern(regex: => scala.util.matching.Regex, error: String = "error.pattern")(implicit reads: Reads[String]) =
    Reads[String](js => reads.reads(js).flatMap { o =>
      regex.unapplySeq(o).map(_ => JsSuccess(o)).getOrElse(JsError(error))
    })

  def email(implicit reads: Reads[String]): Reads[String] =
    pattern("""\b[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}\b""".r, "error.email")

  def verifying[A](cond: A => Boolean)(implicit rds: Reads[A]) =<commit_msg>modified email reggae pattern for "<EMAIL>" domain<commit_after>/*
 * Copyright (C) 2009-2015 Typesafe Inc. <http://www.typesafe.com>
 */
package play.api.libs.json

import play.api.data.validation.ValidationError
import Json._

trait ConstraintFormat {
  def of[A](implicit fmt: Format[A]): Format[A] = fmt

  /** deleted because useless and troublesome (better to use nullable anyway) */
  //def optional[A](implicit fmt: Format[A]): Format[Option[A]] = Format[Option[A]]( Reads.optional(fmt), Writes.optional(fmt) )

  def optionWithNull[A](implicit fmt: Format[A]): Format[Option[A]] = Format[Option[A]](Reads.optionWithNull(fmt), Writes.optionWithNull(fmt))
}

trait PathFormat {
  def at[A](path: JsPath)(implicit f: Format[A]): OFormat[A] =
    OFormat[A](Reads.at(path)(f), Writes.at(path)(f))

  def nullable[A](path: JsPath)(implicit f: Format[A]): OFormat[Option[A]] =
    OFormat(Reads.nullable(path)(f), Writes.nullable(path)(f))

}

trait PathReads {

  def required(path: JsPath)(implicit reads: Reads[JsValue]): Reads[JsValue] = at(path)(reads)

  def at[A](path: JsPath)(implicit reads: Reads[A]): Reads[A] =
    Reads[A](js => path.asSingleJsResult(js).flatMap(reads.reads(_).repath(path)))

  /**
   * Reads a Option[T] search optional or nullable field at JsPath (field not found or null is None
   * and other cases are Error).
   *
   * It runs through JsValue following all JsPath nodes on JsValue except last node:
   * - If one node in JsPath is not found before last node => returns JsError( "missing-path" )
   * - If all nodes are found till last node, it runs through JsValue with last node =>
   *   - If last node if not found => returns None
   *   - If last node is found with value "null" => returns None
   *   - If last node is found => applies implicit Reads[T]
   */
  def nullable[A](path: JsPath)(implicit reads: Reads[A]) = Reads[Option[A]] { json =>
    path.applyTillLast(json).fold(
      jserr => jserr,
      jsres => jsres.fold(
        _ => JsSuccess(None),
        a => a match {
          case JsNull => JsSuccess(None)
          case js => reads.reads(js).repath(path).map(Some(_))
        }
      )
    )
  }

  def jsPick[A <: JsValue](path: JsPath)(implicit reads: Reads[A]): Reads[A] = at(path)(reads)

  def jsPickBranch[A <: JsValue](path: JsPath)(implicit reads: Reads[A]): Reads[JsObject] =
    Reads[JsObject](js => path.asSingleJsResult(js).flatMap { jsv => reads.reads(jsv).repath(path) }.map(jsv => JsPath.createObj(path -> jsv)))

  def jsPut(path: JsPath, a: => JsValue) = Reads[JsObject](json => JsSuccess(JsPath.createObj(path -> a)))

  def jsCopyTo[A <: JsValue](path: JsPath)(reads: Reads[A]) =
    Reads[JsObject](js => reads.reads(js).map(js => JsPath.createObj(path -> js)))

  def jsUpdate[A <: JsValue](path: JsPath)(reads: Reads[A]) =
    Reads[JsObject](js => js match {
      case o: JsObject =>
        path.asSingleJsResult(o)
          .flatMap(js => reads.reads(js).repath(path))
          .map(jsv => JsPath.createObj(path -> jsv))
          .map(opath => o.deepMerge(opath))
      case _ =>
        JsError(JsPath(), ValidationError("error.expected.jsobject"))
    })

  def jsPrune(path: JsPath) = Reads[JsObject](js => path.prune(js))
}

trait ConstraintReads {
  /** The simpler of all Reads that just finds an implicit Reads[A] of the expected type */
  def of[A](implicit r: Reads[A]) = r

  /** deleted because useless and troublesome (better to use nullable anyway) */
  //def optional[A](implicit reads:Reads[A]):Reads[Option[A]] =
  //  Reads[Option[A]](js => JsSuccess(reads.reads(js).asOpt))

  /** very simple optional field Reads that maps "null" to None */
  def optionWithNull[T](implicit rds: Reads[T]): Reads[Option[T]] = Reads(js => js match {
    case JsNull => JsSuccess(None)
    case js => rds.reads(js).map(Some(_))
  })

  /** Stupidly reads a field as an Option mapping any error (format or missing field) to None */
  def optionNoError[A](implicit reads: Reads[A]): Reads[Option[A]] =
    Reads[Option[A]](js => JsSuccess(reads.reads(js).asOpt))

  def list[A](implicit reads: Reads[A]): Reads[List[A]] = Reads.traversableReads[List, A]
  def set[A](implicit reads: Reads[A]): Reads[Set[A]] = Reads.traversableReads[Set, A]
  def seq[A](implicit reads: Reads[A]): Reads[Seq[A]] = Reads.traversableReads[Seq, A]
  def map[A](implicit reads: Reads[A]): Reads[collection.immutable.Map[String, A]] = Reads.mapReads[A]

  /**
   * Defines a minimum value for a numeric Reads. Combine with `max` using `or`, e.g.
   * `.read(Reads.min(0) or Reads.max(100))`.
   */
  def min[N](m: N)(implicit reads: Reads[N], num: Numeric[N]) =
    filterNot[N](ValidationError("error.min", m))(num.lt(_, m))(reads)

  /**
   * Defines a maximum value for a numeric Reads. Combine with `min` using `or`, e.g.
   * `.read(Reads.min(0.1) or Reads.max(1.0))`.
   */
  def max[N](m: N)(implicit reads: Reads[N], num: Numeric[N]) =
    filterNot[N](ValidationError("error.max", m))(num.gt(_, m))(reads)

  def filterNot[A](error: ValidationError)(p: A => Boolean)(implicit reads: Reads[A]) =
    Reads[A](js => reads.reads(js).filterNot(error)(p))

  def filter[A](otherwise: ValidationError)(p: A => Boolean)(implicit reads: Reads[A]) =
    Reads[A](js => reads.reads(js).filter(otherwise)(p))

  def minLength[M](m: Int)(implicit reads: Reads[M], p: M => scala.collection.TraversableLike[_, M]) =
    filterNot[M](ValidationError("error.minLength", m))(_.size < m)

  def maxLength[M](m: Int)(implicit reads: Reads[M], p: M => scala.collection.TraversableLike[_, M]) =
    filterNot[M](ValidationError("error.maxLength", m))(_.size > m)

  /**
   * Defines a regular expression constraint for `String` values, i.e. the string must match the regular expression pattern
   */
  def pattern(regex: => scala.util.matching.Regex, error: String = "error.pattern")(implicit reads: Reads[String]) =
    Reads[String](js => reads.reads(js).flatMap { o =>
      regex.unapplySeq(o).map(_ => JsSuccess(o)).getOrElse(JsError(error))
    })

  def email(implicit reads: Reads[String]): Reads[String] =
    pattern("""\b[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,5}\b""".r, "error.email")

  def verifying[A](cond: A => Boolean)(implicit rds: Reads[A]) =
{'fraction_non_alphanumeric': 0.17572682612547313, 'fraction_numerical': 0.0035435290327776436, 'mean_word_length': 4.112391930835735, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 102, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '7062199', 'n_tokens_mistral': 4640, 'n_tokens_neox': 4464, 'n_words': 1026}