-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathstrings_validation_contract.py
156 lines (130 loc) · 4.87 KB
/
strings_validation_contract.py
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
"""Module Contract."""
from typing_extensions import Self
from flunt.constants.messages import (
CONTAINS,
IS_NOT_NONE_OR_WHITESPACE,
NOT_CONTAINS,
)
from flunt.notifications.notifiable import Notifiable
class StringValidationContract(Notifiable):
"""
Class for validating string values and adding notifications based on various comparisons.
Parameters
----------
N/A
Attributes
----------
N/A
Methods
-------
- is_not_none_or_white_space(value, field, message): Checks if a string value is not None or whitespace.
- contains(value, comparer, field, message): Checks if a string value contains another string.
- not_contains(value, comparer, field, message): Checks if a string value does not contain another string.
"""
def is_not_none_or_white_space(
self, value: str, field: str, message: str = IS_NOT_NONE_OR_WHITESPACE
) -> Self:
"""
Check if a string value is not None or whitespace and adds a notification if it is.
Parameters
----------
`value`: str
The string value to be checked.
`field`: str
The field or identifier associated with the check.
`message`: str (optional)
The notification message to be added if the value is None or whitespace.
Returns
-------
`self`
The current instance with potential notifications added.
Notes
-----
- If the `value` is None or consists only of whitespace characters (spaces, tabs, newlines, etc.),
a notification is added to the current instance with the provided `field` and `message`.
- If the `value` is not None and contains at least one non-whitespace character, no notification is added.
Examples
--------
```python
obj = Contract()
.is_not_none_or_white_space("Hello", "ValueCheck", "Value should not be None or whitespace")
obj.is_valid
```
"""
if value is None or value.isspace():
self.add_notification(field, message.format(field))
return self
def contains(
self, value: str, comparer: str, field: str, message: str = CONTAINS
) -> Self:
"""
Check if a string value contains another string and adds a notification if it does.
Parameters
----------
`value`: str
The string value to be checked.
`comparer`: str
The string to search for within the value.
`field`: str
The field or identifier associated with the check.
`message`: str (optional)
The notification message to be added if the value contains the comparer.
Returns
-------
`self`
The current instance with potential notifications added.
Notes
-----
- If the `value` contains the `comparer` string, a notification is added to the current instance
with the provided `field` and `message`.
- If the `value` does not contain the `comparer` string, no notification is added.
Examples
--------
```python
obj = Contract()
.contains("Hello, world!", "world", "ContainsCheck", "Value should contain 'world'")
obj.is_valid
```
"""
if value.find(comparer) == -1:
self.add_notification(field, message.format(field, comparer))
return self
def not_contains(
self,
value: str,
comparer: str,
field: str,
message: str = NOT_CONTAINS,
) -> Self:
"""
Check if a string value does not contain a specified string and adds a notification if it does.
Parameters
----------
`value`: str
The string value to be checked.
`comparer`: str
The string to search for in the value.
`field`: str
The field or identifier associated with the check.
`message`: str (optional)
The notification message to be added if the value contains the comparer.
Returns
-------
`self`
The current instance with potential notifications added.
Notes
-----
- If the `value` does not contain the `comparer` string, no notification is added.
- If the `value` contains the `comparer` string, a notification is added to the current instance
with the provided `field` and `message`.
Examples
--------
```python
obj = Contract()
.not_contains("Hello", "World", "Comparison", "Value should not contain 'World'")
obj.is_valid
```
"""
if value.find(comparer) != -1:
self.add_notification(field, message.format(field, comparer))
return self