commonMain.schemas.StringSchema.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.lengthBoundsRestrictions
import io.kform.schemas.util.patternRestrictions
import kotlin.reflect.KClass
import kotlin.reflect.KType
/** Schema representing values of type [String]. */
public open class StringSchema(
validations: Iterable> = emptyList(),
override val initialValue: String = ""
) : AbstractSimpleSchema(validations) {
public constructor(
vararg validations: Validation,
initialValue: String = ""
) : this(validations.toList(), initialValue)
override val typeInfo: TypeInfo =
TypeInfo(
String::class,
restrictions =
commonRestrictions(validations) +
lengthBoundsRestrictions(validations) +
patternRestrictions(validations)
)
override fun assignableTo(type: KType): Boolean =
(type.classifier as? KClass<*>)?.isInstance("") == true
override suspend fun fromAny(value: Any?): String = value.toString()
}