Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge branch 2.2.x_2.9 with configurable validation error messages

  • Loading branch information...
commit 5ab278b6b4773e33900c3ee159132081f127ef9d 2 parents 59af07a + 5075876
@casualjim casualjim authored
View
2  atmosphere/src/main/scala/org/atmosphere/cpr/ScalatraBroadcasterFactory.scala
@@ -32,7 +32,7 @@ class ScalatraBroadcasterFactory(cfg: AtmosphereConfig)(implicit wireFormat: Wir
InjectorProvider.getInjector.inject(b)
if (b.getBroadcasterConfig == null) {
- b.setBroadcasterConfig(new BroadcasterConfig(cfg.framework().broadcasterFilters, cfg))
+ b.setBroadcasterConfig(new BroadcasterConfig(cfg.framework().broadcasterFilters, cfg, "scalatra-broadcaster-factory"))
}
b.setBroadcasterLifeCyclePolicy(BroadcasterLifeCyclePolicy.NEVER)
View
7 atmosphere/src/main/scala/org/scalatra/atmosphere/ScalatraAtmosphereHandler.scala
@@ -20,7 +20,10 @@ object ScalatraAtmosphereHandler {
private class ScalatraResourceEventListener extends AtmosphereResourceEventListener {
def client(resource: AtmosphereResource) =
- Option(resource.session()).flatMap(_.get(AtmosphereClientKey)).map(_.asInstanceOf[AtmosphereClient])
+ Option(resource.session()).flatMap(_.get(org.scalatra.atmosphere.AtmosphereClientKey)).map(_.asInstanceOf[AtmosphereClient])
+
+
+ def onPreSuspend(event: AtmosphereResourceEvent) {}
def onBroadcast(event: AtmosphereResourceEvent) {
val resource = event.getResource
@@ -31,7 +34,7 @@ object ScalatraAtmosphereHandler {
}
def onDisconnect(event: AtmosphereResourceEvent) {
- event.getResource.session.removeAttribute(AtmosphereClientKey)
+ event.getResource.session.removeAttribute(org.scalatra.atmosphere.AtmosphereClientKey)
if (event.isCancelled) {
val disconnector = if (event.isCancelled) ClientDisconnected else ServerDisconnected
client(event.getResource) foreach (_.receive.lift(Disconnected(disconnector, Option(event.throwable))))
View
126 commands/src/main/scala/org/scalatra/commands/field.scala
@@ -44,7 +44,7 @@ trait FieldDescriptor[T] {
private[commands] def defVal: DefVal[T]
def defaultValue: T = defVal.value
- def withDefaultValue(default: => T): FieldDescriptor[T]
+ def withDefaultValue(default: => T): FieldDescriptor[T]
def isValid = value.isSuccess
def isInvalid = value.isFailure
@@ -76,7 +76,7 @@ class BasicFieldDescriptor[T](
val name: String,
val validator: Option[Validator[T]] = None,
private[commands] val transformations: T => T = identity _,
- private[commands] var isRequired: Boolean = false,
+ private[commands] var isRequired: Boolean = true,
val description: String = "",
val notes: String = "",
private[commands] val defVal: DefVal[T],
@@ -125,11 +125,11 @@ class BasicFieldDescriptor[T](
def notes(note: String) = copy(notes = note)
- def withDefaultValue(default: => T): FieldDescriptor[T] = copy(defVal = DefVal(default))
+ def withDefaultValue(default: => T): FieldDescriptor[T] = copy(defVal = DefVal(default), isRequired = false)
def sourcedFrom(valueSource: ValueSource.Value): FieldDescriptor[T] = copy(valueSource = valueSource)
- def allowableValues(vals: T*): FieldDescriptor[T] = copy(allowableValues = vals.toList).validateWith(BindingValidators.oneOf(vals:_*))
+ def allowableValues(vals: T*): FieldDescriptor[T] = copy(allowableValues = vals.toList).validateWith(BindingValidators.oneOf("%%s must be one of %s.", vals))
def displayName(name: String): FieldDescriptor[T] = copy(displayName = name.blankOption)
}
@@ -294,120 +294,132 @@ object BindingValidators {
class ValidatableSeq[T <: Seq[_]](b: FieldDescriptor[T]) {
- def notEmpty: FieldDescriptor[T] =
- b.required.validateWith(BindingValidators.nonEmptyCollection)
+ def notEmpty: FieldDescriptor[T] = notEmpty()
+ def notEmpty(messageFormat: String = "%s is required."): FieldDescriptor[T] =
+ b.required.validateWith(BindingValidators.nonEmptyCollection(messageFormat))
}
class ValidatableOrdered[T <% Ordered[T]](b: FieldDescriptor[T]) {
- def greaterThan(min: T): FieldDescriptor[T] =
- b.validateWith(BindingValidators.greaterThan(min))
+ def greaterThan(min: T, messageFormat: String = "%%s must be greater than %s"): FieldDescriptor[T] =
+ b.validateWith(BindingValidators.greaterThan(min, messageFormat))
- def lessThan(max: T): FieldDescriptor[T] =
- b.validateWith(BindingValidators.lessThan(max))
+ def lessThan(max: T, messageFormat: String = "%%s must be less than %s"): FieldDescriptor[T] =
+ b.validateWith(BindingValidators.lessThan(max, messageFormat))
- def greaterThanOrEqualTo(min: T): FieldDescriptor[T] =
- b.validateWith(BindingValidators.greaterThanOrEqualTo(min))
+ def greaterThanOrEqualTo(min: T, messageFormat: String = "%%s must be greater than or equal to %s"): FieldDescriptor[T] =
+ b.validateWith(BindingValidators.greaterThanOrEqualTo(min, messageFormat))
- def lessThanOrEqualTo(max: T): FieldDescriptor[T] =
- b.validateWith(BindingValidators.lessThanOrEqualTo(max))
+ def lessThanOrEqualTo(max: T, messageFormat: String = "%%s must be greater than or equal to %s"): FieldDescriptor[T] =
+ b.validateWith(BindingValidators.lessThanOrEqualTo(max, messageFormat))
}
class ValidatableGenericBinding[T](b: FieldDescriptor[T]) {
- def validate(validate: T => Boolean): FieldDescriptor[T] = b.validateWith(BindingValidators.validate(validate))
+ def validate(validate: T => Boolean, messageFormat: String = "%s is invalid."): FieldDescriptor[T] =
+ b.validateWith(BindingValidators.validate(validate, messageFormat))
}
class ValidatableStringBinding(b: FieldDescriptor[String]) {
- def notBlank: FieldDescriptor[String] = b.required.validateWith(BindingValidators.nonEmptyString)
+ def notBlank: FieldDescriptor[String] = notBlank()
+ def notBlank(messageFormat: String = "%s is required"): FieldDescriptor[String] =
+ b.required.validateWith(BindingValidators.nonEmptyString(messageFormat))
- def validEmail: FieldDescriptor[String] = b.validateWith(BindingValidators.validEmail)
- def validAbsoluteUrl(allowLocalHost: Boolean, schemes: String*): FieldDescriptor[String] =
- b.validateWith(BindingValidators.validAbsoluteUrl(allowLocalHost, schemes:_*))
+ def validEmail: FieldDescriptor[String] = validEmail()
+ def validEmail(messageFormat: String = "%s must be a valid email address."): FieldDescriptor[String] =
+ b.validateWith(BindingValidators.validEmail(messageFormat))
- def validUrl(allowLocalHost: Boolean, schemes: String*): FieldDescriptor[String] =
- b.validateWith(BindingValidators.validUrl(allowLocalHost, schemes:_*))
+ def validAbsoluteUrl(allowLocalHost: Boolean, messageFormat: String = "%s must be a valid absolute url.", schemes: Seq[String] = Seq("http", "https")): FieldDescriptor[String] =
+ b.validateWith(BindingValidators.validAbsoluteUrl(allowLocalHost, messageFormat, schemes))
+
+ def validUrl(allowLocalHost: Boolean, messageFormat: String = "%s must be a valid url.", schemes: Seq[String] = Seq("http", "https")): FieldDescriptor[String] =
+ b.validateWith(BindingValidators.validUrl(allowLocalHost, messageFormat, schemes))
def validForFormat(regex: Regex, messageFormat: String = "%s is invalid."): FieldDescriptor[String] =
b.validateWith(BindingValidators.validFormat(regex, messageFormat))
- def validForConfirmation(against: Field[String]): FieldDescriptor[String] =
- b.validateWith(BindingValidators.validConfirmation(against))
+ def validForConfirmation(against: Field[String], messageFormat: String = "%%s must match %s."): FieldDescriptor[String] =
+ b.validateWith(BindingValidators.validConfirmation(against, messageFormat))
- def minLength(min: Int): FieldDescriptor[String] =
- b.validateWith(BindingValidators.minLength(min))
+ def minLength(min: Int, messageFormat: String = "%%s must be at least %s long."): FieldDescriptor[String] =
+ b.validateWith(BindingValidators.minLength(min, messageFormat))
- def enumValue(enum: Enumeration): FieldDescriptor[String] =
- b.validateWith(BindingValidators.enumValue(enum))
+ def enumValue(enum: Enumeration, messageFormat: String = "%%s must be one of %s."): FieldDescriptor[String] =
+ b.validateWith(BindingValidators.enumValue(enum, messageFormat))
}
import org.scalatra.validation.Validation
- def validate[TValue](validate: TValue => Boolean): BindingValidator[TValue] = (s: String) => {
- _ flatMap (Validators.validate(s, validate = validate).validate(_))
+ def validate[TValue](validate: TValue => Boolean, messageFormat: String = "%s is invalid."): BindingValidator[TValue] = (s: String) => {
+ _ flatMap (Validators.validate(s, messageFormat = messageFormat, validate = validate).validate(_))
}
- def nonEmptyString: BindingValidator[String] = (s: String) => {
- _ flatMap (Validation.nonEmptyString(s, _))
+ def nonEmptyString: BindingValidator[String] = nonEmptyString()
+ def nonEmptyString(messageFormat: String = "%s is required."): BindingValidator[String] = (s: String) => {
+ _ flatMap (Validation.nonEmptyString(s, _, messageFormat))
}
- def notNull: BindingValidator[AnyRef] = (s: String) => {
- _ flatMap (Validation.notNull(s, _))
+ def notNull: BindingValidator[AnyRef] = notNull()
+ def notNull(messageFormat: String = "%s is required."): BindingValidator[AnyRef] = (s: String) => {
+ _ flatMap (Validation.notNull(s, _, messageFormat))
}
- def nonEmptyCollection[TResult <: Traversable[_]]: BindingValidator[TResult] = (s: String) =>{
- _ flatMap (Validation.nonEmptyCollection(s, _))
+ def nonEmptyCollection[TResult <: Traversable[_]]: BindingValidator[TResult] = nonEmptyCollection[TResult]()
+ def nonEmptyCollection[TResult <: Traversable[_]](messageFormat: String = "%s must not be empty."): BindingValidator[TResult] = (s: String) =>{
+ _ flatMap (Validation.nonEmptyCollection(s, _, messageFormat))
}
- def validEmail: BindingValidator[String] = (s: String) =>{
- _ flatMap (Validation.validEmail(s, _))
+ def validEmail: BindingValidator[String] = validEmail()
+ def validEmail(messageFormat: String = "%s must be a valid email address."): BindingValidator[String] = (s: String) =>{
+ _ flatMap (Validation.validEmail(s, _, messageFormat))
}
- def validAbsoluteUrl(allowLocalHost: Boolean, schemes: String*): BindingValidator[String] = (s: String) =>{
- _ flatMap (Validators.validAbsoluteUrl(s, allowLocalHost, schemes:_*).validate(_))
+ def validAbsoluteUrl(allowLocalHost: Boolean, messageFormat: String = "%s must be a absolute valid url.", schemes: Seq[String] = Seq("http", "https")): BindingValidator[String] = (s: String) =>{
+ _ flatMap (Validators.validAbsoluteUrl(s, allowLocalHost, messageFormat, schemes).validate(_))
}
- def validUrl(allowLocalHost: Boolean, schemes: String*): BindingValidator[String] = (s: String) =>{
- _ flatMap (Validators.validUrl(s, allowLocalHost, schemes:_*).validate(_))
+ def validUrl(allowLocalHost: Boolean, messageFormat: String = "%s must be a valid url.", schemes: Seq[String] = Seq("http", "https")): BindingValidator[String] = (s: String) =>{
+ _ flatMap (Validators.validUrl(s, allowLocalHost, messageFormat, schemes).validate(_))
}
- def validFormat(regex: Regex, messageFormat: String = "%sis invalid."): BindingValidator[String] = (s: String) =>{
+ def validFormat(regex: Regex, messageFormat: String = "%s is invalid."): BindingValidator[String] = (s: String) =>{
_ flatMap (Validators.validFormat(s, regex, messageFormat).validate(_))
}
- def validConfirmation(against: Field[String]): BindingValidator[String] = (s: String) =>{
- _ flatMap { Validators.validConfirmation(s, against.name, against.value | against.defaultValue).validate(_) }
+ def validConfirmation(against: Field[String], messageFormat: String = "%%s must match %s."): BindingValidator[String] = (s: String) =>{
+ _ flatMap { Validators.validConfirmation(s, against.name, against.value | against.defaultValue, messageFormat).validate(_) }
}
- def greaterThan[T <% Ordered[T]](min: T): BindingValidator[T] = (s: String) =>{
- _ flatMap (Validators.greaterThan(s, min).validate(_))
+ def greaterThan[T <% Ordered[T]](min: T, messageFormat: String = "%%s must be greater than %s."): BindingValidator[T] = (s: String) =>{
+ _ flatMap (Validators.greaterThan(s, min, messageFormat).validate(_))
}
- def lessThan[T <% Ordered[T]](max: T): BindingValidator[T] = (s: String) =>{
- _ flatMap (Validators.lessThan(s, max).validate(_))
+ def lessThan[T <% Ordered[T]](max: T, messageFormat: String = "%%s must be less than %s."): BindingValidator[T] = (s: String) =>{
+ _ flatMap (Validators.lessThan(s, max, messageFormat).validate(_))
}
- def greaterThanOrEqualTo[T <% Ordered[T]](min: T): BindingValidator[T] = (s: String) =>{
- _ flatMap (Validators.greaterThanOrEqualTo(s, min).validate(_))
+ def greaterThanOrEqualTo[T <% Ordered[T]](min: T, messageFormat: String = "%%s must be greater than or equal to %s."): BindingValidator[T] = (s: String) =>{
+ _ flatMap (Validators.greaterThanOrEqualTo(s, min, messageFormat).validate(_))
}
- def lessThanOrEqualTo[T <% Ordered[T]](max: T): BindingValidator[T] = (s: String) =>{
- _ flatMap (Validators.lessThanOrEqualTo(s, max).validate(_))
+ def lessThanOrEqualTo[T <% Ordered[T]](max: T, messageFormat: String = "%%s must be less than or equal to %s."): BindingValidator[T] = (s: String) =>{
+ _ flatMap (Validators.lessThanOrEqualTo(s, max, messageFormat).validate(_))
}
- def minLength(min: Int): BindingValidator[String] = (s: String) =>{
- _ flatMap (Validators.minLength(s, min).validate(_))
+ def minLength(min: Int, messageFormat: String = "%%s must be at least %s characters long."): BindingValidator[String] = (s: String) =>{
+ _ flatMap (Validators.minLength(s, min, messageFormat).validate(_))
}
- def oneOf[TResult](expected: TResult*): BindingValidator[TResult] = (s: String) => {
- _ flatMap (Validators.oneOf(s, expected:_*).validate(_))
+ def oneOf[TResult](messageFormat: String = "%%s must be one of %s.", expected: Seq[TResult]): BindingValidator[TResult] = (s: String) => {
+ _ flatMap (Validators.oneOf(s, messageFormat, expected).validate(_))
}
- def enumValue(enum: Enumeration): BindingValidator[String] = oneOf(enum.values.map(_.toString).toSeq:_*)
+ def enumValue(enum: Enumeration, messageFormat: String = "%%s must be one of %s."): BindingValidator[String] =
+ oneOf(messageFormat, enum.values.map(_.toString).toSeq)
}
class Field[A:Manifest](descr: FieldDescriptor[A], command: Command) {
View
56 commands/src/main/scala/org/scalatra/validation/Validation.scala
@@ -6,50 +6,50 @@ import commands.FieldValidation
object Validation {
- def nonEmptyString(fieldName: String, value: String): FieldValidation[String] =
- Validators.nonEmptyString(fieldName).validate(value)
+ def nonEmptyString(fieldName: String, value: String, messageFormat: String = "%s is required."): FieldValidation[String] =
+ Validators.nonEmptyString(fieldName, messageFormat).validate(value)
- def notNull(fieldName: String, value: AnyRef): FieldValidation[AnyRef] =
- Validators.notNull(fieldName).validate(value)
+ def notNull(fieldName: String, value: AnyRef, messageFormat: String = "%s is required."): FieldValidation[AnyRef] =
+ Validators.notNull(fieldName, messageFormat).validate(value)
- def nonEmptyCollection[TResult <: Traversable[_]](fieldName: String, value: TResult): FieldValidation[TResult] =
- Validators.nonEmptyCollection(fieldName).validate(value)
+ def nonEmptyCollection[TResult <: Traversable[_]](fieldName: String, value: TResult, messageFormat: String = "%s must not be empty."): FieldValidation[TResult] =
+ Validators.nonEmptyCollection(fieldName, messageFormat).validate(value)
- def validEmail(fieldName: String, value: String): FieldValidation[String] =
- Validators.validEmail(fieldName).validate(value)
+ def validEmail(fieldName: String, value: String, messageFormat: String = "%s must be a valid email."): FieldValidation[String] =
+ Validators.validEmail(fieldName, messageFormat).validate(value)
- def validAbsoluteUrl(fieldName: String, value: String, allowLocalHost: Boolean, schemes: String*) =
- Validators.validAbsoluteUrl(fieldName, allowLocalHost, schemes:_*).validate(value)
+ def validAbsoluteUrl(fieldName: String, value: String, allowLocalHost: Boolean, messageFormat: String = "%s must be a valid absolute url.", schemes: Seq[String] = Seq("http", "https")) =
+ Validators.validAbsoluteUrl(fieldName, allowLocalHost, messageFormat, schemes).validate(value)
- def validUrl(fieldName: String, value: String, allowLocalHost: Boolean, schemes: String*) =
- Validators.validUrl(fieldName, allowLocalHost, schemes:_*).validate(value)
+ def validUrl(fieldName: String, value: String, allowLocalHost: Boolean, messageFormat: String = "%s must be a valid url.", schemes: Seq[String] = Seq("http", "https")) =
+ Validators.validUrl(fieldName, allowLocalHost, messageFormat, schemes).validate(value)
def validFormat(fieldName: String, value: String, regex: Regex, messageFormat: String = "%s is invalid."): FieldValidation[String] =
Validators.validFormat(fieldName, regex, messageFormat).validate(value)
- def validConfirmation(fieldName: String, value: String, confirmationFieldName: String, confirmationValue: => String): FieldValidation[String] =
- Validators.validConfirmation(fieldName, confirmationFieldName, confirmationValue).validate(value)
+ def validConfirmation(fieldName: String, value: String, confirmationFieldName: String, confirmationValue: => String, messageFormat: String = "%%s must match %s."): FieldValidation[String] =
+ Validators.validConfirmation(fieldName, confirmationFieldName, confirmationValue, messageFormat).validate(value)
- def greaterThan[T <% Ordered[T]](fieldName: String, value: T, min: T): FieldValidation[T] =
- Validators.greaterThan(fieldName, min).validate(value)
+ def greaterThan[T <% Ordered[T]](fieldName: String, value: T, min: T, messageFormat: String = "%%s must be greater than %s."): FieldValidation[T] =
+ Validators.greaterThan(fieldName, min, messageFormat).validate(value)
- def lessThan[T <% Ordered[T]](fieldName: String, value: T, max: T): FieldValidation[T] =
- Validators.lessThan(fieldName, max).validate(value)
+ def lessThan[T <% Ordered[T]](fieldName: String, value: T, max: T, messageFormat: String = "%%s must be less than %s."): FieldValidation[T] =
+ Validators.lessThan(fieldName, max, messageFormat).validate(value)
- def greaterThanOrEqualTo[T <% Ordered[T]](fieldName: String, value: T, min: T): FieldValidation[T] =
- Validators.greaterThanOrEqualTo(fieldName, min).validate(value)
+ def greaterThanOrEqualTo[T <% Ordered[T]](fieldName: String, value: T, min: T, messageFormat: String = "%%s must be greater than or equal to %s."): FieldValidation[T] =
+ Validators.greaterThanOrEqualTo(fieldName, min, messageFormat).validate(value)
- def lessThanOrEqualTo[T <% Ordered[T]](fieldName: String, value: T, max: T): FieldValidation[T] =
- Validators.lessThanOrEqualTo(fieldName, max).validate(value)
+ def lessThanOrEqualTo[T <% Ordered[T]](fieldName: String, value: T, max: T, messageFormat: String = "%%s must be less than or equal to %s."): FieldValidation[T] =
+ Validators.lessThanOrEqualTo(fieldName, max, messageFormat).validate(value)
- def minLength(fieldName: String, value: String, min: Int): FieldValidation[String] =
- Validators.minLength(fieldName, min).validate(value)
+ def minLength(fieldName: String, value: String, min: Int, messageFormat: String = "%%s must be at least %s characters long."): FieldValidation[String] =
+ Validators.minLength(fieldName, min, messageFormat).validate(value)
- def oneOf[TResult](fieldName: String, value: TResult, expected: TResult*): FieldValidation[TResult] =
- Validators.oneOf(fieldName, expected:_*).validate(value)
+ def oneOf[TResult](fieldName: String, value: TResult, messageFormat: String = "%%s must be one of %s.", expected: Seq[TResult]): FieldValidation[TResult] =
+ Validators.oneOf(fieldName, messageFormat, expected).validate(value)
- def enumValue(fieldName: String, value: String, enum: Enumeration): FieldValidation[String] =
- oneOf(fieldName, value, enum.values.map(_.toString).toSeq: _*)
+ def enumValue(fieldName: String, value: String, enum: Enumeration, messageFormat: String = "%%s must be one of %s."): FieldValidation[String] =
+ oneOf(fieldName, value, messageFormat, enum.values.map(_.toString).toSeq)
}
View
67 commands/src/main/scala/org/scalatra/validation/Validators.scala
@@ -11,6 +11,7 @@ import scalaz._
import Scalaz._
import mojolly.inflector.InflectorImports._
import commands.FieldValidation
+import util.RicherString._
object Validators {
trait Validator[TValue] {
@@ -21,49 +22,49 @@ object Validators {
extends Validator[TValue] {
def validate[TResult >: TValue <: TValue](value: TResult): FieldValidation[TResult] = {
if (isValid(value)) value.success
- else ValidationError(messageFormat.format(fieldName.humanize), FieldName(fieldName), ValidationFail).fail[TResult]
+ else ValidationError(messageFormat.format(fieldName.underscore.humanize), FieldName(fieldName), ValidationFail).fail[TResult]
}
}
- def validate[TValue](fieldName: String, message: String = "%s is invalid.", validate: TValue => Boolean) =
- new PredicateValidator[TValue](fieldName: String, validate, message: String)
+ def validate[TValue](fieldName: String, messageFormat: String = "%s is invalid.", validate: TValue => Boolean) =
+ new PredicateValidator[TValue](fieldName, validate, messageFormat)
/**
* Must be a non-empty [String]. null, " ", and "" are not allowed.
*/
- def nonEmptyString(fieldName: String, message: String = "%s must be present."): Validator[String] =
- new PredicateValidator[String](fieldName, s => s != null && s.trim.nonEmpty, message)
+ def nonEmptyString(fieldName: String, messageFormat: String = "%s must be present."): Validator[String] =
+ new PredicateValidator[String](fieldName, _.nonBlank, messageFormat)
/**
* Must be non-null.
*/
- def notNull(fieldName: String, message: String = "%s must be present."): Validator[AnyRef] =
- new PredicateValidator[AnyRef](fieldName, s => s != null, message)
+ def notNull(fieldName: String, messageFormat: String = "%s must be present."): Validator[AnyRef] =
+ new PredicateValidator[AnyRef](fieldName, _ != null, messageFormat)
/**
* Must be a collection which isn't empty.
*/
- def nonEmptyCollection[TResult <: Traversable[_]](fieldName: String, message: String = "%s must not be empty."): Validator[TResult] =
- new PredicateValidator[TResult](fieldName, _.nonEmpty, message)
+ def nonEmptyCollection[TResult <: Traversable[_]](fieldName: String, messageFormat: String = "%s must not be empty."): Validator[TResult] =
+ new PredicateValidator[TResult](fieldName, _.nonEmpty, messageFormat)
/**
* Must be a valid email as determined by org.apache.commons.validator.routines.EmailValidator
*/
- def validEmail(fieldName: String, message: String = "%s must be a valid email."): Validator[String] =
- new PredicateValidator[String](fieldName, EmailValidator.getInstance.isValid(_), message)
+ def validEmail(fieldName: String, messageFormat: String = "%s must be a valid email."): Validator[String] =
+ new PredicateValidator[String](fieldName, EmailValidator.getInstance.isValid(_), messageFormat)
/**
* Must be a valid absolute URL, parseable by the Apache Commons URI class.
*/
- def validAbsoluteUrl(fieldName: String, allowLocalHost: Boolean, schemes: String*) =
- buildUrlValidator(fieldName, true, allowLocalHost, schemes: _*)
+ def validAbsoluteUrl(fieldName: String, allowLocalHost: Boolean, messageFormat: String = "%s must be a valid absolute url.", schemes: Seq[String] = Seq("http", "https")) =
+ buildUrlValidator(fieldName, absolute = true, allowLocalHost = allowLocalHost, messageFormat = messageFormat, schemes)
/**
* Must be a valid URL, parseable by the Apache Commons URI class.
*/
- def validUrl(fieldName: String, allowLocalHost: Boolean, schemes: String*) =
- buildUrlValidator(fieldName, false, allowLocalHost, schemes: _*)
+ def validUrl(fieldName: String, allowLocalHost: Boolean, messageFormat: String = "%s must be a valid url.", schemes: Seq[String] = Seq("http", "https")) =
+ buildUrlValidator(fieldName, absolute = false, allowLocalHost = allowLocalHost, messageFormat = messageFormat, schemes)
/**
* Must match the regex.
@@ -74,65 +75,65 @@ object Validators {
/**
* The confirmation fieldName must have a true value.
*/
- def validConfirmation(fieldName: String, confirmationFieldName: String, confirmationValue: => String): Validator[String] =
+ def validConfirmation(fieldName: String, confirmationFieldName: String, confirmationValue: => String, messageFormat: String = "%%s must match %s."): Validator[String] =
new PredicateValidator[String](
fieldName,
_ == confirmationValue,
- "%s must match " + confirmationFieldName.underscore.humanize.toLowerCase(ENGLISH) + ".")
+ messageFormat.format(confirmationFieldName.underscore.humanize.toLowerCase(ENGLISH)))
/**
* Must be greater than the min param.
*/
- def greaterThan[T <% Ordered[T]](fieldName: String, min: T): Validator[T] =
- new PredicateValidator[T](fieldName, _ > min, "%s must be greater than " + min.toString)
+ def greaterThan[T <% Ordered[T]](fieldName: String, min: T, messageFormat: String = "%%s must be greater than %s."): Validator[T] =
+ new PredicateValidator[T](fieldName, _ > min, messageFormat format min.toString)
/**
* Must be less than the max param.
*/
- def lessThan[T <% Ordered[T]](fieldName: String, max: T): Validator[T] =
- new PredicateValidator[T](fieldName, _ < max, "%s must be less than " + max.toString)
+ def lessThan[T <% Ordered[T]](fieldName: String, max: T, messageFormat: String = "%%s must be less than %s."): Validator[T] =
+ new PredicateValidator[T](fieldName, _ < max, messageFormat format max.toString)
/**
* Must be greater than or equal to the min param.
*/
- def greaterThanOrEqualTo[T <% Ordered[T]](fieldName: String, min: T): Validator[T] =
- new PredicateValidator[T](fieldName, _ >= min, "%s must be greater than or equal to " + min.toString)
+ def greaterThanOrEqualTo[T <% Ordered[T]](fieldName: String, min: T, messageFormat: String = "%%s must be greater than or equal to %s."): Validator[T] =
+ new PredicateValidator[T](fieldName, _ >= min, messageFormat format min)
/**
* Must be less than or equal to the max param.
*/
- def lessThanOrEqualTo[T <% Ordered[T]](fieldName: String, max: T): Validator[T] =
- new PredicateValidator[T](fieldName, _ <= max, "%s must be less than or equal to " + max.toString)
+ def lessThanOrEqualTo[T <% Ordered[T]](fieldName: String, max: T, messageFormat: String = "%%s must be less than or equal to %s."): Validator[T] =
+ new PredicateValidator[T](fieldName, _ <= max, messageFormat.format(max))
/**
* Must have a minimum length of min.
*/
- def minLength(fieldName: String, min: Int): Validator[String] =
+ def minLength(fieldName: String, min: Int, messageFormat: String = "%%s must be at least %s characters long."): Validator[String] =
new PredicateValidator[String](
- fieldName, _.size >= min, "%s must be at least " + min.toString + " characters long.")
+ fieldName, _.size >= min, messageFormat.format(min))
/**
* Must be included in the expected collection.
*/
- def oneOf[TResult](fieldName: String, expected: TResult*): Validator[TResult] =
+ def oneOf[TResult](fieldName: String, messageFormat: String = "%%s must be one of %s.", expected: Seq[TResult]): Validator[TResult] =
new PredicateValidator[TResult](
- fieldName, expected.contains, "%s must be one of " + expected.mkString("[", ", ", "]"))
+ fieldName, expected.contains, messageFormat format expected.mkString("[", ", ", "]"))
/**
* Checks if the value of the data is a value of the specified enum.
*/
- def enumValue(fieldName: String, enum: Enumeration): Validator[String] =
- oneOf(fieldName, enum.values.map(_.toString).toSeq: _*)
+ def enumValue(fieldName: String, enum: Enumeration, messageFormat: String = "%%s must be one of %s."): Validator[String] =
+ oneOf(fieldName, messageFormat, enum.values.map(_.toString).toSeq)
- private def buildUrlValidator(fieldName: String, absolute: Boolean, allowLocalHost: Boolean, schemes: String*): Validator[String] = {
+ private def buildUrlValidator(fieldName: String, absolute: Boolean, allowLocalHost: Boolean, messageFormat: String = "%s must be a valid url.", schemes: Seq[String]): Validator[String] = {
val validator = (url: String) {
(allCatch opt {
val u = URI.create(url).normalize()
!absolute || u.isAbsolute
}).isDefined && (allowLocalHost || UrlValidator.getInstance().isValid(url))
}
- new PredicateValidator[String](fieldName, validator, "%s must be a valid url.")
+ new PredicateValidator[String](fieldName, validator, messageFormat)
}
}
View
6 commands/src/test/scala/org/scalatra/commands/CommandSpec.scala
@@ -16,11 +16,11 @@ import org.scalatra.util
trait BindingTemplate { self: Command with TypeConverterFactories =>
- val upperCaseName: Field[String] = bind[String]("name").transform(_.toUpperCase)
+ val upperCaseName: Field[String] = bind[String]("name").transform(_.toUpperCase).optional
- val lowerCaseSurname: Field[String] = asString("surname").transform(_.toLowerCase)
+ val lowerCaseSurname: Field[String] = asString("surname").transform(_.toLowerCase).optional
- val age: Field[Int] = asType[Int]("age") // explicit
+ val age: Field[Int] = asType[Int]("age").optional // explicit
val cap: Field[Int] = "cap" // implicit
View
4 commands/src/test/scala/org/scalatra/validation/ValidationSupportSpec.scala
@@ -9,9 +9,9 @@ import Scalaz._
import org.scalatra.commands.BindingSyntax._
class WithValidation extends WithBindingFromParams {
- val notRequiredCap: Field[Int] = asInt("cap").greaterThan(100)
+ val notRequiredCap: Field[Int] = asInt("cap").optional.greaterThan(100)
- val legalAge: Field[Int] = asInt("age").required.greaterThanOrEqualTo(18)
+ val legalAge: Field[Int] = asInt("age").greaterThanOrEqualTo(18)
}
View
6 project/build.scala
@@ -246,9 +246,9 @@ object ScalatraBuild extends Build {
object Dependencies {
// Sort by artifact ID.
- lazy val akkaActor: MM = sv => "com.typesafe.akka" %% "akka-actor" % akkaVersion(sv)
- lazy val akkaTestkit: MM = sv => "com.typesafe.akka" %% "akka-testkit" % akkaVersion(sv)
- lazy val atmosphereRuntime = "org.atmosphere" % "atmosphere-runtime" % "1.0.9"
+ lazy val akkaActor: MM = sv => "com.typesafe.akka" % "akka-actor" % akkaVersion(sv)
+ lazy val akkaTestkit: MM = sv => "com.typesafe.akka" % "akka-testkit" % akkaVersion(sv)
+ lazy val atmosphereRuntime = "org.atmosphere" % "atmosphere-runtime" % "1.1.0.beta3"
lazy val atmosphereClient = "org.atmosphere" % "wasync" % "1.0.0.beta1"
lazy val base64 = "net.iharder" % "base64" % "2.3.8"
lazy val commonsFileupload = "commons-fileupload" % "commons-fileupload" % "1.2.2"
View
6 swagger-ext/src/test/scala/org/scalatra/swagger/SwaggerCommandSupportSpec.scala
@@ -10,7 +10,7 @@ import test.specs2.MutableScalatraSpec
object SwaggerCommandSupportSpec {
class SimpleCommand extends ParamsOnlyCommand {
val name: Field[String] = asString("name").notBlank
- val age: Field[Int] = "age"
+ val age: Field[Int] = bind[Int]("age").optional
}
class FullCommand extends JsonCommand {
@@ -18,14 +18,14 @@ object SwaggerCommandSupportSpec {
import ValueSource._
val name: Field[String] = asString("name").notBlank
- val age: Field[Int] = "age"
+ val age: Field[Int] = bind[Int]("age").withDefaultValue(0)
val token: Field[String] = (
asString("API-TOKEN").notBlank
sourcedFrom Header
description "The API token for this request"
notes "Invalid data kills kittens"
allowableValues "123")
- val skip: Field[Int] = asInt("skip").sourcedFrom(Query).description("The offset for this collection index")
+ val skip: Field[Int] = asInt("skip").withDefaultValue(0).sourcedFrom(Query).description("The offset for this collection index")
val limit: Field[Int] = asType[Int]("limit").sourcedFrom(Query).withDefaultValue(20).description("the max number of items to return")
}
Please sign in to comment.
Something went wrong with that request. Please try again.