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

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

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

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

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




© 2015 - 2024 Weber Informatics LLC | Privacy Policy