feat: constraints (#409)
This commit is contained in:
@ -7,6 +7,11 @@ data class ArrayDefinition(
|
||||
val items: JsonSchema,
|
||||
override val deprecated: Boolean? = null,
|
||||
override val description: String? = null,
|
||||
|
||||
// Constraints
|
||||
val maxItems: Int? = null,
|
||||
val minItems: Int? = null,
|
||||
val uniqueItems: Boolean? = null,
|
||||
) : JsonSchema {
|
||||
val type: String = "array"
|
||||
}
|
||||
|
@ -1,5 +1,6 @@
|
||||
package io.bkbn.kompendium.json.schema.definition
|
||||
|
||||
import io.bkbn.kompendium.json.schema.util.Serializers
|
||||
import kotlinx.serialization.Contextual
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
@ -12,6 +13,30 @@ data class TypeDefinition(
|
||||
@Contextual val default: Any? = null,
|
||||
override val deprecated: Boolean? = null,
|
||||
override val description: String? = null,
|
||||
// Constraints
|
||||
|
||||
// Number
|
||||
@Serializable(with = Serializers.Number::class)
|
||||
val multipleOf: Number? = null,
|
||||
@Serializable(with = Serializers.Number::class)
|
||||
val maximum: Number? = null,
|
||||
@Serializable(with = Serializers.Number::class)
|
||||
val exclusiveMaximum: Number? = null,
|
||||
@Serializable(with = Serializers.Number::class)
|
||||
val minimum: Number? = null,
|
||||
@Serializable(with = Serializers.Number::class)
|
||||
val exclusiveMinimum: Number? = null,
|
||||
|
||||
// String
|
||||
val maxLength: Int? = null,
|
||||
val minLength: Int? = null,
|
||||
val pattern: String? = null,
|
||||
val contentEncoding: String? = null,
|
||||
val contentMediaType: String? = null,
|
||||
|
||||
// Object
|
||||
val maxProperties: Int? = null,
|
||||
val minProperties: Int? = null,
|
||||
) : JsonSchema {
|
||||
|
||||
fun withDefault(default: Any): TypeDefinition = this.copy(default = default)
|
||||
|
@ -169,12 +169,33 @@ object SimpleObjectHandler {
|
||||
|
||||
private fun PropertyEnrichment.applyToSchema(schema: JsonSchema): JsonSchema = when (schema) {
|
||||
is AnyOfDefinition -> schema.copy(deprecated = deprecated, description = description)
|
||||
is ArrayDefinition -> schema.copy(deprecated = deprecated, description = description)
|
||||
is ArrayDefinition -> schema.copy(
|
||||
deprecated = deprecated,
|
||||
description = description,
|
||||
minItems = minItems,
|
||||
maxItems = maxItems,
|
||||
uniqueItems = uniqueItems,
|
||||
)
|
||||
is EnumDefinition -> schema.copy(deprecated = deprecated, description = description)
|
||||
is MapDefinition -> schema.copy(deprecated = deprecated, description = description)
|
||||
is NullableDefinition -> schema.copy(deprecated = deprecated, description = description)
|
||||
is OneOfDefinition -> schema.copy(deprecated = deprecated, description = description)
|
||||
is ReferenceDefinition -> schema.copy(deprecated = deprecated, description = description)
|
||||
is TypeDefinition -> schema.copy(deprecated = deprecated, description = description)
|
||||
is TypeDefinition -> schema.copy(
|
||||
deprecated = deprecated,
|
||||
description = description,
|
||||
multipleOf = multipleOf,
|
||||
maximum = maximum,
|
||||
exclusiveMaximum = exclusiveMaximum,
|
||||
minimum = minimum,
|
||||
exclusiveMinimum = exclusiveMinimum,
|
||||
maxLength = maxLength,
|
||||
minLength = minLength,
|
||||
pattern = pattern,
|
||||
contentEncoding = contentEncoding,
|
||||
contentMediaType = contentMediaType,
|
||||
maxProperties = maxProperties,
|
||||
minProperties = minProperties,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,43 @@
|
||||
package io.bkbn.kompendium.json.schema.util
|
||||
|
||||
import kotlinx.serialization.KSerializer
|
||||
import kotlinx.serialization.descriptors.PrimitiveKind
|
||||
import kotlinx.serialization.descriptors.PrimitiveSerialDescriptor
|
||||
import kotlinx.serialization.descriptors.SerialDescriptor
|
||||
import kotlinx.serialization.encoding.Decoder
|
||||
import kotlinx.serialization.encoding.Encoder
|
||||
import java.util.UUID
|
||||
import kotlin.Number as KNumber
|
||||
|
||||
object Serializers {
|
||||
|
||||
object Uuid : KSerializer<UUID> {
|
||||
override val descriptor = PrimitiveSerialDescriptor("UUID", PrimitiveKind.STRING)
|
||||
|
||||
override fun deserialize(decoder: Decoder): UUID {
|
||||
return UUID.fromString(decoder.decodeString())
|
||||
}
|
||||
|
||||
override fun serialize(encoder: Encoder, value: UUID) {
|
||||
encoder.encodeString(value.toString())
|
||||
}
|
||||
}
|
||||
|
||||
object Number : KSerializer<KNumber> {
|
||||
override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor("Number", PrimitiveKind.STRING)
|
||||
|
||||
override fun deserialize(decoder: Decoder): KNumber {
|
||||
TODO("Not yet implemented")
|
||||
}
|
||||
|
||||
override fun serialize(encoder: Encoder, value: KNumber) {
|
||||
when (value) {
|
||||
is Int -> encoder.encodeInt(value)
|
||||
is Long -> encoder.encodeLong(value)
|
||||
is Double -> encoder.encodeDouble(value)
|
||||
is Float -> encoder.encodeFloat(value)
|
||||
else -> throw IllegalArgumentException("Number is not a valid type")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user