mirror of
https://github.com/adrienverge/yamllint.git
synced 2025-02-25 18:55:20 -06:00
config: Validate config options with list of enums
Allow rules to declare a list of valid values for an option. For example, a rule like: CONF = {'allowed-values': list} ... allowed any value to be passed in the list (including bad ones). It is now possible to declare: CONF = {'allowed-values': ['value1', 'value2', 'value3']} ... so that the list passed to the options must contain only values in `['value1', 'value2', 'value3']`.
This commit is contained in:
parent
0fef4c14e7
commit
f65553c4f7
@ -174,6 +174,25 @@ class SimpleConfigTestCase(unittest.TestCase):
|
||||
self.assertRaises(config.YamlLintConfigError,
|
||||
config.validate_rule_conf, Rule, {'choice': 'abc'})
|
||||
|
||||
Rule.CONF = {'multiple': ['item1', 'item2', 'item3']}
|
||||
Rule.DEFAULT = {'multiple': ['item1']}
|
||||
config.validate_rule_conf(Rule, {'multiple': []})
|
||||
config.validate_rule_conf(Rule, {'multiple': ['item2']})
|
||||
config.validate_rule_conf(Rule, {'multiple': ['item2', 'item3']})
|
||||
config.validate_rule_conf(Rule, {})
|
||||
self.assertRaises(config.YamlLintConfigError,
|
||||
config.validate_rule_conf, Rule,
|
||||
{'multiple': 'item1'})
|
||||
self.assertRaises(config.YamlLintConfigError,
|
||||
config.validate_rule_conf, Rule,
|
||||
{'multiple': ['']})
|
||||
self.assertRaises(config.YamlLintConfigError,
|
||||
config.validate_rule_conf, Rule,
|
||||
{'multiple': ['item1', 4]})
|
||||
self.assertRaises(config.YamlLintConfigError,
|
||||
config.validate_rule_conf, Rule,
|
||||
{'multiple': ['item4']})
|
||||
|
||||
|
||||
class ExtendedConfigTestCase(unittest.TestCase):
|
||||
def test_extend_on_object(self):
|
||||
|
@ -134,12 +134,26 @@ def validate_rule_conf(rule, conf):
|
||||
raise YamlLintConfigError(
|
||||
'invalid config: unknown option "%s" for rule "%s"' %
|
||||
(optkey, rule.ID))
|
||||
# Example: CONF = {option: (bool, 'mixed')}
|
||||
# → {option: true} → {option: mixed}
|
||||
if isinstance(options[optkey], tuple):
|
||||
if (conf[optkey] not in options[optkey] and
|
||||
type(conf[optkey]) not in options[optkey]):
|
||||
raise YamlLintConfigError(
|
||||
'invalid config: option "%s" of "%s" should be in %s'
|
||||
% (optkey, rule.ID, options[optkey]))
|
||||
# Example: CONF = {option: ['flag1', 'flag2']}
|
||||
# → {option: [flag1]} → {option: [flag1, flag2]}
|
||||
elif isinstance(options[optkey], list):
|
||||
if (type(conf[optkey]) is not list or
|
||||
any(flag not in options[optkey]
|
||||
for flag in conf[optkey])):
|
||||
raise YamlLintConfigError(
|
||||
('invalid config: option "%s" of "%s" should only '
|
||||
'contain values in %s')
|
||||
% (optkey, rule.ID, str(options[optkey])))
|
||||
# Example: CONF = {option: int}
|
||||
# → {option: 42}
|
||||
else:
|
||||
if not isinstance(conf[optkey], options[optkey]):
|
||||
raise YamlLintConfigError(
|
||||
|
Loading…
Reference in New Issue
Block a user