Skip to content

Commit

Permalink
Add a KDoc comments for the soil.form.rule package
Browse files Browse the repository at this point in the history
  • Loading branch information
ogaclejapan committed May 26, 2024
1 parent 940dc9c commit 780ef82
Show file tree
Hide file tree
Showing 10 changed files with 483 additions and 10 deletions.
47 changes: 46 additions & 1 deletion soil-form/src/commonMain/kotlin/soil/form/rule/ArrayRule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,22 @@

package soil.form.rule

import soil.form.fieldError
import soil.form.FieldErrors
import soil.form.ValidationRule
import soil.form.ValidationRuleBuilder
import soil.form.fieldError
import soil.form.noErrors

typealias ArrayRule<V> = ValidationRule<Array<V>>
typealias ArrayRuleBuilder<V> = ValidationRuleBuilder<Array<V>>

/**
* A rule that tests the array value.
*
* @property predicate The predicate to test the array value. Returns `true` if the test passes; `false` otherwise.
* @property message The message to return when the test fails.
* @constructor Creates a new instance of [ArrayRuleTester].
*/
class ArrayRuleTester<V>(
val predicate: Array<V>.() -> Boolean,
val message: () -> String
Expand All @@ -21,14 +28,52 @@ class ArrayRuleTester<V>(
}
}

/**
* Validates that the array is not empty.
*
* Usage:
* ```kotlin
* rules<Array<String>> {
* notEmpty { "must be not empty" }
* }
* ```
*
* @param message The message to return when the test fails.
*/
fun <V> ArrayRuleBuilder<V>.notEmpty(message: () -> String) {
extend(ArrayRuleTester(Array<V>::isNotEmpty, message))
}

/**
* Validates that the array size is at least [limit].
*
* Usage:
* ```kotlin
* rules<Array<String>> {
* minSize(3) { "must have at least 3 items" }
* }
* ```
*
* @param limit The minimum number of elements the array must have.
* @param message The message to return when the test fails.
*/
fun <V> ArrayRuleBuilder<V>.minSize(limit: Int, message: () -> String) {
extend(ArrayRuleTester({ size >= limit }, message))
}

/**
* Validates that the array size is no more than [limit].
*
* Usage:
* ```kotlin
* rules<Array<String>> {
* maxSize(20) { "must have at no more 20 items" }
* }
* ```
*
* @param limit The maximum number of elements the array can have.
* @param message The message to return when the test fails.
*/
fun <V> ArrayRuleBuilder<V>.maxSize(limit: Int, message: () -> String) {
extend(ArrayRuleTester({ size <= limit }, message))
}
33 changes: 32 additions & 1 deletion soil-form/src/commonMain/kotlin/soil/form/rule/BooleanRule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,22 @@

package soil.form.rule

import soil.form.fieldError
import soil.form.FieldErrors
import soil.form.ValidationRule
import soil.form.ValidationRuleBuilder
import soil.form.fieldError
import soil.form.noErrors

typealias BooleanRule = ValidationRule<Boolean>
typealias BooleanRuleBuilder = ValidationRuleBuilder<Boolean>

/**
* A rule that tests the boolean value.
*
* @property predicate The predicate to test the boolean value. Returns `true` if the test passes; `false` otherwise.
* @property message The message to return when the test fails.
* @constructor Creates a new instance of [BooleanRuleTester].
*/
class BooleanRuleTester(
val predicate: Boolean.() -> Boolean,
val message: () -> String
Expand All @@ -21,10 +28,34 @@ class BooleanRuleTester(
}
}

/**
* Validates that the boolean value is `true`.
*
* Usage:
* ```kotlin
* rules<Boolean> {
* isTrue { "must be true" }
* }
* ```
*
* @param message The message to return when the test fails.
*/
fun BooleanRuleBuilder.isTrue(message: () -> String) {
extend(BooleanRuleTester({ this }, message))
}

/**
* Validates that the boolean value is `false`.
*
* Usage:
* ```kotlin
* rules<Boolean> {
* isFalse { "must be false" }
* }
* ```
*
* @param message The message to return when the test fails.
*/
fun BooleanRuleBuilder.isFalse(message: () -> String) {
extend(BooleanRuleTester({ !this }, message))
}
49 changes: 48 additions & 1 deletion soil-form/src/commonMain/kotlin/soil/form/rule/CollectionRule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,24 @@

package soil.form.rule

import soil.form.fieldError
import soil.form.FieldErrors
import soil.form.ValidationRule
import soil.form.ValidationRuleBuilder
import soil.form.fieldError
import soil.form.noErrors

