Skip to content

Commit

Permalink
validation rules should be errors by default
Browse files Browse the repository at this point in the history
  • Loading branch information
jenschude committed Oct 28, 2023
1 parent 6d620d8 commit 8274740
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,22 +1,27 @@
package com.commercetools.rmf.validators

import com.commercetools.rmf.diff.resource
import io.vrap.rmf.raml.model.resources.HttpMethod
import io.vrap.rmf.raml.model.resources.Method
import io.vrap.rmf.raml.model.resources.Resource
import io.vrap.rmf.raml.model.resources.ResourceType
import org.eclipse.emf.common.util.Diagnostic
import java.util.*

@ValidatorSet
class MethodResponseRule(severity: RuleSeverity, options: List<RuleOption>? = null) : ResourcesRule(severity, options) {
class MethodResponseRule(severity: RuleSeverity, options: List<RuleOption>? = null) : ResolvedResourcesRule(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 caseMethod(method: Method): List<Diagnostic> {
val validationResults: MutableList<Diagnostic> = ArrayList()

if (method.eContainer() is ResourceType) {
return validationResults
}
if (method.responses.isEmpty() && exclude.contains("${method.method.name} ${(method.eContainer() as Resource).fullUri.template}").not()) {
validationResults.add(create(method, "Method \"{0} {1}\" must have at least one response defined", method.method.name, (method.eContainer() as Resource).fullUri.template))
validationResults.add(error(method, "Method \"{0} {1}\" must have at least one response defined", method.method.name, (method.eContainer() as Resource).fullUri.template))
}
return validationResults
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ package com.commercetools.rmf.validators
annotation class ValidatorSets(val value: Array<ValidatorSet>)

@JvmRepeatable(ValidatorSets::class)
annotation class ValidatorSet(val name: String = "default", val severity: RuleSeverity = RuleSeverity.INFO)
annotation class ValidatorSet(val name: String = "default", val severity: RuleSeverity = RuleSeverity.ERROR)
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ import java.lang.reflect.Modifier
class ValidatorSetup {
companion object {
@JvmStatic
fun setup(config: File): List<RamlValidator> {
return setup(config.inputStream())
fun setup(config: File, verbose: Boolean = false): List<RamlValidator> {
return setup(config.inputStream(), verbose)
}

fun setup(config: InputStream): List<RamlValidator> {
fun setup(config: InputStream, verbose: Boolean = false): List<RamlValidator> {
val mapper = XmlMapper.builder(XmlFactory(WstxInputFactory(), WstxOutputFactory())).defaultUseWrapper(false)
.enable(SerializationFeature.INDENT_OUTPUT)
.enable(SerializationFeature.WRAP_ROOT_VALUE)
Expand All @@ -46,6 +46,9 @@ class ValidatorSetup {
}
}

if (verbose) {
validators.filterIsInstance(DiagnosticsAware::class.java).forEach { validator -> println("${validator::class.java}: ${validator.severity}") }
}
return listOf(
ResolvedResourcesValidator(validators.filterIsInstance( ResolvedResourcesRule::class.java )),
ResourcesValidator(validators.filterIsInstance( ResourcesRule::class.java )),
Expand All @@ -58,7 +61,7 @@ class ValidatorSetup {
var checks = setOne
setTwo.forEach { (checkName, check) ->
if (checks.containsKey(checkName)) {
checks = checks.plus(checkName to Rule(checkName, check.severity, checks[checkName]!!.options?.plus(check.options ?: listOf()) ?: check.options, check.enabled && checks[checkName]!!.enabled))
checks = checks.plus(checkName to Rule(checkName, check.severity ?: checks[checkName]!!.severity, checks[checkName]!!.options?.plus(check.options ?: listOf()) ?: check.options, check.enabled && checks[checkName]!!.enabled))
} else {
checks = checks.plus(checkName to check)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.commercetools.rmf.validators

import io.vrap.rmf.raml.model.RamlModelBuilder
import org.eclipse.emf.common.util.Diagnostic
import spock.lang.Specification
import static java.util.Collections.emptyList

Expand Down Expand Up @@ -109,6 +110,7 @@ class ValidatorRulesTest extends Specification implements ValidatorFixtures {
then:
result.validationResults.size == 1
result.validationResults[0].message == "Method \"HEAD /invalid\" must have at least one response defined"
result.validationResults[0].severity == Diagnostic.ERROR
}

// def "named body type rule"() {
Expand Down
7 changes: 7 additions & 0 deletions ctp-validators/src/test/resources/method-response-rule.raml
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
#%RAML 1.0
title: method responses

resourceTypes:
test:
get?:
responses:
200:
post?:
/carts:
get:
description: test
responses:
200:
head:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ class ValidateSubcommand : Callable<Int> {
@CommandLine.Option(names = ["--list-rules"], description = ["Show all rules"])
var listRules: Boolean = false

@CommandLine.Option(names = ["-v", "--verbose"], description = ["Verbose"])
var verbose: Boolean = false;

lateinit var modelBuilder: RamlModelBuilder

private fun linkURI(): java.net.URI {
Expand All @@ -97,7 +100,7 @@ class ValidateSubcommand : Callable<Int> {
return 0
}
val tmpDir = tempFile?.toAbsolutePath()?.normalize() ?: Paths.get(".tmp")
modelBuilder = setupValidators()
modelBuilder = setupValidators(verbose)
val res = safeRun { validate(tmpDir)}
if (watch) {
val watchDir = ramlFileLocation.toRealPath().toAbsolutePath().parent
Expand Down Expand Up @@ -182,9 +185,9 @@ class ValidateSubcommand : Callable<Int> {
return modelResult.validationResults.any { result -> result.severity >= checkSeverity.value }.let { b -> if(b) 1 else 0 }
}

private fun setupValidators(): RamlModelBuilder {
private fun setupValidators(verbose: Boolean = false): RamlModelBuilder {
val ruleset = rulesetFile?.toFile()?.inputStream() ?: ValidateSubcommand::class.java.getResourceAsStream("/ruleset.xml")
return RamlModelBuilder(ValidatorSetup.setup(ruleset))
return RamlModelBuilder(ValidatorSetup.setup(ruleset, verbose))
}

private fun diagnosticFormatter(printer: OutputFormat, filePath: Path, linkUri: java.net.URI): FormatPrinter {
Expand Down

0 comments on commit 8274740

Please sign in to comment.