-
Notifications
You must be signed in to change notification settings - Fork 12
Added function for parameter validation #38
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 6 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
77aa797
initial work on validate params
KevinMenden ec727a0
checking for unexpected and nextflow params
KevinMenden aca0717
working memory und duration checks
KevinMenden 715ea8b
added enum check
KevinMenden a9ddb2d
switched to pattern in schema option
KevinMenden ba015b7
exit upong wrong parameters
KevinMenden 9dd200d
validation using json schema
KevinMenden 69d2a21
Update error message
KevinMenden a3aa6e9
added option to skip; returning unexpected params to wf
KevinMenden 2962fc8
added validate_schema param to schema
KevinMenden 5ca326d
added warning about unexpected parameters on error
KevinMenden 3470f74
groovy-style formatting
KevinMenden ba9e5ae
added jars; https to http to resolve error
KevinMenden 6fa7e4e
renamed dependencies
KevinMenden 1ed68b2
remove grab imports
KevinMenden 9adc2f5
switched to 'validate_params'
KevinMenden 1238c47
updated schema
KevinMenden cf9a7d5
updated schema validation
KevinMenden aee1141
merged jar files
KevinMenden File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| import groovy.json.JsonSlurper | ||
| import groovy.util.logging.Log | ||
|
|
||
| class Validation { | ||
|
|
||
| /* | ||
| * Function to loop over all parameters defined in schema and check | ||
| * whether the given paremeters adhere to the specificiations | ||
| */ | ||
| private static void validateParameters(params, json_schema, log, workflow){ | ||
|
|
||
| def json = new File(json_schema).text | ||
| def Map json_params = (Map) new JsonSlurper().parseText(json).get('definitions') | ||
| def specified_param_keys = params.keySet() | ||
| def nf_params = ['profile', 'config', 'c', 'C', 'syslog', 'd', 'dockerize', | ||
| 'bg', 'h', 'log', 'quiet', 'q', 'v', 'version'] | ||
| def valid_params = [] | ||
| def expected_params = [] | ||
| def blacklist = ['hostnames'] // ignored parameters | ||
|
KevinMenden marked this conversation as resolved.
Outdated
|
||
|
|
||
|
|
||
| // Loop over all parameters in schema and compare to given parameters | ||
| for (group in json_params){ | ||
| for (p in group.value['properties']){ | ||
| if (!blacklist.contains(p.key)){ | ||
| valid_params.push(validateParamPair(params[p.key], p, log)) | ||
| expected_params.push(p.key) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Exit if any invalid params where found | ||
| if (valid_params.contains(false)){ | ||
| System.exit(0) | ||
|
KevinMenden marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| // Check for nextflow core params and unexpected params | ||
| for (specified_param in specified_param_keys){ | ||
| // nextflow params | ||
| if (nf_params.contains(specified_param)){ | ||
| log.error "ERROR: You have overwritten the core Nextflow parameter -${specified_param} with --${specified_param}!" | ||
|
KevinMenden marked this conversation as resolved.
Outdated
|
||
| System.exit(0) | ||
|
KevinMenden marked this conversation as resolved.
Outdated
|
||
| } | ||
| // unexpected params | ||
| if (!expected_params.contains(specified_param)){ | ||
| log.warn "Unexpected parameter specified: ${specified_param}" | ||
|
KevinMenden marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| } | ||
|
|
||
| } | ||
|
|
||
|
|
||
|
|
||
| /* | ||
| * Compare a pair of params (schema, command line) and check whether | ||
| * they are valid | ||
| */ | ||
| private static boolean validateParamPair(given_param, json_param, log){ | ||
|
KevinMenden marked this conversation as resolved.
Outdated
|
||
| def param_type = json_param.value['type'] | ||
| def valid_param = true | ||
| def required = json_param.value['required'] | ||
|
KevinMenden marked this conversation as resolved.
Outdated
|
||
| def param_enum = json_param.value['enum'] | ||
| def param_pattern = json_param.value['pattern'] | ||
|
|
||
| // Check only if required or parameter is given | ||
| if (required || given_param){ | ||
| def given_param_class = given_param.getClass() | ||
|
KevinMenden marked this conversation as resolved.
Outdated
|
||
|
|
||
| switch(param_type) { | ||
|
KevinMenden marked this conversation as resolved.
Outdated
|
||
| case 'string': | ||
| // If pattern given, check that param adheres to it | ||
| if (param_pattern){ | ||
| valid_param = given_param ==~ param_pattern | ||
| } | ||
| // If enum given, check that param is within choices | ||
| else if (param_enum){ | ||
| valid_param = param_enum.contains(given_param) | ||
| } | ||
| // else just check whether valid String | ||
| else { | ||
| valid_param = given_param_class == String | ||
| } | ||
| break | ||
| case 'boolean': | ||
| if (given_param_class == Boolean){ | ||
| valid_param = true | ||
| } | ||
| else if (given_param){ | ||
| valid_param = true | ||
| } | ||
| break | ||
| case 'integer': | ||
| valid_param = given_param_class == Integer | ||
| break | ||
| case 'number': | ||
| valid_param = given_param_class == BigDecimal | ||
| break | ||
| } | ||
|
|
||
| if (!valid_param){ | ||
| log.error "ERROR: Parameter ${json_param.key} is wrong type! Expected ${param_type}, found ${given_param_class}, ${given_param}" | ||
| if (param_enum){ | ||
| log.error "Must be one of: ${param_enum}" | ||
| } | ||
| if (param_pattern){ | ||
| log.error "Parameter must adhere to the following pattern: ${param_pattern}" | ||
| } | ||
| } | ||
|
|
||
| } | ||
| return valid_param | ||
| } | ||
|
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.