Skip to content

Commit

Permalink
Add 'include_a_year' helper function for validation of free-text date…
Browse files Browse the repository at this point in the history
… fields
  • Loading branch information
nonprofittechy committed Dec 3, 2024
1 parent dca9069 commit 6b5cd76
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions docassemble/ALToolbox/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
user_has_privilege,
value,
word,
validation_error,
)
import re

Expand All @@ -36,6 +37,7 @@
"thousands",
"true_values_with_other",
"yes_no_unknown",
"include_a_year",
]


Expand Down Expand Up @@ -459,3 +461,21 @@ def true_values_with_other(
true_values.append(value(other_variable_name))

return true_values


def include_a_year(text: str, field: Optional[str] = None) -> bool:
"""
Validates whether the input text contains at least one 4-digit sequence
that occurs within a range of ~ 200 years, indicating a valid "year"
for an event that should be reported on most court forms, like a birthdate
or a moving date.
Returns True if found, otherwise raises a DAValidationError.
"""
# Match a 4-digit sequence
if re.search(r"\b(18|19|20|21)\d{2}\b", text):
return True
else:
validation_error(word("Include a year, like: Fall of 2023"), field=field)

return False

0 comments on commit 6b5cd76

Please sign in to comment.