forked from zalando/zally
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(server): KebabCaseInPathSegmentsRule generalized to CaseCheckerR…
…ule.checkPathSegments() zalando#813
- Loading branch information
Showing
10 changed files
with
111 additions
and
77 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
16 changes: 7 additions & 9 deletions
16
server/src/main/java/de/zalando/zally/rule/zalando/KebabCaseInPathSegmentsRule.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 |
---|---|---|
@@ -1,29 +1,27 @@ | ||
package de.zalando.zally.rule.zalando | ||
|
||
import com.typesafe.config.Config | ||
import de.zalando.zally.rule.CaseChecker | ||
import de.zalando.zally.rule.api.Check | ||
import de.zalando.zally.rule.api.Context | ||
import de.zalando.zally.rule.api.Rule | ||
import de.zalando.zally.rule.api.Severity | ||
import de.zalando.zally.rule.api.Violation | ||
import de.zalando.zally.util.PatternUtil | ||
|
||
@Rule( | ||
ruleSet = ZalandoRuleSet::class, | ||
id = "129", | ||
severity = Severity.MUST, | ||
title = "Lowercase words with hyphens" | ||
) | ||
class KebabCaseInPathSegmentsRule { | ||
class KebabCaseInPathSegmentsRule(config: Config) { | ||
|
||
private val checker = CaseChecker.load(config) | ||
|
||
private val description = "Use lowercase separate words with hyphens for path segments" | ||
internal val lowerCaseHyphenSeparatedRegex = """^[a-z-]+$""".toRegex() | ||
internal val lowerCaseHyphenSeparatedRegex = "^[a-z-]+$".toRegex() | ||
|
||
@Check(severity = Severity.MUST) | ||
fun checkKebabCaseInPathSegments(context: Context): List<Violation> = | ||
context.api.paths.orEmpty().entries | ||
.filter { (path, _) -> | ||
path.split("/").filterNot { it.isEmpty() } | ||
.any { segment -> !PatternUtil.isPathVariable(segment) && !segment.matches(lowerCaseHyphenSeparatedRegex) } | ||
} | ||
.map { (_, pathEntry) -> context.violation(description, pathEntry) } | ||
checker.checkPathSegments(context).map { Violation(description, it.pointer!!) } | ||
} |
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
3 changes: 2 additions & 1 deletion
3
server/src/test/java/de/zalando/zally/rule/zalando/KebabCaseInPathSegmentsRuleTest.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
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