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

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

@ValidatorSet
class DiscriminatorParentRule(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 (type.discriminator != null && !type.isInlineType && exclude.contains(type.name).not()) {
            val parentType = type.type
            if (parentType != null && !BuiltinType.of(parentType.name).isPresent && parentType is ObjectType && parentType.getAllProperties().firstOrNull {  p ->  p.name == type.discriminator } != null ) {
                validationResults.add(create(type, "Discriminator property \"{0}\" must defined in the type {1} only", type.discriminator(), type.name))
            }
        }

        return validationResults
    }

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

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

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




© 2015 - 2024 Weber Informatics LLC | Privacy Policy