mirror of
https://github.com/deepset-ai/haystack.git
synced 2025-11-02 18:59:28 +00:00
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:
parent
091711b8c4
commit
4d2a06989d
@ -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:
|
||||
|
||||
@ -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"}]})
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user