diff --git a/.github/workflows/unit-tests.yml b/.github/workflows/unit-tests.yml index 4d5104e..12b6cb0 100644 --- a/.github/workflows/unit-tests.yml +++ b/.github/workflows/unit-tests.yml @@ -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 diff --git a/docassemble/ALToolbox/misc.py b/docassemble/ALToolbox/misc.py index 410662b..0ba4a2f 100644 --- a/docassemble/ALToolbox/misc.py +++ b/docassemble/ALToolbox/misc.py @@ -14,6 +14,7 @@ user_has_privilege, value, word, + validation_error, ) import re @@ -36,6 +37,7 @@ "thousands", "true_values_with_other", "yes_no_unknown", + "include_a_year", ] @@ -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