Skip to content

Commit

Permalink
Merge pull request #268 from SuffolkLITLab/include_a_year
Browse files Browse the repository at this point in the history
Add 'include_a_year' helper function for validation of free-text date fields
  • Loading branch information
nonprofittechy authored Dec 5, 2024
2 parents dca9069 + 3802f62 commit 0124574
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .github/workflows/unit-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ jobs:
sudo locale-gen en_US.UTF-8
sudo locale-gen ar_AE.UTF-8
sudo update-locale LANG=en_US.UTF-8
- uses: SuffolkLITLab/ALActions/pythontests@main
- uses: SuffolkLITLab/ALActions/pythontests
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 0124574

Please sign in to comment.