Skip to content
This repository has been archived by the owner on Aug 20, 2024. It is now read-only.

Commit

Permalink
feat: add error if 'arrays' used in samplesheet schema but a csv/tsv …
Browse files Browse the repository at this point in the history
…samplesheet is passed. Add tests for this behaviour
  • Loading branch information
awgymer committed Jan 4, 2024
1 parent 69f6c8f commit 8bc3d3a
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,11 @@ class SchemaValidator extends PluginExtensionPoint {
def List<Map<String,String>> fileContentCasted
def Boolean s3PathCheck = params.validationS3PathCheck ? params.validationS3PathCheck : false
def Map types = variableTypes(schemaFile.toString(), baseDir)
if (types.find{ it.value == "array" } as Boolean && fileType in ["csv", "tsv"]){
def msg = "Using \"type\": \"array\" in schema with a \".$fileType\" samplesheet is not supported\n"
log.error("ERROR: Validation of pipeline parameters failed!")
throw new SchemaValidationException(msg, [])
}
def Boolean containsHeader = !(types.keySet().size() == 1 && types.keySet()[0] == "")

if(!containsHeader){
Expand Down Expand Up @@ -433,6 +438,11 @@ class SchemaValidator extends PluginExtensionPoint {
def List<Map<String,String>> fileContent
def List<Map<String,String>> fileContentCasted
def Map types = variableTypes(schema_name, baseDir)
if (types.find{ it.value == "array" } as Boolean && fileType in ["csv", "tsv"]){
def msg = "${colors.red}Using {\"type\": \"array\"} in schema with a \".$fileType\" samplesheet is not supported${colors.reset}\n"
log.error("ERROR: Validation of pipeline parameters failed!")
throw new SchemaValidationException(msg, [])
}
def Boolean containsHeader = !(types.keySet().size() == 1 && types.keySet()[0] == "")

if(!containsHeader){
Expand Down Expand Up @@ -556,6 +566,8 @@ class SchemaValidator extends PluginExtensionPoint {
Boolean monochrome_logs, String paramName, Object fileContent, String schemaFilename, String baseDir, Boolean s3PathCheck = false

) {
// declare this once for the method
def colors = logColours(monochrome_logs)

// Load the schema
def String schema_string = Files.readString( Path.of(getSchemaPath(baseDir, schemaFilename)) )
Expand Down Expand Up @@ -612,13 +624,11 @@ class SchemaValidator extends PluginExtensionPoint {
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()
JSONObject objectJSON = new JSONObject();
objectJSON.put("objects",arrayJSON);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -867,4 +867,28 @@ class PluginExtensionMethodsTest extends Dsl2Spec{
error.message == '''The following errors have been detected:\n\n* -- Entry 1: Missing required value: sample\n* -- Entry 2: Missing required value: sample\n\n'''
!stdout
}
}

def 'should fail because of arrays with csv' () {
given:
def schema = Path.of('src/testResources/nextflow_schema_with_samplesheet_converter_arrays.json').toAbsolutePath().toString()
def SCRIPT_TEXT = """
params.monochrome_logs = true
params.input = 'src/testResources/correct.csv'
include { validateParameters } from 'plugin/nf-validation'
validateParameters(parameters_schema: '$schema', monochrome_logs: params.monochrome_logs)
"""

when:
dsl_eval(SCRIPT_TEXT)
def stdout = capture
.toString()
.readLines()
.findResults {it.contains('WARN nextflow.validation.SchemaValidator') || it.startsWith('* --') ? it : null }

then:
def error = thrown(SchemaValidationException)
error.message == '''Using {"type": "array"} in schema with a ".csv" samplesheet is not supported\n'''
!stdout
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,59 @@ class SamplesheetConverterTest extends Dsl2Spec{
!stdout
}

def 'array errors samplesheet format - CSV' () {
given:
def SCRIPT_TEXT = '''
include { fromSamplesheet } from 'plugin/nf-validation'
params.input = 'src/testResources/correct.csv'
workflow {
Channel.fromSamplesheet("input", parameters_schema:"src/testResources/nextflow_schema_with_samplesheet_converter_arrays.json").view()
}
'''

when:
dsl_eval(SCRIPT_TEXT)
def stdout = capture
.toString()
.readLines()
.findResults {it.startsWith('[[') ? it : null }

then:
def error = thrown(SchemaValidationException)
def errorMessages = error.message.readLines()
errorMessages[0] == 'Using "type": "array" in schema with a ".csv" samplesheet is not supported'
!stdout
}

def 'array errors samplesheet format - TSV' () {
given:
def SCRIPT_TEXT = '''
include { fromSamplesheet } from 'plugin/nf-validation'
params.input = 'src/testResources/correct.tsv'
workflow {
Channel.fromSamplesheet("input", parameters_schema:"src/testResources/nextflow_schema_with_samplesheet_converter_arrays.json").view()
}
'''

when:
dsl_eval(SCRIPT_TEXT)
def stdout = capture
.toString()
.readLines()
.findResults {it.startsWith('[[') ? it : null }

then:
def error = thrown(SchemaValidationException)
def errorMessages = error.message.readLines()
errorMessages[0] == 'Using "type": "array" in schema with a ".tsv" samplesheet is not supported'
!stdout
}


def 'no header - CSV' () {
given:
def SCRIPT_TEXT = '''
Expand Down

0 comments on commit 8bc3d3a

Please sign in to comment.