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

commonMain.schemas.CharSchema.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 io.kform.schemas.util.comparableBoundsRestrictions
import kotlin.reflect.KClass
import kotlin.reflect.KType

/** Schema representing values of type [Char]. */
public open class CharSchema(
    validations: Iterable> = emptyList(),
    override val initialValue: Char = 0.toChar()
) : AbstractSimpleSchema(validations) {
    public constructor(
        vararg validations: Validation,
        initialValue: Char = 0.toChar()
    ) : this(validations.toList(), initialValue)

    override val typeInfo: TypeInfo =
        TypeInfo(
            Char::class,
            restrictions =
                commonRestrictions(validations) +
                    comparableBoundsRestrictions(validations, Char.MIN_VALUE, Char.MAX_VALUE)
        )

    override fun assignableTo(type: KType): Boolean =
        (type.classifier as? KClass<*>)?.isInstance(0.toChar()) == true

    override suspend fun fromAny(value: Any?): Char =
        when (value) {
            is Char -> value
            is Number -> value.toInt().toChar()
            is String -> value.single()
            else -> throw IllegalArgumentException("Cannot convert value '$value' to Char.")
        }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy