com.commercetools.rmf.validators.NamedStringEnumRule.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.StringType
import org.eclipse.emf.common.util.Diagnostic
import java.util.*
@ValidatorSet
class NamedStringEnumRule(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 caseStringType(type: StringType): List {
val validationResults: MutableList = ArrayList()
if (exclude.contains(type.name).not() && type.name != "string") {
if (!type.isInlineType() && type.enum.isNullOrEmpty() && type.pattern == null) {
validationResults.add(create(type, "Named string type \"{0}\" must define enum values", type.name))
}
if (type.isInlineType() && type.type.enum.isNullOrEmpty() && (type.type as StringType).pattern == null) {
validationResults.add(create(type, "Named string type \"{0}\" must define enum values", type.name))
}
}
return validationResults
}
companion object : ValidatorFactory {
private val defaultExcludes by lazy { listOf("") }
@JvmStatic
override fun create(options: List?): NamedStringEnumRule {
return NamedStringEnumRule(RuleSeverity.ERROR, options)
}
@JvmStatic
override fun create(severity: RuleSeverity, options: List?): NamedStringEnumRule {
return NamedStringEnumRule(severity, options)
}
}
}