All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonMain.schemas.BooleanSchema.kt Maven / Gradle / Ivy

There is a newer version: 0.23.0
Show newest version
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.")
        }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy