All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.commercetools.rmf.validators.PropertyPluralRule.kt Maven / Gradle / Ivy

Go to download

RAML API client code generators based on the REST Modeling Framework. https://github.com/vrapio/rest-modeling-framework

There is a newer version: 1.0.0-20241120142200
Show newest version
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)
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy