com.commercetools.rmf.validators.QueryParameterCamelCaseRule.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.Method
import io.vrap.rmf.raml.model.util.StringCaseFormat
import org.eclipse.emf.common.util.Diagnostic
import java.util.*
@ValidatorSet
class QueryParameterCamelCaseRule(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 caseMethod(method: Method): List {
val validationResults: MutableList = ArrayList()
method.queryParameters.forEach { queryParameter ->
run {
if (exclude.contains(queryParameter.name).not() && queryParameter.pattern == null) {
if (!queryParameter.name.matches(Regex("^[.a-zA-Z0-9]+$"))) {
validationResults.add(create(queryParameter, "Query parameter \"{0}\" name must use alphanum and dot only", queryParameter.name))
} else if (StringCaseFormat.LOWER_CAMEL_CASE.apply(queryParameter.name.replace(".", "")) != queryParameter.name.replace(".", "")) {
validationResults.add(create(queryParameter, "Query parameter \"{0}\" must be lower camel cased", queryParameter.name))
}
}
}
}
return validationResults
}
companion object : ValidatorFactory {
private val defaultExcludes by lazy { listOf("") }
@JvmStatic
override fun create(options: List?): QueryParameterCamelCaseRule {
return QueryParameterCamelCaseRule(RuleSeverity.ERROR, options)
}
@JvmStatic
override fun create(severity: RuleSeverity, options: List?): QueryParameterCamelCaseRule {
return QueryParameterCamelCaseRule(RuleSeverity.ERROR, options)
}
}
}