@@ -38,11 +38,28 @@ abstract class Validator<T> {
38
38
* @param action The action that will be executed by the validation builder.
39
39
* @return Returns a custom validator implementation.
40
40
*/
41
- inline fun <reified T : Any > validatorFor (
42
- crossinline action : ValidationBuilder <T >.() -> Unit
43
- ) = object : Validator <T >() {
44
- override val genericKotlinClass: KClass <* > get() = T ::class
45
- override fun validate (builder : ValidationBuilder <T >) = action(builder)
41
+ inline fun <reified T : Any > validatorFor (crossinline action : ValidationBuilder <T >.() -> Unit ): Validator <T > {
42
+ return object : Validator <T >() {
43
+ override val genericKotlinClass: KClass <* > get() = T ::class
44
+ override fun validate (builder : ValidationBuilder <T >) = action(builder)
45
+ }
46
+ }
47
+
48
+ /* *
49
+ * Builds and validates a subject on-the-fly.
50
+ *
51
+ * @param T The underlying subject class of the validator.
52
+ * @param subject The subject to be validated.
53
+ * @param action he action that will be executed by the validation builder.
54
+ * @throws ValidationGraphException if the validator fails.
55
+ */
56
+ inline fun <reified T : Any > validate (subject : T , crossinline action : ValidationBuilder <T >.() -> Unit ) {
57
+ val validator = validatorFor(action)
58
+ val result = validator.validate(subject)
59
+ if (result.members.isNotEmpty()) {
60
+ val message = " Validation of the specified object type failed: ${result.name} ."
61
+ throw ValidationGraphException (message, result.toList())
62
+ }
46
63
}
47
64
}
48
65
0 commit comments