com.commercetools.rmf.validators.UriParameterDeclaredRule.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ctp-validators Show documentation
Show all versions of ctp-validators Show documentation
RAML API client code generators based on the REST Modeling Framework. https://github.com/vrapio/rest-modeling-framework
package com.commercetools.rmf.validators
import io.vrap.rmf.raml.model.resources.Resource
import org.eclipse.emf.common.util.Diagnostic
import java.util.*
@ValidatorSet
class UriParameterDeclaredRule(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 caseResource(resource: Resource): List {
val validationResults: MutableList = ArrayList()
if (exclude.contains(resource.fullUri.template).not() && resource.relativeUri.variables.filter { v -> resource.uriParameters.firstOrNull { p -> p.name == v } == null }.size > 0 ) {
validationResults.add(create(resource, "Resource \"{0}\" must define all uri parameters", resource.fullUri.template))
}
return validationResults
}
companion object : ValidatorFactory {
private val defaultExcludes by lazy { emptyList() }
@JvmStatic
override fun create(options: List?): UriParameterDeclaredRule {
return UriParameterDeclaredRule(RuleSeverity.ERROR, options)
}
@JvmStatic
override fun create(severity: RuleSeverity, options: List?): UriParameterDeclaredRule {
return UriParameterDeclaredRule(severity, options)
}
}
}