commonMain.schemas.ByteSchema.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 [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.")
}
}