-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidators.py
More file actions
282 lines (222 loc) · 8.63 KB
/
validators.py
File metadata and controls
282 lines (222 loc) · 8.63 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
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
"""Validation functions for API request parameters."""
from __future__ import annotations
import math
from typing import TYPE_CHECKING, Any
from astroapi.errors import AstrologyError
from astroapi.types.config import (
DAY_RANGE,
HOUR_RANGE,
HOUSE_NUMBER_RANGE,
LATITUDE_RANGE,
LONGITUDE_RANGE,
MINUTE_RANGE,
MONTH_RANGE,
ORB_RANGE,
SECOND_RANGE,
YEAR_RANGE,
)
if TYPE_CHECKING:
from astroapi.types.requests import BirthData, DateTimeLocation, Subject
def validate_year(year: int | None) -> None:
"""Validate year is within acceptable range.
Args:
year: Year value to validate
Raises:
AstrologyError: If year is None, not an integer, or out of range
"""
if year is None:
raise AstrologyError("Year is required")
if not isinstance(year, int):
raise AstrologyError(f"Year must be an integer, got {type(year).__name__}")
if not (YEAR_RANGE[0] <= year <= YEAR_RANGE[1]):
raise AstrologyError(
f"Year must be between {YEAR_RANGE[0]} and {YEAR_RANGE[1]}, got {year}"
)
def validate_month(month: int | None) -> None:
"""Validate month is within acceptable range.
Args:
month: Month value to validate (1-12)
Raises:
AstrologyError: If month is not an integer or out of range
"""
if month is not None:
if not isinstance(month, int):
raise AstrologyError(f"Month must be an integer, got {type(month).__name__}")
if not (MONTH_RANGE[0] <= month <= MONTH_RANGE[1]):
raise AstrologyError(
f"Month must be between {MONTH_RANGE[0]} and {MONTH_RANGE[1]}, got {month}"
)
def validate_day(day: int | None) -> None:
"""Validate day is within acceptable range.
Args:
day: Day value to validate (1-31)
Raises:
AstrologyError: If day is not an integer or out of range
"""
if day is not None:
if not isinstance(day, int):
raise AstrologyError(f"Day must be an integer, got {type(day).__name__}")
if not (DAY_RANGE[0] <= day <= DAY_RANGE[1]):
raise AstrologyError(
f"Day must be between {DAY_RANGE[0]} and {DAY_RANGE[1]}, got {day}"
)
def validate_hour(hour: int | None) -> None:
"""Validate hour is within acceptable range.
Args:
hour: Hour value to validate (0-23)
Raises:
AstrologyError: If hour is not an integer or out of range
"""
if hour is not None:
if not isinstance(hour, int):
raise AstrologyError(f"Hour must be an integer, got {type(hour).__name__}")
if not (HOUR_RANGE[0] <= hour <= HOUR_RANGE[1]):
raise AstrologyError(
f"Hour must be between {HOUR_RANGE[0]} and {HOUR_RANGE[1]}, got {hour}"
)
def validate_minute(minute: int | None) -> None:
"""Validate minute is within acceptable range.
Args:
minute: Minute value to validate (0-59)
Raises:
AstrologyError: If minute is not an integer or out of range
"""
if minute is not None:
if not isinstance(minute, int):
raise AstrologyError(f"Minute must be an integer, got {type(minute).__name__}")
if not (MINUTE_RANGE[0] <= minute <= MINUTE_RANGE[1]):
raise AstrologyError(
f"Minute must be between {MINUTE_RANGE[0]} and {MINUTE_RANGE[1]}, got {minute}"
)
def validate_second(second: int | None) -> None:
"""Validate second is within acceptable range.
Args:
second: Second value to validate (0-59)
Raises:
AstrologyError: If second is not an integer or out of range
"""
if second is not None:
if not isinstance(second, int):
raise AstrologyError(f"Second must be an integer, got {type(second).__name__}")
if not (SECOND_RANGE[0] <= second <= SECOND_RANGE[1]):
raise AstrologyError(
f"Second must be between {SECOND_RANGE[0]} and {SECOND_RANGE[1]}, got {second}"
)
def validate_latitude(lat: float | None) -> None:
"""Validate latitude is within acceptable range.
Args:
lat: Latitude value to validate (-90 to 90)
Raises:
AstrologyError: If latitude is not finite or out of range
"""
if lat is not None:
if not isinstance(lat, (int, float)):
raise AstrologyError(f"Latitude must be a number, got {type(lat).__name__}")
if not math.isfinite(lat):
raise AstrologyError(f"Latitude must be finite, got {lat}")
if not (LATITUDE_RANGE[0] <= lat <= LATITUDE_RANGE[1]):
raise AstrologyError(
f"Latitude must be between {LATITUDE_RANGE[0]} and {LATITUDE_RANGE[1]}, got {lat}"
)
def validate_longitude(lon: float | None) -> None:
"""Validate longitude is within acceptable range.
Args:
lon: Longitude value to validate (-180 to 180)
Raises:
AstrologyError: If longitude is not finite or out of range
"""
if lon is not None:
if not isinstance(lon, (int, float)):
raise AstrologyError(f"Longitude must be a number, got {type(lon).__name__}")
if not math.isfinite(lon):
raise AstrologyError(f"Longitude must be finite, got {lon}")
if not (LONGITUDE_RANGE[0] <= lon <= LONGITUDE_RANGE[1]):
raise AstrologyError(
f"Longitude must be between {LONGITUDE_RANGE[0]} and "
f"{LONGITUDE_RANGE[1]}, got {lon}"
)
def validate_orb(orb: float | None) -> None:
"""Validate orb value is within acceptable range.
Args:
orb: Orb value to validate (0.0 to 10.0)
Raises:
AstrologyError: If orb is not finite or out of range
"""
if orb is not None:
if not isinstance(orb, (int, float)):
raise AstrologyError(f"Orb must be a number, got {type(orb).__name__}")
if not math.isfinite(orb):
raise AstrologyError(f"Orb must be finite, got {orb}")
if not (ORB_RANGE[0] <= orb <= ORB_RANGE[1]):
raise AstrologyError(
f"Orb must be between {ORB_RANGE[0]} and {ORB_RANGE[1]}, got {orb}"
)
def validate_house_number(house: int | None) -> None:
"""Validate house number is within acceptable range.
Args:
house: House number to validate (1-12)
Raises:
AstrologyError: If house number is not an integer or out of range
"""
if house is not None:
if not isinstance(house, int):
raise AstrologyError(f"House number must be an integer, got {type(house).__name__}")
if not (HOUSE_NUMBER_RANGE[0] <= house <= HOUSE_NUMBER_RANGE[1]):
raise AstrologyError(
f"House number must be between {HOUSE_NUMBER_RANGE[0]} and "
f"{HOUSE_NUMBER_RANGE[1]}, got {house}"
)
def validate_birth_data(data: BirthData) -> None:
"""Validate BirthData object.
Args:
data: BirthData instance to validate
Raises:
AstrologyError: If any field validation fails
"""
validate_year(data.year)
validate_month(data.month)
validate_day(data.day)
validate_hour(data.hour)
validate_minute(data.minute)
validate_second(data.second)
validate_latitude(data.latitude)
validate_longitude(data.longitude)
def validate_subject(subject: Subject) -> None:
"""Validate Subject object.
Args:
subject: Subject instance to validate
Raises:
AstrologyError: If subject has no birth_data or validation fails
"""
if subject.birth_data is None:
raise AstrologyError("Subject must have birth_data")
validate_birth_data(subject.birth_data)
def validate_datetime_location(dtl: DateTimeLocation) -> None:
"""Validate DateTimeLocation object.
Args:
dtl: DateTimeLocation instance to validate
Raises:
AstrologyError: If any field validation fails
"""
validate_year(dtl.year)
validate_month(dtl.month)
validate_day(dtl.day)
validate_hour(dtl.hour)
validate_minute(dtl.minute)
validate_second(dtl.second)
validate_latitude(dtl.latitude)
validate_longitude(dtl.longitude)
def validate_subjects(subjects: list[Any]) -> None:
"""Validate list of Subject objects.
Args:
subjects: List of Subject instances to validate
Raises:
AstrologyError: If subjects list is empty or any subject validation fails
"""
if not subjects:
raise AstrologyError("At least one subject is required")
for idx, subject in enumerate(subjects):
try:
validate_subject(subject)
except AstrologyError as e:
raise AstrologyError(f"Subject {idx}: {e.message}") from e