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

com.commercetools.rmf.validators.MethodResponseRule.kt Maven / Gradle / Ivy

Go to download

RAML API client code generators based on the REST Modeling Framework. https://github.com/vrapio/rest-modeling-framework

There is a newer version: 1.0.0-20241120142200
Show newest version
package com.commercetools.rmf.validators

import com.commercetools.rmf.diff.resource
import io.vrap.rmf.raml.model.resources.HttpMethod
import io.vrap.rmf.raml.model.resources.Method
import io.vrap.rmf.raml.model.resources.Resource
import io.vrap.rmf.raml.model.resources.ResourceType
import org.eclipse.emf.common.util.Diagnostic
import java.util.*

@ValidatorSet
class MethodResponseRule(severity: RuleSeverity, options: List? = null) : ResolvedResourcesRule(severity, options) {

    private val exclude: List =
        (options?.filter { ruleOption -> ruleOption.type.lowercase(Locale.getDefault()) == RuleOptionType.EXCLUDE.toString() }?.map { ruleOption -> ruleOption.value }?.plus("") ?: defaultExcludes)

    override fun caseMethod(method: Method): List {
        val validationResults: MutableList = ArrayList()

        if (method.eContainer() is ResourceType) {
            return validationResults
        }
        if (method.responses.isEmpty() && exclude.contains("${method.method.name} ${(method.eContainer() as Resource).fullUri.template}").not()) {
            validationResults.add(error(method, "Method \"{0} {1}\" must have at least one response defined", method.method.name, (method.eContainer() as Resource).fullUri.template))
        }
        return validationResults
    }

    companion object : ValidatorFactory {
        private val defaultExcludes by lazy { listOf("") }

        @JvmStatic
        override fun create(options: List?): MethodResponseRule {
            return MethodResponseRule(RuleSeverity.ERROR, options)
        }

        @JvmStatic
        override fun create(severity: RuleSeverity, options: List?): MethodResponseRule {
            return MethodResponseRule(severity, options)
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy