This repository was archived by the owner on Sep 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathvalidators.py
More file actions
91 lines (72 loc) · 2.96 KB
/
validators.py
File metadata and controls
91 lines (72 loc) · 2.96 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
89
90
91
from prompt_toolkit.validation import Validator, ValidationError
import re
class TypeValidator(Validator):
def __init__(self, valid_types: list):
self.valid_types = valid_types
super().__init__()
def validate(self, document):
text = document.text
if text not in self.valid_types:
raise ValidationError(message="That is not a valid type.")
class YesNoValidator(Validator):
def validate(self, document):
text = document.text
if text.lower().strip() not in ["", "y", "n", "yes", "no"]:
raise ValidationError(message="Answer must be yes/no.")
class DescriptionValidator(Validator):
def __init__(self, maximum_chars: int):
if maximum_chars < 0:
raise ValueError("Cannot have sub-zero maximum")
self.max_chars_allowed = maximum_chars
super().__init__()
def validate(self, document):
text = document.text
if text.strip() == "":
raise ValidationError(message="You must write a description.")
if len(text) > self.max_chars_allowed:
num_chars_overflowed = len(text) - self.max_chars_allowed
raise ValidationError(
message=f"You are {num_chars_overflowed} characters over the limit."
)
class BodyValidator(Validator):
def __init__(self, session, is_breaking_change: bool):
self.session = session
self.is_breaking_change = is_breaking_change
super().__init__()
def validate(self, document):
text = document.text
if text.strip() == "":
self.session.multiline = False
if self.is_breaking_change:
raise ValidationError(
message="You must write a body for a breaking change."
)
else:
self.session.multiline = True
class FooterValidator(Validator):
def __init__(self, session):
self.session = session
def validate(self, document):
text = document.text
if text.strip() == "":
self.session.multiline = False
else:
self.session.multiline = True
input_lines = text.split("\n")
for line in input_lines:
matches_incomplete = [
m[0]
for m in re.findall(
r"(\[\s*(ch|branch(\s*ch)?)\s*[0-9]+\s*\]?)", line
)
] # rough matches
matches_complete = [
m[0] for m in re.findall(r"(\[(ch|branch ch)[0-9]+\])", line)
] # strict matches
if len(matches_incomplete) != len(matches_complete):
for match in matches_incomplete:
if match not in matches_complete:
raise ValidationError(
message="An invalid Clubhouse reference was found: "
+ str(match)
)