commonMain.schemas.BooleanSchema.kt Maven / Gradle / Ivy
package io.kform.schemas
import io.kform.TypeInfo
import io.kform.Validation
import io.kform.schemas.util.commonRestrictions
import kotlin.reflect.KClass
import kotlin.reflect.KType
/** Schema representing values of type [Boolean]. */
public open class BooleanSchema(
validations: Iterable> = emptyList(),
override val initialValue: Boolean = false
) : AbstractSimpleSchema(validations) {
public constructor(
vararg validations: Validation,
initialValue: Boolean = false
) : this(validations.toList(), initialValue)
override val typeInfo: TypeInfo =
TypeInfo(Boolean::class, restrictions = commonRestrictions(validations))
override fun assignableTo(type: KType): Boolean =
(type.classifier as? KClass<*>)?.isInstance(false) == true
override suspend fun fromAny(value: Any?): Boolean =
when (value) {
is Boolean -> value
is String -> value.toBoolean()
else -> throw IllegalArgumentException("Cannot convert value '$value' to Boolean.")
}
}