// TODO: ListRule, SetRule, MapRule

typealias CollectionRule<V> = ValidationRule<Collection<V>>
typealias CollectionRuleBuilder<V> = ValidationRuleBuilder<Collection<V>>

/**
* A rule that tests the collection value.
*
* @property predicate The predicate to test the collection value. Returns `true` if the test passes; `false` otherwise.
* @property message The message to return when the test fails.
* @constructor Creates a new instance of [CollectionRuleTester].
*/
class CollectionRuleTester<V>(
val predicate: Collection<V>.() -> Boolean,
val message: () -> String
Expand All @@ -21,14 +30,52 @@ class CollectionRuleTester<V>(
}
}

/**
* Validates that the collection is not empty.
*
* Usage:
* ```kotlin
* rules<Collection<String>> {
* notEmpty { "must not be empty" }
* }
* ```
*
* @param message The message to return when the test fails.
*/
fun <V> CollectionRuleBuilder<V>.notEmpty(message: () -> String) {
extend(CollectionRuleTester(Collection<V>::isNotEmpty, message))
}

/**
* Validates that the collection size is at least [limit].
*
* Usage:
* ```kotlin
* rules<Collection<String>> {
* minSize(3) { "must have at least 3 items" }
* }
* ```
*
* @param limit The minimum number of elements the collection must have.
* @param message The message to return when the test fails.
*/
fun <V> CollectionRuleBuilder<V>.minSize(limit: Int, message: () -> String) {
extend(CollectionRuleTester({ size >= limit }, message))
}

/**
* Validates that the collection size is no more than [limit].
*
* Usage:
* ```kotlin
* rules<Collection<String>> {
* maxSize(20) { "must have at no more 20 items" }
* }
* ```
*
* @param limit The maximum number of elements the collection can have.
* @param message The message to return when the test fails.
*/
fun <V> CollectionRuleBuilder<V>.maxSize(limit: Int, message: () -> String) {
extend(CollectionRuleTester({ size <= limit }, message))
}
59 changes: 58 additions & 1 deletion soil-form/src/commonMain/kotlin/soil/form/rule/DoubleRule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,22 @@

package soil.form.rule

import soil.form.fieldError
import soil.form.FieldErrors
import soil.form.ValidationRule
import soil.form.ValidationRuleBuilder
import soil.form.fieldError
import soil.form.noErrors

typealias DoubleRule = ValidationRule<Double>
typealias DoubleRuleBuilder = ValidationRuleBuilder<Double>

/**
* A rule that tests the double value.
*
* @property predicate The predicate to test the double value. Returns `true` if the test passes; `false` otherwise.
* @property message The message to return when the test fails.
* @constructor Creates a new instance of [DoubleRuleTester].
*/
class DoubleRuleTester(
val predicate: Double.() -> Boolean,
val message: () -> String
Expand All @@ -21,18 +28,68 @@ class DoubleRuleTester(
}
}

/**
* Validates that the double value is greater than or equal to [limit].
*
* Usage:
* ```kotlin
* rules<Double> {
* minimum(3.0) { "must be greater than or equal to 3.0" }
* }
* ```
*
* @param limit The minimum value the double must have.
* @param message The message to return when the test fails.
*/
fun DoubleRuleBuilder.minimum(limit: Double, message: () -> String) {
extend(DoubleRuleTester({ this >= limit }, message))
}

/**
* Validates that the double value is less than or equal to [limit].
*
* Usage:
* ```kotlin
* rules<Double> {
* maximum(20.0) { "must be less than or equal to 20.0" }
* }
* ```
*
* @param limit The maximum value the double can have.
* @param message The message to return when the test fails.
*/
fun DoubleRuleBuilder.maximum(limit: Double, message: () -> String) {
extend(DoubleRuleTester({ this <= limit }, message))
}

/**
* Validates that the double value is `NaN`.
*
* Usage:
* ```kotlin
* rules<Double> {
* isNaN { "must be NaN" }
* }
* ```
*
* @param message The message to return when the test fails.
*/
fun DoubleRuleBuilder.isNaN(message: () -> String) {
extend(DoubleRuleTester({ isNaN() }, message))
}

/**
* Validates that the double value is not `NaN`.
*
* Usage:
* ```kotlin
* rules<Double> {
* notNaN { "must be not NaN" }
* }
* ```
*
* @param message The message to return when the test fails.
*/
fun DoubleRuleBuilder.notNaN(message: () -> String) {
extend(DoubleRuleTester({ !isNaN() }, message))
}
Loading

0 comments on commit 780ef82

Please sign in to comment.