to State Or Null
Create an object with a known, correct state, if all the property states are correct. Each property is validated separately, and it is also possible to validate the combination of properties.
The easiest way to use this function is to create an extension function on the input object, and then calling this function. For example:
fun User.toCompleteOrNull(): UserComplete? =
toStateOrNull(
::UserComplete,
isNotNull(name).and { length > 3 },
isNotNull(password).and { length > 10 },
isNotNull(emailAddress),
isNotNull(remarks)
) { name, password, _, _ -> !password.contains(name, true) }Return
if all property states are correct, a result object created using constructor. Otherwise null.
Parameters
the type of the result object.
(TSecond, ...) the types of the properties in the correct state.
the function used to create the result object if the properties are in the correct state.
(ps2, ...) the states of the properties based on the input.
an optional function to check if the combination of (individually correct) properties is correct.