com.commercetools.rmf.validators.AsMapRule.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.*
import org.eclipse.emf.common.util.Diagnostic
import java.util.*
@ValidatorSet
class AsMapRule(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 caseObjectType(type: ObjectType): List {
val validationResults: MutableList = ArrayList()
if (exclude.contains(type.name).not()) {
if (type.properties.size == 1 && type.properties[0].isPatternProperty() && type.properties[0].type.isScalar()) {
if (type.getAnnotation("asMap") == null) {
validationResults.add(create(type, "Pattern property of type \"{0}\" must define an asMap annotation", type.name))
}
}
}
return validationResults
}
private fun Property.isPatternProperty() = this.name.startsWith("/") && this.name.endsWith("/")
companion object : ValidatorFactory {
private val defaultExcludes by lazy { listOf("") }
@JvmStatic
override fun create(options: List?): AsMapRule {
return AsMapRule(RuleSeverity.ERROR, options)
}
@JvmStatic
override fun create(severity: RuleSeverity, options: List?): AsMapRule {
return AsMapRule(severity, options)
}
}
private fun AnyType.isScalar(): Boolean {
return when(this) {
is StringType -> true
is IntegerType -> true
is NumberType -> true
is BooleanType -> true
is DateTimeType -> true
is DateOnlyType -> true
is DateTimeOnlyType -> true
is TimeOnlyType -> true
is ArrayType -> this.items == null || this.items.isScalar()
else -> false
}
}
}