Merge pull request #2732 from freqtrade/config_validation_split

Config validation split
This commit is contained in:
hroff-1902 2020-01-03 23:41:44 +03:00 committed by GitHub
commit 3798f94d4c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 35 additions and 15 deletions

View File

@ -1,4 +1,5 @@
import logging import logging
from copy import deepcopy
from typing import Any, Dict from typing import Any, Dict
from jsonschema import Draft4Validator, validators from jsonschema import Draft4Validator, validators
@ -42,15 +43,25 @@ def validate_config_schema(conf: Dict[str, Any]) -> Dict[str, Any]:
:param conf: Config in JSON format :param conf: Config in JSON format
:return: Returns the config if valid, otherwise throw an exception :return: Returns the config if valid, otherwise throw an exception
""" """
conf_schema = deepcopy(constants.CONF_SCHEMA)
if conf.get('runmode', RunMode.OTHER) in (RunMode.DRY_RUN, RunMode.LIVE):
conf_schema['required'] = constants.SCHEMA_TRADE_REQUIRED
else:
conf_schema['required'] = constants.SCHEMA_MINIMAL_REQUIRED
# Dynamically allow empty stake-currency
# Since the minimal config specifies this too.
# It's not allowed for Dry-run or live modes
conf_schema['properties']['stake_currency']['enum'] += [''] # type: ignore
try: try:
FreqtradeValidator(constants.CONF_SCHEMA).validate(conf) FreqtradeValidator(conf_schema).validate(conf)
return conf return conf
except ValidationError as e: except ValidationError as e:
logger.critical( logger.critical(
f"Invalid configuration. See config.json.example. Reason: {e}" f"Invalid configuration. See config.json.example. Reason: {e}"
) )
raise ValidationError( raise ValidationError(
best_match(Draft4Validator(constants.CONF_SCHEMA).iter_errors(conf)).message best_match(Draft4Validator(conf_schema).iter_errors(conf)).message
) )

View File

@ -269,7 +269,9 @@ CONF_SCHEMA = {
'required': ['process_throttle_secs', 'allowed_risk', 'capital_available_percentage'] 'required': ['process_throttle_secs', 'allowed_risk', 'capital_available_percentage']
} }
}, },
'required': [ }
SCHEMA_TRADE_REQUIRED = [
'exchange', 'exchange',
'max_open_trades', 'max_open_trades',
'stake_currency', 'stake_currency',
@ -280,5 +282,9 @@ CONF_SCHEMA = {
'unfilledtimeout', 'unfilledtimeout',
'stoploss', 'stoploss',
'minimal_roi', 'minimal_roi',
] ]
}
SCHEMA_MINIMAL_REQUIRED = [
'exchange',
'dry_run',
]

View File

@ -12,7 +12,8 @@ from colorama import init as colorama_init
from tabulate import tabulate from tabulate import tabulate
from freqtrade.configuration import (Configuration, TimeRange, from freqtrade.configuration import (Configuration, TimeRange,
remove_credentials) remove_credentials,
validate_config_consistency)
from freqtrade.configuration.directory_operations import (copy_sample_files, from freqtrade.configuration.directory_operations import (copy_sample_files,
create_userdata_dir) create_userdata_dir)
from freqtrade.constants import USERPATH_HYPEROPTS, USERPATH_STRATEGY from freqtrade.constants import USERPATH_HYPEROPTS, USERPATH_STRATEGY
@ -40,6 +41,7 @@ def setup_utils_configuration(args: Dict[str, Any], method: RunMode) -> Dict[str
# Ensure we do not use Exchange credentials # Ensure we do not use Exchange credentials
remove_credentials(config) remove_credentials(config)
validate_config_consistency(config)
return config return config

View File

@ -49,6 +49,7 @@ def test_load_config_missing_attributes(default_conf) -> None:
conf = deepcopy(default_conf) conf = deepcopy(default_conf)
conf.pop('stake_currency') conf.pop('stake_currency')
conf['runmode'] = RunMode.DRY_RUN
with pytest.raises(ValidationError, match=r".*'stake_currency' is a required property.*"): with pytest.raises(ValidationError, match=r".*'stake_currency' is a required property.*"):
validate_config_schema(conf) validate_config_schema(conf)