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

commonMain.schemas.LongSchema.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 [Long]. */
public open class LongSchema(
    validations: Iterable> = emptyList(),
    override val initialValue: Long = 0L
) : AbstractSimpleSchema(validations) {
    public constructor(
        vararg validations: Validation,
        initialValue: Long = 0L
    ) : this(validations.toList(), initialValue)

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

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

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




© 2015 - 2024 Weber Informatics LLC | Privacy Policy