com.commercetools.rmf.validators.PropertyPluralRule.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 com.hypertino.inflector.English
import io.vrap.rmf.raml.model.types.ArrayType
import io.vrap.rmf.raml.model.types.ObjectType
import io.vrap.rmf.raml.model.types.Property
import org.eclipse.emf.common.util.Diagnostic
import java.util.*
@ValidatorSet
class PropertyPluralRule(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 ?: ""
val singularName = English.singular(propertyName)
val pluralName = English.plural(singularName)
if (property.type is ArrayType && property.pattern == null && propertyName != pluralName && exclude.contains(propertyName).not()) {
val containerName = when(val propertyContainer = property.eContainer()) {
is ObjectType -> propertyContainer.name
else -> "unknown"
}
validationResults.add(create(property, "Array property \"{0}\" of type \"{1}\" must be plural. (singularized: {2}, pluralized: {3})", propertyName, containerName, singularName, pluralName))
}
return validationResults
}
companion object : ValidatorFactory {
private val defaultExcludes by lazy { listOf("error_description") }
@JvmStatic
override fun create(options: List?): PropertyPluralRule {
return PropertyPluralRule(RuleSeverity.ERROR, options)
}
@JvmStatic
override fun create(severity: RuleSeverity, options: List?): PropertyPluralRule {
return PropertyPluralRule(severity, options)
}
}
}