Fix YAML validation for ElasticsearchDocumentStore.custom_query (#2789)

* Add exception for  in the validation code

* Update Documentation & Code Style

* Add tests

* Update Documentation & Code Style

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
This commit is contained in:
Sara Zan 2022-07-12 13:49:06 +02:00 committed by GitHub
parent 091711b8c4
commit 4d2a06989d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 49 additions and 2 deletions

View File

@ -97,6 +97,9 @@ def read_pipeline_config_from_yaml(path: Path) -> Dict[str, Any]:
return yaml.safe_load(stream)
JSON_FIELDS = ["custom_query"] # ElasticsearchDocumentStore.custom_query
def validate_config_strings(pipeline_config: Any):
"""
Ensures that strings used in the pipelines configuration
@ -105,8 +108,24 @@ def validate_config_strings(pipeline_config: Any):
try:
if isinstance(pipeline_config, dict):
for key, value in pipeline_config.items():
validate_config_strings(key)
validate_config_strings(value)
# FIXME find a better solution
# Some nodes take parameters that expect JSON input,
# like `ElasticsearchDocumentStore.custom_query`
# These parameters fail validation using the standard input regex,
# so they're validated separately.
#
# Note that these fields are checked by name: if two nodes have a field
# with the same name, one of which is JSON and the other not,
# this hack will break.
if key in JSON_FIELDS:
try:
json.loads(value)
except json.decoder.JSONDecodeError as e:
raise PipelineConfigError(f"'{pipeline_config}' does not contain valid JSON.")
else:
validate_config_strings(key)
validate_config_strings(value)
elif isinstance(pipeline_config, list):
for value in pipeline_config:

View File

@ -681,6 +681,34 @@ def test_validate_user_input_valid(input):
validate_config_strings(input)
def test_validate_pipeline_config_component_with_json_input_valid():
validate_config_strings(
{"components": [{"name": "test", "type": "test", "params": {"custom_query": '{"json-key": "json-value"}'}}]}
)
def test_validate_pipeline_config_component_with_json_input_invalid_key():
with pytest.raises(PipelineConfigError, match="is not a valid variable name or value"):
validate_config_strings(
{
"components": [
{"name": "test", "type": "test", "params": {"another_param": '{"json-key": "json-value"}'}}
]
}
)
def test_validate_pipeline_config_component_with_json_input_invalid_value():
with pytest.raises(PipelineConfigError, match="does not contain valid JSON"):
validate_config_strings(
{
"components": [
{"name": "test", "type": "test", "params": {"custom_query": "this is surely not JSON! :)"}}
]
}
)
def test_validate_pipeline_config_invalid_component_name():
with pytest.raises(PipelineConfigError, match="is not a valid variable name or value"):
validate_config_strings({"components": [{"name": "\btest"}]})