Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add expected value to RuleViolation #107

Closed
frobs opened this issue Feb 23, 2017 · 1 comment
Closed

Add expected value to RuleViolation #107

frobs opened this issue Feb 23, 2017 · 1 comment
Assignees

Comments

@frobs
Copy link

frobs commented Feb 23, 2017

Hello.
I think that will be good add to RuleViolation class a field with the expected value.
With this value we can set our custom error messages:

//our applicationErrorCodes
object Code extends Enumeration {
    type ErrorCodes = String
    val greaterOrEqual = "number.mustBe.greaterOrEqual"
  }

//Our messages, maybe internationaliced maybe not
  val message: Map[String, String] = Map(
    Code.greaterOrEqual -> "Number %s must be greater than or equal to %s",
  )

//our validator
val oneValidator = validator[ObjectToBeValidated] { element =>
    element.quantity as Code.greaterOrEqual should be >= 0.0
  }

private def getValidationError(ruleViolation: RuleViolation): ValidationError = {
    val code = Descriptions.render(ruleViolation.description)
    ValidationError(code = code, message = String.format(ValidationError.message(code), ruleViolation.value.toString(),ruleViolation.expectedValue.toString()), receivedValue = ruleViolation.value.toString, expectedValue = ruleViolation.expectedValue)
  }

private def toErrorSeq(errors: Failure): Seq[ValidationError] = {
    errors.violations.map(error => {
      error match {
        case error: RuleViolation => errors :+= getValidationError(error)
      }
    })
  }

val validationResult = validate(objectToBeValidated)(oneValidator)
if(validationResult.isFailure){
toErrorSeq(result.asInstanceOf[Failure])
}

This is a simplified example but this is the idea

@frobs frobs changed the title Add expected value to RuleValidation Add expected value to RuleViolation Feb 23, 2017
@holograph
Copy link
Contributor

This is essentially the same as #21, which I expect to give full attention to in the next few weeks as part of 0.7. I'm still trying to get a clean API out of the whole thing, but you can weigh in (and/or track progress) in that issue.

@holograph holograph self-assigned this Feb 26, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants