commonMain.schemas.LocalDateSchema.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
import kotlinx.datetime.LocalDate
/**
* Schema representing values of type [LocalDate] defined by
* [KotlinX DateTime](https://github.com/Kotlin/kotlinx-datetime).
*/
public open class LocalDateSchema(
validations: Iterable> = emptyList(),
override val initialValue: LocalDate = DEFAULT_INITIAL_VALUE
) : AbstractSimpleSchema(validations) {
public constructor(
vararg validations: Validation,
initialValue: LocalDate = DEFAULT_INITIAL_VALUE
) : this(validations.toList(), initialValue)
override val typeInfo: TypeInfo =
TypeInfo(
LocalDate::class,
restrictions =
commonRestrictions(validations) + comparableBoundsRestrictions(validations)
)
override fun assignableTo(type: KType): Boolean =
(type.classifier as? KClass<*>)?.isInstance(DEFAULT_INITIAL_VALUE) == true
override suspend fun fromAny(value: Any?): LocalDate =
when (value) {
is LocalDate -> value
is String -> LocalDate.parse(value)
else -> throw IllegalArgumentException("Cannot convert value '$value' to LocalDate.")
}
public companion object {
/** Default initial value (epoch in UTC). */
public val DEFAULT_INITIAL_VALUE: LocalDate = LocalDateTimeSchema.DEFAULT_INITIAL_VALUE.date
}
}