commonMain.schemas.FloatSchema.kt Maven / Gradle / Ivy
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.")
}
}