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

commonMain.schemas.FloatSchema.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 numeric values of type [Float]. */
public open class FloatSchema(
    validations: Iterable> = emptyList(),
    override val initialValue: Float = 0f
) : AbstractSimpleSchema(validations) {
    public constructor(
        vararg validations: Validation,
        initialValue: Float = 0f
    ) : this(validations.toList(), initialValue)

    override val typeInfo: TypeInfo =
        TypeInfo(
            Float::class,
            restrictions =
                commonRestrictions(validations) + comparableBoundsRestrictions(validations)
        )

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

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




© 2015 - 2024 Weber Informatics LLC | Privacy Policy