Skip to content

Commit 2e9cb80

Browse files
Merge pull request #1 from onix-labs/feature-validate-on-the-fly
Implementation to validate on the fly.
2 parents e9590eb + 3976320 commit 2e9cb80

File tree

8 files changed

+54
-14
lines changed

8 files changed

+54
-14
lines changed

CHANGELOG.md

+10
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,16 @@
44

55
This document serves as the change log for the ONIXLabs Kotlin Validation API.
66

7+
## Version 2.0.0
8+
9+
#### ValidationGraphException (class)
10+
11+
Represents an exception that will be thrown when validation mode is set to DEFAULT.
12+
13+
#### Validator.validate (function)
14+
15+
Builds and validates a subject on-the-fly.
16+
717
## Version 1.0.0
818

919
#### MemberAssertion (class)

build.gradle

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ buildscript {
1313
jackson_datatype_version = "2.9.8"
1414

1515
onixlabs_group = "io.onixlabs"
16-
onixlabs_kotlin_core_version = "1.0.0"
16+
onixlabs_kotlin_core_version = "2.0.0"
1717
}
1818

1919
repositories {
@@ -28,7 +28,7 @@ buildscript {
2828
}
2929

3030
group 'io.onixlabs'
31-
version '1.0.0'
31+
version '2.0.0'
3232

3333
allprojects {
3434
repositories {

src/main/kotlin/io/onixlabs/kotlin/validation/MemberAssertionContextStringExtensions.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ fun MemberAssertionContext<out String?>.mustNotBeBlank(
143143
override val id: String = id.toUpperSnakeCase()
144144

145145
override fun isValid(): Boolean {
146-
return subject?.isNotBlank() ?: false
146+
return subject?.isNotBlank() ?: true
147147
}
148148
}
149149

@@ -187,7 +187,7 @@ fun MemberAssertionContext<out String?>.mustNotBeEmpty(
187187
override val id: String = id.toUpperSnakeCase()
188188

189189
override fun isValid(): Boolean {
190-
return subject?.isNotEmpty() ?: false
190+
return subject?.isNotEmpty() ?: true
191191
}
192192
}
193193

src/main/kotlin/io/onixlabs/kotlin/validation/ValidationException.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,4 @@ package io.onixlabs.kotlin.validation
2222
* @param message A message detailing the validation exception. Typically this should be the assertion message.
2323
* @param cause An underlying cause of the validation exception.
2424
*/
25-
class ValidationException(message: String, cause: Throwable? = null) : Exception(message, cause)
25+
open class ValidationException(message: String, cause: Throwable? = null) : Exception(message, cause)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package io.onixlabs.kotlin.validation
2+
3+
/**
4+
* Represents an exception that will be thrown when validation mode is set to DEFAULT.
5+
*
6+
* @param message A message detailing the validation exception. Typically this should be the assertion message.
7+
* @param failures The failures that occurred during validation.
8+
* @param cause An underlying cause of the validation exception.
9+
*/
10+
class ValidationGraphException(
11+
message: String,
12+
val failures: List<String>,
13+
override val cause: Throwable? = null
14+
) : ValidationException(message, cause)

src/main/kotlin/io/onixlabs/kotlin/validation/ValidationResult.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
package io.onixlabs.kotlin.validation
1818

19-
import io.onixlabs.kotlin.core.reflection.fullName
19+
import io.onixlabs.kotlin.core.reflection.formattedQualifiedName
2020
import kotlin.reflect.KCallable
2121
import kotlin.reflect.KClass
2222
import kotlin.reflect.KFunction
@@ -72,7 +72,7 @@ open class ValidationResult internal constructor(val name: String, protected val
7272
internal fun createMember(callable: KCallable<*>): MemberResult {
7373
return MemberResult(
7474
name = callable.name,
75-
type = callable.returnType.fullName,
75+
type = callable.returnType.formattedQualifiedName,
7676
declaration = when (callable) {
7777
is KProperty -> "Property"
7878
is KFunction -> "Function"

src/main/kotlin/io/onixlabs/kotlin/validation/Validator.kt

+22-5
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,28 @@ abstract class Validator<T> {
3838
* @param action The action that will be executed by the validation builder.
3939
* @return Returns a custom validator implementation.
4040
*/
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+
}
4663
}
4764
}
4865

src/test/kotlin/io/onixlabs/kotlin/validation/MemberAssertionContextStringExtensionTests.kt

+1-2
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ import org.junit.jupiter.api.assertThrows
2323

2424
class MemberAssertionContextStringExtensionTests {
2525

26-
private val member =
27-
"Property 'StringExtensionTestSubject.property' of type 'kotlin.String?'"
26+
private val member = "Property 'StringExtensionTestSubject.property' of type 'kotlin.String?'"
2827

2928
@Test
3029
fun `MemberAssertionContext_mustBeNullOrBlank should assert correctly on validation failure`() {

0 commit comments

Comments
 (0)