<commit_before>/*
 * Copyright 2014 <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.liftmodules.validate

import scala.xml._
import net.liftweb.http._
import net.liftweb.http.js.JE._
import net.liftweb.json._
import net.liftweb.json.JsonDSL._
import net.liftweb.json.{ JObject, JField, JNull }
import net.liftweb.common.Full
import net.liftweb.common.Loggable
import java.net.URI
import scala.util.matching.Regex

object Validators {
  //  To allow build for Scala 2.9.x
  //  import scala.language.implicitConversions

  class Validatable(in: NodeSeq) {
    def >>(attr: Validator[_]): NodeSeq = in match {
      case e: Node => attr(e)
      case _ => attr(in.head) ++ in.tail
    }
  }

  implicit def elemToValidatable(e: NodeSeq): Validatable = {
    new Validatable(e)
  }

  /**
   * Validates if some value is entered
   *
   * @param value value to be checked
   * @param errorMessage message to be shown if validation fails
   * @param isEnabled allows to disable validator on some condition.
   */
  case class ValidateRequired(
      override val value: () => String,
      isEnabled: () => Boolean = () => true,
      override val errorMessage: Option[String] = None)(implicit ctx: ValidationContext) extends Validator[String] {

    override def validate(): Boolean = !isEnabled() || Option(value()).exists(_.trim.nonEmpty)

    override def check: JObject = if (isEnabled()) "required" -> true else JObject(Nil)

    override def messages: Option[JObject] = errorMessage map ("required" -> _)
  }

  object ValidateRequired {
    def apply(value: () => String, isEnabled: () => Boolean, errorMessage: String)(implicit ctx: ValidationContext): ValidateRequired =
      ValidateRequired(value, isEnabled, Some(errorMessage))

    def apply(value: () => String, errorMessage: String)(implicit ctx: ValidationContext): ValidateRequired =
      ValidateRequired(value, () => true, Some(errorMessage))<commit_msg>Implemented new validator ValidateRequiredBoolean for validate checkboxes<commit_after>/*
 * Copyright 2014 <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.liftmodules.validate

import scala.xml._
import net.liftweb.http._
import net.liftweb.http.js.JE._
import net.liftweb.json._
import net.liftweb.json.JsonDSL._
import net.liftweb.json.{ JObject, JField, JNull }
import net.liftweb.common.Full
import net.liftweb.common.Loggable
import java.net.URI
import scala.util.matching.Regex

object Validators {
  //  To allow build for Scala 2.9.x
  //  import scala.language.implicitConversions

  class Validatable(in: NodeSeq) {
    def >>(attr: Validator[_]): NodeSeq = in match {
      case e: Node => attr(e)
      case _ => attr(in.head) ++ in.tail
    }
  }

  implicit def elemToValidatable(e: NodeSeq): Validatable = {
    new Validatable(e)
  }

  /**
   * Validates if some value is entered
   *
   * @param value value to be checked
   * @param errorMessage message to be shown if validation fails
   * @param isEnabled allows to disable validator on some condition.
   */
  case class ValidateRequired(
      override val value: () => String,
      isEnabled: () => Boolean = () => true,
      override val errorMessage: Option[String] = None)(implicit ctx: ValidationContext) extends Validator[String] {

    override def validate(): Boolean = !isEnabled() || Option(value()).exists(_.trim.nonEmpty)

    override def check: JObject = if (isEnabled()) "required" -> true else JObject(Nil)

    override def messages: Option[JObject] = errorMessage map ("required" -> _)
  }

  case class ValidateRequiredBoolean(
      override val value: () => Boolean,
      isEnabled: () => Boolean = () => true,
      override val errorMessage: Option[String] = None)(implicit ctx: ValidationContext) extends Validator[Boolean] {

    override def validate(): Boolean = !isEnabled() || value()

    override def check: JObject = if (isEnabled()) "required" -> true else JObject(Nil)

    override def messages: Option[JObject] = errorMessage map ("required" -> _)
  }

  object ValidateRequired {
    def apply(value: () => String, isEnabled: () => Boolean, errorMessage: String)(implicit ctx: ValidationContext): ValidateRequired =
      ValidateRequired(value, isEnabled, Some(errorMessage))

    def apply(value: () => String, errorMessage: String)(implicit ctx: ValidationContext): ValidateRequired =
      ValidateRequired(value, () => true, Some(errorMessage))

    def apply(value: () => Boolean, errorMessage: String)(implicit ctx: ValidationContext): ValidateRequiredBoolean =
      ValidateRequiredBoolean(value, () => true, Some(errorMessage))
{'fraction_non_alphanumeric': 0.10949423247559893, 'fraction_numerical': 0.00354924578527063, 'mean_word_length': 3.913687881429817, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 38, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 2}, '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': '2400373', 'n_tokens_mistral': 1600, 'n_tokens_neox': 1554, 'n_words': 534}