-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,14 +31,13 @@ import com.github.fge.jsonschema.core.report.{ ListProcessingReport, ProcessingM | |
|
||
// Schema DDL | ||
import com.snowplowanalytics.iglu.schemaddl.jsonschema.{ Schema, SanityLinter } | ||
import com.snowplowanalytics.iglu.schemaddl.jsonschema.SanityLinter.SeverityLevel | ||
import com.snowplowanalytics.iglu.schemaddl.jsonschema.json4s.Json4sToSchema._ | ||
|
||
// This library | ||
import FileUtils.{ getJsonFilesStream, JsonFile, filterJsonSchemas } | ||
import Utils.{ extractSchema, splitValidations } | ||
|
||
case class LintCommand(inputDir: File, skipWarnings: Boolean, severityLevel: SeverityLevel) extends Command.CtlCommand { | ||
case class LintCommand(inputDir: File, skipWarnings: Boolean, linters: List[SanityLinter.Linter]) extends Command.CtlCommand { | ||
import LintCommand._ | ||
|
||
/** | ||
|
@@ -51,36 +50,46 @@ case class LintCommand(inputDir: File, skipWarnings: Boolean, severityLevel: Sev | |
if (failures.nonEmpty) { | ||
println("JSON Parsing errors:") | ||
println(failures.mkString("\n")) | ||
sys.exit(1) | ||
} | ||
|
||
// lint schema versions | ||
val stubFile: File = new File(inputDir.getAbsolutePath) | ||
val stubCommand = GenerateCommand(stubFile, stubFile) | ||
val (_, schemas) = splitValidations(validatedJsons.map(_.extractSelfDescribingSchema)) | ||
val (schemaVerWarnings, schemaVerErrors) = stubCommand.validateSchemaVersions(schemas) | ||
val lintSchemaVer: Boolean = (schemaVerWarnings, schemaVerErrors) match { | ||
case (Nil, Nil) => true | ||
case (warnings, Nil) => | ||
println(warnings.mkString("\n")) | ||
true | ||
case (Nil, errors) => | ||
println(errors.mkString("\n")) | ||
false | ||
case _ => | ||
// this case is here only to trick compiler, | ||
// GenerateCommand.validateSchemaVersions doesn't return warnings together with errors | ||
true | ||
} | ||
if (linters.contains(SanityLinter.lintSchemaVersions)) { | ||
val stubFile: File = new File(inputDir.getAbsolutePath) | ||
val stubCommand = GenerateCommand(stubFile, stubFile) | ||
val (_, schemas) = splitValidations(validatedJsons.map(_.extractSelfDescribingSchema)) | ||
val (schemaVerWarnings, schemaVerErrors) = stubCommand.validateSchemaVersions(schemas) | ||
val lintSchemaVer: Boolean = (schemaVerWarnings, schemaVerErrors) match { | ||
case (Nil, Nil) => true | ||
case (warnings, Nil) => | ||
println(warnings.mkString("\n")) | ||
true | ||
case (Nil, errors) => | ||
println(errors.mkString("\n")) | ||
false | ||
case _ => | ||
This comment has been minimized.
Sorry, something went wrong. |
||
// this case is here only to trick compiler, | ||
// GenerateCommand.validateSchemaVersions doesn't return warnings together with errors | ||
true | ||
} | ||
|
||
if (!lintSchemaVer) { | ||
sys.exit(1) | ||
if (!lintSchemaVer) { | ||
sys.exit(1) | ||
} else { | ||
val reports = jsons.map { file => | ||
val report = file.map(check) | ||
flattenReport(report) | ||
} | ||
reports.foldLeft(Total(0, 0, 0))((acc, cur) => acc.add(cur)).exit() | ||
} | ||
} else { | ||
val reports = jsons.map { file => | ||
val report = file.map(check) | ||
flattenReport(report) | ||
} | ||
reports.foldLeft(Total(0, 0, 0))((acc, cur) => acc.add(cur)).exit() | ||
} | ||
|
||
} | ||
|
||
/** | ||
|
@@ -93,7 +102,7 @@ case class LintCommand(inputDir: File, skipWarnings: Boolean, severityLevel: Sev | |
val pathCheck = extractSchema(jsonFile).map(_ => ()).validation.toValidationNel | ||
val syntaxCheck = validateSchema(jsonFile.content, skipWarnings) | ||
|
||
val lintCheck = Schema.parse(jsonFile.content).map { schema => SanityLinter.lint(schema, severityLevel, 0) } | ||
val lintCheck = Schema.parse(jsonFile.content).map { schema => SanityLinter.lint(schema, 0, linters) } | ||
|
||
val fullValidation = syntaxCheck |+| pathCheck |+| lintCheck.getOrElse("Doesn't contain JSON Schema".failureNel) | ||
|
||
|
@@ -228,4 +237,29 @@ object LintCommand { | |
case _ => true | ||
} | ||
else true | ||
|
||
def validateSkippedLinters(skipChecks: String): Either[String, List[SanityLinter.Linter]] = { | ||
This comment has been minimized.
Sorry, something went wrong. |
||
val skippedLinters = skipChecks.split(",") | ||
|
||
val linterValidationErrors = for { | ||
sl <- skippedLinters | ||
linter = SanityLinter.allLinters.get(sl) match { | ||
This comment has been minimized.
Sorry, something went wrong.
chuwy
Contributor
|
||
case Some(l) => l | ||
case None => Nil | ||
} | ||
if linter == Nil | ||
} yield s"Unknown linter $sl" | ||
|
||
if (linterValidationErrors.nonEmpty) { | ||
Left(linterValidationErrors.mkString("\n")) | ||
} else { | ||
val lintersToUse = skippedLinters.foldLeft(Right(SanityLinter.allLinters.values.toList)) { (linters, cur) => | ||
(linters, SanityLinter.allLinters.get(cur)) match { | ||
case (Right(linters), Some(l)) => Right(linters.diff(List(l))) | ||
This comment has been minimized.
Sorry, something went wrong.
chuwy
Contributor
|
||
case (Right(linters), None) => Right(linters) | ||
} | ||
} | ||
lintersToUse | ||
} | ||
} | ||
} |
The right way ™ is to have:
Instad of
(List[String], List[String])
. Invalid state should be simply impossible.