Skip to content

Commit

Permalink
Merge pull request #63 from uclahs-cds/yashpatel-generalize-specific-…
Browse files Browse the repository at this point in the history
…validation

Add more options for specific validation function
  • Loading branch information
yashpatel6 authored Apr 29, 2024
2 parents f3448ac + 46e7b67 commit af6a606
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
### Changed
- Allow `set_resources_allocation` to use maximum allocation values defined in params
- Add `def`s to multiple variables that should not be globally defined
- Allow specific schema validation function to accept pre-loaded schema as input

---

Expand Down
2 changes: 1 addition & 1 deletion config/schema/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ input:
- Positional args:
|position|name|type|required|default|description|
|:--:|:--:|:--:|:--:|:--:|:--:|
|1|`file_path`|String|Yes|none|Path to the `schema.yaml` file|
|1|`file_path`|String or Map|Yes|none|Path to the `schema.yaml` file or loaded schema as a Map|
|2|`params_to_validate`|Map|Yes|none|Namespace of parameters to validate|
|3|`keys_to_exclude`|List|No|`[]`|List of parameters to skip validation|

Expand Down
16 changes: 13 additions & 3 deletions config/schema/schema.config
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import org.yaml.snakeyaml.Yaml
includeConfig "./custom_schema_types.config"
/**
* This schema namespace is used to valide the params defined in config file(s) using a schema
* YAML file. This config script should be included and called through `schema.validate()` at the
Expand Down Expand Up @@ -252,12 +253,21 @@ schema {

/**
* Fine-grained validation entrypoint; to be used for validating specific namespaces with certain parameters excluded
* file_path: path to schema YAML to be used for validation
* schema_to_validate: path to schema YAML to be used for validation
* params_to_validate: Map of parameters to validate against schema
* keys_to_exclude: params to skip during validation
* @throws IllegalArgumentException when invalid format of schema is provided
*/
validate_specific = { String file_path, Map params_to_validate, List keys_to_exclude=[] ->
def params_schema = schema.load_schema(file_path)
validate_specific = { Object schema_to_validate, Map params_to_validate, List keys_to_exclude=[] ->
def params_schema;
if (custom_schema_types.is_string(schema_to_validate)) {
params_schema = schema.load_schema(schema_to_validate)
} else if (schema_to_validate in Map) {
params_schema = schema_to_validate
} else {
throw new IllegalArgumentException("The given schema must be a path to the schema YAML or a Map, received `${schema_to_validate.getClass()}` instead.")
}

params_schema.removeAll{ key, val -> keys_to_exclude.contains(key) }
params_schema.each { key, val ->
schema.validate_parameter(params_to_validate, key, val)
Expand Down

0 comments on commit af6a606

Please sign in to comment.