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

Support receiver bound properties to validate: FR #71 #72

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
76 changes: 68 additions & 8 deletions valiktor-core/src/main/kotlin/org/valiktor/Validator.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package org.valiktor

import kotlin.reflect.KProperty
import kotlin.reflect.KProperty0
import kotlin.reflect.KProperty1

/**
Expand Down Expand Up @@ -62,11 +64,33 @@ open class Validator<E>(private val obj: E) {
/**
* Returns a [Property] for this property
*
* @receiver the property to be validated
* @receiver the class property to be validated
* @return the property validator
*
* @see KProperty1
*/
@JvmName("validate")
fun <T> validate(property: KProperty1<E, T?>): Property<T?> = Property(obj, property)
fun <T> validate(property: KProperty1<E, T?>): Property<T?> = Property1(obj, property)

/**
* Returns a [Property] for this property
*
* @receiver the object property to be validated
* @return the property validator
*
* @see KProperty0
*/
@JvmName("validate")
fun <T> validate(property: KProperty0<T?>): Property<T?> = Property0(property)

/**
* Returns a [Property] for this iterable property
*
* @receiver the property to be validated
* @return the property validator
*/
@JvmName("validateIterable")
fun <T> validate(property: KProperty1<E, Iterable<T>?>): Property<Iterable<T>?> = Property1(obj, property)

/**
* Returns a [Property] for this iterable property
Expand All @@ -75,7 +99,7 @@ open class Validator<E>(private val obj: E) {
* @return the property validator
*/
@JvmName("validateIterable")
fun <T> validate(property: KProperty1<E, Iterable<T>?>): Property<Iterable<T>?> = Property(obj, property)
fun <T> validate(property: KProperty0<Iterable<T>?>): Property<Iterable<T>?> = Property0(property)

/**
* Returns a [Property] for this array property
Expand All @@ -84,20 +108,56 @@ open class Validator<E>(private val obj: E) {
* @return the property validator
*/
@JvmName("validateArray")
fun <T> validate(property: KProperty1<E, Array<T>?>): Property<Array<T>?> = Property(obj, property)
fun <T> validate(property: KProperty1<E, Array<T>?>): Property<Array<T>?> = Property1(obj, property)

/**
* Returns a [Property] for this array property
*
* @receiver the property to be validated
* @return the property validator
*/
@JvmName("validateArray")
fun <T> validate(property: KProperty0<Array<T>?>): Property<Array<T>?> = Property0(property)

/**
* KProperty1 based implementation of a Property
*
* @param property KProperty1
*
* @author Gennadi Kudrjavtsev
* @since 0.13
* @see Property
* @see KProperty1
*/
private inner class Property1<T, E>(obj: E, property: KProperty1<E, T?>) :
Property<T>(property, { property.get(obj) })

/**
* KProperty0 based implementation of a Property
*
* @param property KProperty0
*
* @author Gennadi Kudrjavtsev
* @since 0.13
* @see Property
* @see KProperty0
*/
private inner class Property0<T>(property: KProperty0<T?>) :
Property<T>(property, property::get)

/**
* Represents a property validator that contains extended functions
*
* @param obj specifies the object to be validated
* @param property specifies the property to be validated
* @param getValue specifies the function to get value of a property
*
* @author Rodolpho S. Couto
* @author Gennadi Kudrjavtsev
* @see Validator
* @see KProperty1
* @since 0.1.0
*/
open inner class Property<T>(val obj: E, val property: KProperty1<E, T?>) {
abstract inner class Property<T>(val property: KProperty<T?>, val getValue: () -> T?) {

/**
* Validates the property by passing the constraint and the validation function
Expand All @@ -109,7 +169,7 @@ open class Validator<E>(private val obj: E) {
* @return the property validator
*/
fun validate(constraint: (T?) -> Constraint, isValid: (T?) -> Boolean): Property<T> {
val value = this.property.get(this.obj)
val value = getValue()
if (!isValid(value)) {
this@Validator.constraintViolations += DefaultConstraintViolation(
property = this.property.name,
Expand Down Expand Up @@ -142,7 +202,7 @@ open class Validator<E>(private val obj: E) {
* @return the property validator
*/
suspend fun coValidate(constraint: (T?) -> Constraint, isValid: suspend (T?) -> Boolean): Property<T> {
val value = this.property.get(this.obj)
val value = getValue()
if (!isValid(value)) {
this@Validator.constraintViolations += DefaultConstraintViolation(
property = this.property.name,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import org.valiktor.constraints.Valid
* @return the same receiver property
*/
inline fun <E, T : Any> Validator<E>.Property<T?>.validate(block: Validator<T>.(T) -> Unit): Validator<E>.Property<T?> {
val value = this.property.get(this.obj)
val value = getValue()
if (value != null) {
this.addConstraintViolations(
Validator(value).apply { block(value) }.constraintViolations.map {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ import org.valiktor.constraints.Size
inline fun <E, T> Validator<E>.Property<Array<T>?>.validateForEach(
block: Validator<T>.(T) -> Unit
): Validator<E>.Property<Array<T>?> {
this.property.get(this.obj)?.forEachIndexed { index, value ->
getValue()?.forEachIndexed { index, value ->
this.addConstraintViolations(
Validator(value).apply { block(value) }.constraintViolations.map {
DefaultConstraintViolation(
Expand All @@ -68,7 +68,7 @@ inline fun <E, T> Validator<E>.Property<Array<T>?>.validateForEach(
inline fun <E, T> Validator<E>.Property<Array<T>?>.validateForEachIndexed(
block: Validator<T>.(Int, T) -> Unit
): Validator<E>.Property<Array<T>?> {
this.property.get(this.obj)?.forEachIndexed { index, value ->
getValue()?.forEachIndexed { index, value ->
this.addConstraintViolations(
Validator(value).apply { block(index, value) }.constraintViolations.map {
DefaultConstraintViolation(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ import org.valiktor.constraints.Size
inline fun <E, T> Validator<E>.Property<Iterable<T>?>.validateForEach(
block: Validator<T>.(T) -> Unit
): Validator<E>.Property<Iterable<T>?> {
this.property.get(this.obj)?.forEachIndexed { index, value ->
getValue()?.forEachIndexed { index, value ->
this.addConstraintViolations(
Validator(value).apply { block(value) }.constraintViolations.map {
DefaultConstraintViolation(
Expand All @@ -66,7 +66,7 @@ inline fun <E, T> Validator<E>.Property<Iterable<T>?>.validateForEach(
inline fun <E, T> Validator<E>.Property<Iterable<T>?>.validateForEachIndexed(
block: Validator<T>.(Int, T) -> Unit
): Validator<E>.Property<Iterable<T>?> {
this.property.get(this.obj)?.forEachIndexed { index, value ->
getValue()?.forEachIndexed { index, value ->
this.addConstraintViolations(
Validator(value).apply { block(index, value) }.constraintViolations.map {
DefaultConstraintViolation(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,18 @@ private object AnyFunctionsFixture {
@ExperimentalCoroutinesApi
class AnyFunctionsTest {

@Test
fun `should validate by KProperty0`() {
validate(Employee(company = Company(1))) {
validate(it::company).isNotNull().validate { company ->
validate(company::id).isNotNull()
}
validate(it::id).isNull()
validate(it::address).isNull()
validate(it::name).isNull()
}
}

@Test
fun `isNull with null property should be valid`() {
validate(Employee()) {
Expand Down