com.commercetools.rmf.validators.CamelCaseRule.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.types.Property
import io.vrap.rmf.raml.model.util.StringCaseFormat
import org.eclipse.emf.common.util.Diagnostic
import java.util.*
@ValidatorSet
class CamelCaseRule(severity: RuleSeverity, options: List? = null) : TypesRule(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 caseProperty(property: Property): List {
val validationResults: MutableList = ArrayList()
val propertyName = property.name ?: ""
if (property.pattern == null && StringCaseFormat.LOWER_CAMEL_CASE.apply(propertyName) != propertyName && exclude.contains(propertyName).not()) {
validationResults.add(create(property, "Property \"{0}\" must be lower camel cased", propertyName))
}
return validationResults
}
companion object : ValidatorFactory {
private val defaultExcludes by lazy { listOf("error_description") }
@JvmStatic
override fun create(options: List?): CamelCaseRule {
return CamelCaseRule(RuleSeverity.ERROR, options)
}
@JvmStatic
override fun create(severity: RuleSeverity, options: List?): CamelCaseRule {
return CamelCaseRule(severity, options)
}
}
}