Skip to content

Commit 36b2938

Browse files
authored
Merge pull request #110 from nextflow-io/fix/map-params-error-value
Add a config option to limit the max amount of characters for a value shown in error messages
2 parents accb153 + 8fd073c commit 36b2938

File tree

5 files changed

+59
-5
lines changed

5 files changed

+59
-5
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
1. Slow uniqueness check (> 2hrs for 100k samples) made 400x faster by switching from `findAll` to a `subMap` for isolating the required unique fields.
1414
2. `patternProperties` now has greater support, with no warnings about invalid parameters which actually match a pattern
15+
3. Added a new configuration option: `validation.maxErrValSize` which sets the maximum length that a value in an error message can be. The default is set to 150 characters.
1516

1617
## Changes
1718

docs/configuration/configuration.md

+16
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,22 @@ This option does exactly the same as `validation.ignoreParams`, but provides pip
9595
validation.defaultIgnoreParams = ["param1", "param2"] // default: []
9696
```
9797

98+
## maxErrValSize
99+
100+
Configure the maximum characters of a value that may be shown in an error message. It takes a whole number above or equal to `1`. A value will be truncated when it goes over the maximum amount of characters.
101+
102+
Setting this option to `-1` will allow any amount of characters for values.
103+
104+
See the below example where the limit is to 20 characters:
105+
106+
```
107+
* --test (abcdefghij...qrstuvwxyz): Value is [string] but should be [integer]
108+
```
109+
110+
```groovy
111+
validation.maxErrValSize = 100 // default: 150
112+
```
113+
98114
## help
99115

100116
The `validation.help` config scope can be used to configure the creation of the help message.

plugins/nf-schema/src/main/nextflow/validation/config/ValidationConfig.groovy

+13-5
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,26 @@ class ValidationConfig {
2020
final public Boolean failUnrecognisedHeaders
2121
final public String parametersSchema
2222
final public Boolean showHiddenParams
23+
final public Integer maxErrValSize = 150
2324
final public HelpConfig help
2425
final public SummaryConfig summary
2526

2627
final public List<String> ignoreParams
2728

2829
ValidationConfig(Map map, Map params){
2930
def config = map ?: Collections.emptyMap()
30-
lenientMode = config.lenientMode ?: false
31-
monochromeLogs = config.monochromeLogs ?: false
32-
failUnrecognisedParams = config.failUnrecognisedParams ?: false
33-
failUnrecognisedHeaders = config.failUnrecognisedHeaders ?: false
34-
showHiddenParams = config.showHiddenParams ?: false
31+
lenientMode = config.lenientMode ?: false
32+
monochromeLogs = config.monochromeLogs ?: false
33+
failUnrecognisedParams = config.failUnrecognisedParams ?: false
34+
failUnrecognisedHeaders = config.failUnrecognisedHeaders ?: false
35+
showHiddenParams = config.showHiddenParams ?: false
36+
if(config.maxErrValSize != null) {
37+
if(config.maxErrValSize >= 1 || config.maxErrValSize == -1) {
38+
maxErrValSize = config.maxErrValSize
39+
} else {
40+
log.warn("`validation.maxErrValSize` needs to be a value above 0 or equal to -1, defaulting to ${maxErrValSize}")
41+
}
42+
}
3543
if(config.containsKey("showHiddenParams")) {
3644
log.warn("configuration option `validation.showHiddenParams` is deprecated, please use `validation.help.showHidden` or the `--showHidden` parameter instead")
3745
}

plugins/nf-schema/src/main/nextflow/validation/validators/JsonSchemaValidator.groovy

+3
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ public class JsonSchemaValidator {
6565

6666
def String instanceLocation = error.getInstanceLocation()
6767
def String value = getValueFromJsonPointer(instanceLocation, rawJson)
68+
if(config.maxErrValSize >= 1 && value.size() > config.maxErrValSize) {
69+
value = "${value[0..(config.maxErrValSize/2-1)]}...${value[-config.maxErrValSize/2..-1]}" as String
70+
}
6871

6972
// Get the custom errorMessage if there is one and the validation errors are not about the content of the file
7073
def String schemaLocation = error.getSchemaLocation().replaceFirst(/^[^#]+/, "")

plugins/nf-schema/src/test/nextflow/validation/ValidateParametersTest.groovy

+26
Original file line numberDiff line numberDiff line change
@@ -1409,4 +1409,30 @@ class ValidateParametersTest extends Dsl2Spec{
14091409
!stdout
14101410
}
14111411

1412+
def 'should truncate long values in errors' () {
1413+
given:
1414+
def schema = Path.of('src/testResources/nextflow_schema.json').toAbsolutePath().toString()
1415+
def SCRIPT = """
1416+
params.input = 'src/testResources/wrong_samplesheet_with_a_super_long_name.and_a_weird_extension'
1417+
params.outdir = 'src/testResources/testDir'
1418+
include { validateParameters } from 'plugin/nf-schema'
1419+
1420+
validateParameters(parameters_schema: '$schema')
1421+
"""
1422+
1423+
when:
1424+
def config = ["validation": [
1425+
"maxErrValSize": 20
1426+
]]
1427+
def result = new MockScriptRunner(config).setScript(SCRIPT).execute()
1428+
def stdout = capture
1429+
.toString()
1430+
.readLines()
1431+
.findResults {it.contains('WARN nextflow.validation.SchemaValidator') || it.startsWith('* --') ? it : null }
1432+
1433+
then:
1434+
def error = thrown(SchemaValidationException)
1435+
error.message.contains("* --input (src/testRe..._extension): \"src/testResources/wrong_samplesheet_with_a_super_long_name.and_a_weird_extension\" does not match regular expression [^\\S+\\.(csv|tsv|yaml|json)\$]")
1436+
!stdout
1437+
}
14121438
}

0 commit comments

Comments
 (0)