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

com.commercetools.rmf.validators.ResourceCatchAllRule.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.damnhandy.uri.template.Literal
import io.vrap.rmf.raml.model.resources.Resource
import org.eclipse.emf.common.util.Diagnostic
import java.util.*

@ValidatorSet
class ResourceCatchAllRule(severity: RuleSeverity, options: List? = null) : ResourcesRule(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 caseResource(resource: Resource): List {
        val validationResults: MutableList = ArrayList()
        val resourcePathName = resource.resourcePathName
        if (exclude.contains(resourcePathName).not() && resource.resources.filter { r -> r.relativeUri.components.filter { c -> c is Literal && c.value == "/" }.size == 1 && r.relativeUri.variables.size == 1 }.size > 1) {
            validationResults.add(create(resource, "Resource \"{0}\" define only one catch all sub resource", resourcePathName))
        }
        return validationResults
    }

    companion object : ValidatorFactory {
        private val defaultExcludes by lazy { listOf("", "inventory", "login", "me", "import", "in-store") }

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

        override fun create(severity: RuleSeverity, options: List?): ResourceCatchAllRule {
            return ResourceCatchAllRule(severity, options)
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy