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

com.commercetools.rmf.validators.StringPropertySingularRule.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.Property
import io.vrap.rmf.raml.model.types.StringType
import org.eclipse.emf.common.util.Diagnostic
import java.util.*

@ValidatorSet
class StringPropertySingularRule(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 propertySingular = English.singular(propertyName)

        if (property.type is StringType && property.pattern == null && propertyName != propertySingular && exclude.contains(propertyName).not()) {

            validationResults.add(create(property, "Non array property \"{0}\" must be singular", propertyName))
        }
        return validationResults
    }


    companion object : ValidatorFactory {
        private val defaultExcludes by lazy { listOf("") }

        @JvmStatic
        override fun create(options: List?): StringPropertySingularRule {
            return StringPropertySingularRule(RuleSeverity.ERROR, options)
        }

        @JvmStatic
        override fun create(severity: RuleSeverity, options: List?): StringPropertySingularRule {
            return StringPropertySingularRule(severity, options)
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy