com.ancientlightstudios.quarkus.kotlin.openapi.MaybeValidation.kt Maven / Gradle / Ivy
package com.ancientlightstudios.quarkus.kotlin.openapi
import com.fasterxml.jackson.databind.JsonNode
@Suppress("unused")
fun Maybe.validate(block: DefaultValidator.(T) -> Unit): Maybe =
onNotNull {
try {
val errors = DefaultValidator(context).apply { this.block(value) }.validationErrors
if (errors.isEmpty()) {
this@validate
} else {
failure(errors)
}
} catch (_: Exception) {
failure(ValidationError("is not a valid value", context, ErrorKind.Unknown))
}
}
@Suppress("unused")
fun Maybe.validateString(block: StringValidator.(String) -> Unit): Maybe =
onNotNull {
try {
val errors = StringValidator(context).apply { this.block(value) }.validationErrors
if (errors.isEmpty()) {
this@validateString
} else {
failure(errors)
}
} catch (_: Exception) {
failure(ValidationError("is not a valid value", context, ErrorKind.Unknown))
}
}
// TODO: name should be validateByteArray, but we can't determine the correct name right now in the ValidationStatementEmitter
@Suppress("unused")
@JvmName("validateByteArray")
fun Maybe.validateString(block: ByteArrayValidator.(ByteArray) -> Unit): Maybe =
onNotNull {
try {
val errors = ByteArrayValidator(context).apply { this.block(value) }.validationErrors
if (errors.isEmpty()) {
this@validateString
} else {
failure(errors)
}
} catch (_: Exception) {
failure(ValidationError("is not a valid value", context, ErrorKind.Unknown))
}
}
@Suppress("unused")
@JvmName("validateNumber")
fun Maybe.validateNumber(block: NumberValidator.(T) -> Unit): Maybe =
onNotNull {
try {
val errors = NumberValidator(context).apply { this.block(value) }.validationErrors
if (errors.isEmpty()) {
this@validateNumber
} else {
failure(errors)
}
} catch (_: Exception) {
failure(ValidationError("is not a valid value", context, ErrorKind.Unknown))
}
}
@Suppress("unused")
@JvmName("validateULongNumber")
fun Maybe.validateNumber(block: NumberValidator.(ULong) -> Unit): Maybe =
onNotNull {
try {
val errors = NumberValidator(context).apply { this.block(value) }.validationErrors
if (errors.isEmpty()) {
this@validateNumber
} else {
failure(errors)
}
} catch (_: Exception) {
failure(ValidationError("is not a valid value", context, ErrorKind.Unknown))
}
}
@Suppress("unused")
@JvmName("validateUIntNumber")
fun Maybe.validateNumber(block: NumberValidator.(UInt) -> Unit): Maybe =
onNotNull {
try {
val errors = NumberValidator(context).apply { this.block(value) }.validationErrors
if (errors.isEmpty()) {
this@validateNumber
} else {
failure(errors)
}
} catch (_: Exception) {
failure(ValidationError("is not a valid value", context, ErrorKind.Unknown))
}
}
@Suppress("unused")
@JvmName("validateUShortNumber")
fun Maybe.validateNumber(block: NumberValidator.(UShort) -> Unit): Maybe =
onNotNull {
try {
val errors = NumberValidator(context).apply { this.block(value) }.validationErrors
if (errors.isEmpty()) {
this@validateNumber
} else {
failure(errors)
}
} catch (_: Exception) {
failure(ValidationError("is not a valid value", context, ErrorKind.Unknown))
}
}
@Suppress("unused")
@JvmName("validateUByteNumber")
fun Maybe.validateNumber(block: NumberValidator.(UByte) -> Unit): Maybe =
onNotNull {
try {
val errors = NumberValidator(context).apply { this.block(value) }.validationErrors
if (errors.isEmpty()) {
this@validateNumber
} else {
failure(errors)
}
} catch (_: Exception) {
failure(ValidationError("is not a valid value", context, ErrorKind.Unknown))
}
}
@Suppress("unused")
fun Maybe?>.validateList(block: ListValidator.(List) -> Unit): Maybe?> =
onNotNull {
try {
val errors = ListValidator(context).apply { this.block(value) }.validationErrors
if (errors.isEmpty()) {
this@validateList
} else {
failure(errors)
}
} catch (_: Exception) {
failure(ValidationError("is not a valid value", context, ErrorKind.Unknown))
}
}
@Suppress("unused")
fun Maybe
© 2015 - 2025 Weber Informatics LLC | Privacy Policy