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

com.ancientlightstudios.quarkus.kotlin.openapi.emitter.statements.ValidationUtils.kt Maven / Gradle / Ivy

There is a newer version: 0.4.14
Show newest version
package com.ancientlightstudios.quarkus.kotlin.openapi.emitter.statements

import com.ancientlightstudios.quarkus.kotlin.openapi.emitter.CodeWriter
import com.ancientlightstudios.quarkus.kotlin.openapi.models.kotlin.expression.Expression
import com.ancientlightstudios.quarkus.kotlin.openapi.models.kotlin.expression.StringExpression.Companion.stringExpression
import com.ancientlightstudios.quarkus.kotlin.openapi.models.openapi.schema.validation.*
import com.ancientlightstudios.quarkus.kotlin.openapi.models.transformed.name.MethodName.Companion.methodName

fun CodeWriter.render(valueTransform: (String) -> Expression, validations: List) {
    renderStringValidation(validations)
    renderNumberValidation(valueTransform, validations)
    renderArrayValidation(validations)
    renderCustomConstraintsValidation(validations)
}

private fun CodeWriter.renderStringValidation(validations: List) {
    val stringValidations = validations.filterIsInstance()

    if (stringValidations.isNotEmpty()) {
        write(".validateString ")
        block(newLineAfter = true) {
            stringValidations.forEach {
                if (it.minLength != null) {
                    writeln("it.minLength(${it.minLength})")
                }
                if (it.maxLength != null) {
                    writeln("it.maxLength(${it.maxLength})")
                }
                if (it.pattern != null) {
                    writeln("it.pattern(${it.pattern.stringExpression().evaluate()})")
                }
            }
        }
    }
}

private fun CodeWriter.renderNumberValidation(valueTransform: (String) -> Expression, validations: List) {
    val numberValidations = validations.filterIsInstance()

    if (numberValidations.isNotEmpty()) {
        write(".validateNumber ")
        block(newLineAfter = true) {
            numberValidations.forEach {
                if (it.minimum != null) {
                    writeln("it.minimum(${valueTransform(it.minimum.value).evaluate()}, ${it.minimum.exclusive})")
                }
                if (it.maximum != null) {
                    writeln("it.maximum(${valueTransform(it.maximum.value).evaluate()}, ${it.maximum.exclusive})")
                }
            }
        }
    }
}

private fun CodeWriter.renderArrayValidation(validations: List) {
    val arrayValidations = validations.filterIsInstance()

    if (arrayValidations.isNotEmpty()) {
        write(".validateList ")
        block(newLineAfter = true) {
            arrayValidations.forEach {
                if (it.minSize != null) {
                    writeln("it.minSize(${it.minSize})")
                }
                if (it.maxSize != null) {
                    writeln("it.maxSize(${it.maxSize})")
                }
            }
        }
    }
}

private fun CodeWriter.renderCustomConstraintsValidation(validations: List) {
    val customConstraintsValidations = validations.filterIsInstance()
        .flatMap { it.constraints }

    customConstraintsValidations.forEach {
        val methodName = "validate".methodName().extend(postfix = it)
        writeln(".validate(DefaultValidator::${methodName.render()})")
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy