-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
123 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
ctp-validators/src/main/kotlin/com/commercetools/rmf/validators/PolymorphicSubtypesRule.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package com.commercetools.rmf.validators | ||
|
||
import io.vrap.rmf.raml.model.modules.Library | ||
import io.vrap.rmf.raml.model.types.AnyType | ||
import io.vrap.rmf.raml.model.types.BuiltinType | ||
import io.vrap.rmf.raml.model.types.ObjectType | ||
import io.vrap.rmf.raml.model.types.TypeTemplate | ||
import org.eclipse.emf.common.util.Diagnostic | ||
import java.util.* | ||
|
||
//@ValidatorSet | ||
class PolymorphicSubtypesRule(severity: RuleSeverity, options: List<RuleOption>? = null) : TypesRule(severity, options) { | ||
|
||
private val exclude: List<String> = | ||
(options?.filter { ruleOption -> ruleOption.type.lowercase(Locale.getDefault()) == RuleOptionType.EXCLUDE.toString() }?.map { ruleOption -> ruleOption.value }?.plus("") ?: defaultExcludes) | ||
|
||
override fun caseAnyType(type: AnyType): List<Diagnostic> { | ||
val validationResults: MutableList<Diagnostic> = ArrayList() | ||
|
||
if (exclude.contains(type.name).not() && type is ObjectType && type.subTypes.filterNot { it.isInlineType }.any() && type.discriminator == null) { | ||
validationResults.add(create(type, "Type \"{0}\" has subtypes but no discriminator is set", type.name)) | ||
} | ||
return validationResults | ||
} | ||
|
||
companion object : ValidatorFactory<PolymorphicSubtypesRule> { | ||
private val defaultExcludes by lazy { listOf("") } | ||
|
||
@JvmStatic | ||
override fun create(options: List<RuleOption>?): PolymorphicSubtypesRule { | ||
return PolymorphicSubtypesRule(RuleSeverity.ERROR, options) | ||
} | ||
|
||
@JvmStatic | ||
override fun create(severity: RuleSeverity, options: List<RuleOption>?): PolymorphicSubtypesRule { | ||
return PolymorphicSubtypesRule(severity, options) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
ctp-validators/src/test/resources/polymorphic-subtype-rule.raml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
#%RAML 1.0 | ||
title: discriminator subtype rule | ||
|
||
types: | ||
Foo: | ||
type: object | ||
discriminator: type | ||
properties: | ||
type: string | ||
SubFoo: | ||
discriminatorValue: sub | ||
type: Foo | ||
Bar: | ||
type: object | ||
description: InvalidBar | ||
properties: | ||
name: string | ||
FooBar: | ||
description: FooBar | ||
type: object | ||
properties: | ||
bar: Bar | ||
FooBar2: | ||
type: object | ||
properties: | ||
bar: | ||
description: Bar | ||
type: Bar | ||
InvalidBaz: | ||
type: object | ||
description: InvalidBar | ||
properties: | ||
name: string | ||
SubBaz: | ||
description: SubBaz | ||
type: InvalidBaz | ||
SubBaz2: | ||
type: InvalidBaz |