-
Notifications
You must be signed in to change notification settings - Fork 81
32075 Legal-API: Validation on director names for CoD #4076
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
loneil
wants to merge
1
commit into
bcgov:main
Choose a base branch
from
loneil:32075changeDirectorNameValidation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+139
−5
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,17 +15,15 @@ | |
| from http import HTTPStatus | ||
|
|
||
| import pycountry | ||
| from flask_babel import _ as babel # noqa: N813, I004, I001; importing camelcase '_' as a name | ||
| from flask_babel import _ as babel # importing camelcase '_' as a name | ||
|
|
||
| from legal_api.errors import Error | ||
| from legal_api.models import Address, Business, Filing | ||
| from legal_api.services.filings.validations.common_validations import validate_parties_addresses | ||
| from legal_api.services.filings.validations.common_validations import PARTY_NAME_MAX_LENGTH, validate_parties_addresses | ||
| from legal_api.services.utils import get_str | ||
| from legal_api.utils.datetime import date, datetime | ||
| from legal_api.utils.legislation_datetime import LegislationDatetime | ||
|
|
||
| # noqa: I003; needed as the linter gets confused from the babel override above. | ||
|
|
||
|
|
||
| def validate(business: Business, cod: dict) -> Error: | ||
| """Validate the Change of Directors filing.""" | ||
|
|
@@ -41,6 +39,10 @@ def validate(business: Business, cod: dict) -> Error: | |
| if msg_appointment_date: | ||
| msg += msg_appointment_date | ||
|
|
||
| msg_directors_names = validate_directors_name(cod) | ||
| if msg_directors_names: | ||
| msg += msg_directors_names | ||
|
|
||
| msg_directors_addresses = validate_directors_addresses(business, cod) | ||
| if msg_directors_addresses: | ||
| msg += msg_directors_addresses | ||
|
|
@@ -267,3 +269,62 @@ def validate_effective_date(business: Business, cod: dict) -> list: | |
| msg.append({"error": babel("Effective date cannot be before another Change of Director filing.")}) | ||
|
|
||
| return msg | ||
|
|
||
| def validate_directors_name(cod: dict) -> list: | ||
| """Return error messages if a director's name fields are invalid. | ||
|
|
||
| Rules: | ||
| - firstName and lastName are required (non-empty) for all directors. | ||
| - prevFirstName and prevLastName are required when "nameChanged" is in actions. | ||
| - No leading or trailing whitespace. | ||
| - All name fields have a maximum length of 30 characters. | ||
| """ | ||
| msg = [] | ||
| filing_type = "changeOfDirectors" | ||
| directors = cod["filing"][filing_type]["directors"] | ||
|
|
||
| name_fields = ["firstName", "middleInitial", "lastName", | ||
| "prevFirstName", "prevMiddleInitial", "prevLastName"] | ||
| required_fields = ["firstName", "lastName"] | ||
| name_changed_required_fields = ["prevFirstName", "prevLastName"] | ||
|
|
||
| for idx, director in enumerate(directors): | ||
| officer = director.get("officer", {}) | ||
| actions = director.get("actions", []) | ||
| is_name_changed = "nameChanged" in actions | ||
|
|
||
| for field in name_fields: | ||
| value = officer.get(field) | ||
| path = f"/filing/changeOfDirectors/directors/{idx}/officer/{field}" | ||
|
|
||
| if field in required_fields and (not value or not value.strip()): | ||
| msg.append({ | ||
| "error": babel(f"Director {field} is required."), | ||
| "path": path | ||
| }) | ||
| continue | ||
|
|
||
| # Check prev first/last required when nameChanged | ||
| if field in name_changed_required_fields and is_name_changed and (not value or not value.strip()): | ||
| msg.append({ | ||
| "error": babel(f"Director {field} is required when name has changed."), | ||
| "path": path | ||
| }) | ||
| continue | ||
|
|
||
| if value: | ||
| # No leading or trailing whitespace | ||
| if value != value.strip(): | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This may have impact on PROD if existing data has space ("prevFirstName", "prevMiddleInitial", "prevLastName"), Checked and removed trailing space from PROD. So if dev/test has issues with existing data no need to be concerned about it. |
||
| msg.append({ | ||
| "error": babel(f"Director {field} cannot have leading or trailing whitespace."), | ||
| "path": path | ||
| }) | ||
|
|
||
| # Max length | ||
| if len(value) > PARTY_NAME_MAX_LENGTH: | ||
| msg.append({ | ||
| "error": babel(f"Director {field} cannot be longer than {PARTY_NAME_MAX_LENGTH} characters."), | ||
| "path": path | ||
| }) | ||
|
|
||
| return msg | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
...s/unit/services/filings/validations/change_of_director/test_validation_directors_names.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| import copy | ||
| import pytest | ||
| from http import HTTPStatus | ||
| from legal_api.services.filings.validations.change_of_directors import validate_directors_name | ||
|
|
||
| # Minimal valid director structure for reuse | ||
| def make_director(**kwargs): | ||
| base = { | ||
| "actions": [], | ||
| "officer": { | ||
| "firstName": "John", | ||
| "middleInitial": "Q", | ||
| "lastName": "Public", | ||
| "prevFirstName": "", | ||
| "prevMiddleInitial": "", | ||
| "prevLastName": "" | ||
| } | ||
| } | ||
| for k, v in kwargs.items(): | ||
| if k == "officer": | ||
| base["officer"].update(v) | ||
| else: | ||
| base[k] = v | ||
| return base | ||
|
|
||
| @pytest.mark.parametrize( | ||
| "test_name,directors,expected_msgs", | ||
| [ | ||
| ("valid_minimal", [make_director()], []), | ||
| ("missing_firstName", [make_director(officer={"firstName": ""})], [ | ||
| {"error": "Director firstName is required.", "path": "/filing/changeOfDirectors/directors/0/officer/firstName"} | ||
| ]), | ||
| ("missing_lastName", [make_director(officer={"lastName": ""})], [ | ||
| {"error": "Director lastName is required.", "path": "/filing/changeOfDirectors/directors/0/officer/lastName"} | ||
| ]), | ||
| ("nameChanged_missing_prevFirstName", [make_director(actions=["nameChanged"], officer={"prevFirstName": "", "prevLastName": "Smith"})], [ | ||
| {"error": "Director prevFirstName is required when name has changed.", "path": "/filing/changeOfDirectors/directors/0/officer/prevFirstName"} | ||
| ]), | ||
| ("nameChanged_missing_prevLastName", [make_director(actions=["nameChanged"], officer={"prevFirstName": "Jane", "prevLastName": ""})], [ | ||
| {"error": "Director prevLastName is required when name has changed.", "path": "/filing/changeOfDirectors/directors/0/officer/prevLastName"} | ||
| ]), | ||
| ("leading_whitespace", [make_director(officer={"firstName": " John"})], [ | ||
| {"error": "Director firstName cannot have leading or trailing whitespace.", "path": "/filing/changeOfDirectors/directors/0/officer/firstName"} | ||
| ]), | ||
| ("trailing_whitespace", [make_director(officer={"lastName": "Public "})], [ | ||
| {"error": "Director lastName cannot have leading or trailing whitespace.", "path": "/filing/changeOfDirectors/directors/0/officer/lastName"} | ||
| ]), | ||
| ("over_max_length", [make_director(officer={"firstName": "A"*31})], [ | ||
| {"error": "Director firstName cannot be longer than 30 characters.", "path": "/filing/changeOfDirectors/directors/0/officer/firstName"} | ||
| ]), | ||
| ("multiple_directors", [ | ||
| make_director(), | ||
| make_director(officer={"firstName": "", "lastName": ""}) | ||
| ], [ | ||
| {"error": "Director firstName is required.", "path": "/filing/changeOfDirectors/directors/1/officer/firstName"}, | ||
| {"error": "Director lastName is required.", "path": "/filing/changeOfDirectors/directors/1/officer/lastName"} | ||
| ]), | ||
| ] | ||
| ) | ||
| def test_validate_directors_name(test_name, directors, expected_msgs): | ||
| cod = { | ||
| "filing": { | ||
| "changeOfDirectors": { | ||
| "directors": directors | ||
| } | ||
| } | ||
| } | ||
| msgs = validate_directors_name(cod) | ||
| msgs_simple = [ | ||
| {"error": m["error"].replace("Director ", "Director "), "path": m["path"]} for m in msgs | ||
| ] | ||
| assert msgs_simple == expected_msgs |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is a validate_party_name function in common validations, but this one has differences than a party name (though posted as basically the same format from the UI) in that there's the
prev*fields, the role isn't variable, and there's the actions to take into account.Could maybe generalize a "validate person name" that is shared but there's also the 20 character limit on the name fields, so probably consider any refactoring in context with https://app.zenhub.com/workspaces/entities-team-65af15f59e89f5043c2911f7/issues/gh/bcgov/entity/31979 if that gets addressed at some point.