-
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.
Merge pull request #307 from mo-alras/rules/boolean-property-name-rule
Rule: Boolean property name
- Loading branch information
Showing
5 changed files
with
137 additions
and
2 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
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
46 changes: 46 additions & 0 deletions
46
ctp-validators/src/main/kotlin/com/commercetools/rmf/validators/BooleanPropertyNameRule.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,46 @@ | ||
package com.commercetools.rmf.validators | ||
|
||
import io.vrap.rmf.raml.model.types.* | ||
import org.eclipse.emf.common.util.Diagnostic | ||
import java.util.* | ||
|
||
@ValidatorSet | ||
class BooleanPropertyNameRule(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 caseObjectType(type: ObjectType): List<Diagnostic> { | ||
val validationResults: MutableList<Diagnostic> = ArrayList() | ||
|
||
type.properties.filter { property -> property.type.isBoolean() }.forEach { property -> | ||
if (exclude.contains(property.name).not()) { | ||
if (property.name.matches(Regex("^is[A-Z].*$"))) { | ||
validationResults.add(error(type, "Property \"{0}\" of type \"{1}\" must not have \"is\" as a prefix", property.name, type.name)) | ||
} | ||
} | ||
} | ||
return validationResults | ||
} | ||
|
||
private fun AnyType.isBoolean(): Boolean { | ||
return when(this) { | ||
is BooleanType -> true | ||
else -> false | ||
} | ||
} | ||
|
||
companion object : ValidatorFactory<BooleanPropertyNameRule> { | ||
private val defaultExcludes by lazy { listOf("") } | ||
|
||
@JvmStatic | ||
override fun create(options: List<RuleOption>?): BooleanPropertyNameRule { | ||
return BooleanPropertyNameRule(RuleSeverity.ERROR, options) | ||
} | ||
|
||
@JvmStatic | ||
override fun create(severity: RuleSeverity, options: List<RuleOption>?): BooleanPropertyNameRule { | ||
return BooleanPropertyNameRule(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
49 changes: 49 additions & 0 deletions
49
ctp-validators/src/test/resources/boolean-property-name-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,49 @@ | ||
#%RAML 1.0 | ||
title: boolean property name rule | ||
|
||
annotationTypes: | ||
package: string | ||
sdkBaseUri: string | ||
|
||
baseUri: https://api.europe-west1.commercetools.com | ||
|
||
types: | ||
InvalidBoolean: | ||
(package): Common | ||
type: object | ||
properties: # invalid because it is a boolean that is prefixed with "is" | ||
isBad: | ||
description: xyz | ||
type: boolean | ||
isNotGood: boolean | ||
ValidBoolean: | ||
(package): Common | ||
type: object | ||
properties: | ||
/a-z/: # rule can't apply on dynamic properties | ||
description: xyz | ||
type: boolean | ||
good: # name is not prefixed with "is" | ||
description: xyz | ||
type: boolean | ||
alsoGood: boolean | ||
isolated: # name starts with "is" but not as a prefix | ||
description: xyz | ||
type: boolean | ||
ValidNonBoolean: | ||
(package): Common | ||
type: object | ||
properties: # type is not a boolean | ||
isFine: | ||
description: xyz | ||
type: string | ||
isAlsoFine: number | ||
ValidBooleanArray: | ||
(package): Common | ||
type: object | ||
properties: | ||
isGoods: | ||
description: xyz | ||
type: array | ||
items: boolean | ||
|