forked from pre-commit/pre-commit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidate_config.py
More file actions
88 lines (69 loc) · 2.43 KB
/
validate_config.py
File metadata and controls
88 lines (69 loc) · 2.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
from __future__ import unicode_literals
from pre_commit.clientlib.validate_base import get_run_function
from pre_commit.clientlib.validate_base import get_validator
from pre_commit.clientlib.validate_base import is_regex_valid
from pre_commit.errors import FatalError
_LOCAL_HOOKS_MAGIC_REPO_STRING = 'local'
def is_local_hooks(repo_entry):
return repo_entry['repo'] == _LOCAL_HOOKS_MAGIC_REPO_STRING
class InvalidConfigError(FatalError):
pass
CONFIG_JSON_SCHEMA = {
'type': 'array',
'minItems': 1,
'items': {
'type': 'object',
'properties': {
'repo': {'type': 'string'},
'sha': {'type': 'string'},
'hooks': {
'type': 'array',
'minItems': 1,
'items': {
'type': 'object',
'properties': {
'id': {'type': 'string'},
'files': {'type': 'string'},
'exclude': {'type': 'string'},
'language_version': {'type': 'string'},
'args': {
'type': 'array',
'items': {'type': 'string'},
},
},
'required': ['id'],
}
}
},
'required': ['repo', 'hooks'],
}
}
def try_regex(repo, hook, value, field_name):
if not is_regex_valid(value):
raise InvalidConfigError(
'Invalid {0} regex at {1}, {2}: {3}'.format(
field_name, repo, hook, value,
)
)
def validate_config_extra(config):
for repo in config:
if is_local_hooks(repo):
if 'sha' in repo:
raise InvalidConfigError(
'"sha" property provided for local hooks'
)
elif 'sha' not in repo:
raise InvalidConfigError(
'Missing "sha" field for repository {0}'.format(repo['repo'])
)
for hook in repo['hooks']:
try_regex(repo, hook['id'], hook.get('files', ''), 'files')
try_regex(repo, hook['id'], hook.get('exclude', ''), 'exclude')
load_config = get_validator(
CONFIG_JSON_SCHEMA,
InvalidConfigError,
additional_validation_strategy=validate_config_extra,
)
run = get_run_function('Config filenames.', load_config, InvalidConfigError)
if __name__ == '__main__':
exit(run())