diff --git a/CHANGELOG.md b/CHANGELOG.md index 86da077b..680e2732 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,7 +8,9 @@ - Only validate a file with a schema if the file path is provided ([#51](https://github.com/nextflow-io/nf-validation/pull/51)) - Handle errors when sample sheet not provided or doesn't have a schema ([#56](https://github.com/nextflow-io/nf-validation/pull/56)) - Silently ignore samplesheet fields that are not defined in samplesheet schema ([#59](https://github.com/nextflow-io/nf-validation/pull/59)) -- Correctly handle double-quoted fields containing commas in csv files by `.fromSamplesheet()` [#63](https://github.com/nextflow-io/nf-validation/pull/63)) +- Correctly handle double-quoted fields containing commas in csv files by `.fromSamplesheet()` ([#63](https://github.com/nextflow-io/nf-validation/pull/63)) +- Print param name when path does not exist ([#65](https://github.com/nextflow-io/nf-validation/pull/65)) +- Fix file or directory does not exist error not printed when it was the only error in a samplesheet ([#65](https://github.com/nextflow-io/nf-validation/pull/65)) - Do not return parameter in summary if it has no default in the schema and is set to 'false' ([#66](https://github.com/nextflow-io/nf-validation/pull/66)) ## Version 0.2.1 diff --git a/plugins/nf-validation/src/main/nextflow/validation/SchemaValidator.groovy b/plugins/nf-validation/src/main/nextflow/validation/SchemaValidator.groovy index bb0b73df..957d59ba 100644 --- a/plugins/nf-validation/src/main/nextflow/validation/SchemaValidator.groovy +++ b/plugins/nf-validation/src/main/nextflow/validation/SchemaValidator.groovy @@ -279,7 +279,7 @@ class SchemaValidator extends PluginExtensionPoint { def List pathsToCheck = (List) collectExists(schemaParams) pathsToCheck.each { if (params[it] != null) { - pathExists(params[it].toString()) + pathExists(params[it].toString(), it.toString()) } } @@ -510,12 +510,11 @@ class SchemaValidator extends PluginExtensionPoint { //=====================================================================// // Check if files or directories exist def List pathsToCheck = (List) collectExists(schemaParams) - pathsToCheck.each { fieldName -> - def String filedName = fieldName + pathsToCheck.each { String fieldName -> for (int i=0; i < arrayJSON.size(); i++) { def JSONObject entry = arrayJSON.getJSONObject(i) - if ( entry.has(filedName) ) { - pathExists(entry[filedName].toString()) + if ( entry.has(fieldName) ) { + pathExists(entry[fieldName].toString(), " Entry ${(i+1).toString()} - ${fieldName.toString()}") } } } @@ -528,6 +527,13 @@ class SchemaValidator extends PluginExtensionPoint { .primitiveValidationStrategy(PrimitiveValidationStrategy.LENIENT) .build(); validator.performValidation(schema, arrayJSON); + if (this.hasErrors()) { + // Needed for custom errors such as pathExists() errors + def colors = logColours(monochrome_logs) + def msg = "${colors.red}The following errors have been detected:\n\n" + this.getErrors().join('\n').trim() + "\n${colors.reset}\n" + log.error("ERROR: Validation of '$paramName' file failed!") + throw new SchemaValidationException(msg, this.getErrors()) + } } catch (ValidationException e) { def colors = logColours(monochrome_logs) JSONObject exceptionJSON = (JSONObject) e.toJSON() @@ -546,10 +552,10 @@ class SchemaValidator extends PluginExtensionPoint { // // Function to check if a file or directory exists // - List pathExists(String path) { + List pathExists(String path, String paramName) { def Path file = Nextflow.file(path) as Path if (!file.exists()) { - errors << "* The file or directory '${path}' does not exist.".toString() + errors << "* --${paramName}: the file or directory '${path}' does not exist.".toString() } } diff --git a/plugins/nf-validation/src/test/nextflow/validation/SamplesheetConverterTest.groovy b/plugins/nf-validation/src/test/nextflow/validation/SamplesheetConverterTest.groovy index 379e36f0..c62d8c7e 100644 --- a/plugins/nf-validation/src/test/nextflow/validation/SamplesheetConverterTest.groovy +++ b/plugins/nf-validation/src/test/nextflow/validation/SamplesheetConverterTest.groovy @@ -352,8 +352,8 @@ class SamplesheetConverterTest extends Dsl2Spec{ def error = thrown(SchemaValidationException) def errorMessages = error.message.readLines() errorMessages[0] == "\033[0;31mThe following errors have been detected:" - errorMessages[2] == "* The file or directory 'non_existing_path' does not exist." - errorMessages[3] == "* The file or directory 'non_existing_file.tsv' does not exist." + errorMessages[2] == "* -- Entry 1 - field_9: the file or directory 'non_existing_path' does not exist." + errorMessages[3] == "* -- Entry 1 - field_7: the file or directory 'non_existing_file.tsv' does not exist." errorMessages[4] == '* -- Entry 1 - field_7: string [non_existing_file.tsv] does not match pattern ^.*\\.txt$ (non_existing_file.tsv)' errorMessages[5] == "* -- Entry 1 - field_8: 'src/testResources/test.txt' is not a directory, but a file (src/testResources/test.txt)" errorMessages[6] == "* -- Entry 1 - field_5: expected type: Number, found: String (string)"