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

commonMain.schemas.StringSchema.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.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()
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy