com.github.iskrenyp.spockdbrepo.api.IValidatableEntity.groovy Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of spock-db-repo Show documentation
Show all versions of spock-db-repo Show documentation
A simple annotation driven local extension for Spock framework, which enables you to screen record your Specifications
The newest version!
package com.github.iskrenyp.spockdbrepo.api
import groovy.transform.TupleConstructor
import net.sf.oval.ConstraintViolation
import net.sf.oval.Validator
import net.sf.oval.configuration.Configurer
import net.sf.oval.exception.ValidationFailedException
@TupleConstructor
trait IValidatableEntity {
Boolean validated(Collection configurers = null) {
List violations = []
try {
violations = withValidator(configurers) { Validator validator -> validator.validate(this) }
} catch ( IllegalArgumentException | ValidationFailedException e) {
println("Validation for ${this.class.simpleName} failed due to: $e")
return false
}
if (!violations.empty) {
violations.each { println("Validation for ${this.class.simpleName} failed due to: $it.message, $it.invalidValue") }
return false
} else return true
}
def T withValidator(Collection configurers = null, Closure < T > consumeValidator) {
configurers ? new Validator(configurers).with(consumeValidator) : new Validator().with(consumeValidator)
}
List constraintViolations() {
try {
new Validator().validate(this)
} catch ( IllegalArgumentException | ValidationFailedException e) {
println("Validation for ${this.class.simpleName} failed due to: $e")
null
}
}
}