Skip to content

Commit

Permalink
Merge pull request #715 from hubmapconsortium/Derek-Furst/validate-en…
Browse files Browse the repository at this point in the history
…tityid-not-in-ancestor

Derek furst/validate entityid not in ancestor
  • Loading branch information
yuanzhou authored Aug 6, 2024
2 parents 715c7f6 + 66dc317 commit 4e6d7b5
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/schema/provenance_schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,7 @@ ENTITIES:
before_property_update_validators:
- validate_no_duplicates_in_list
- validate_not_invalid_creation_action
- validate_id_not_in_direct_ancestor
transient: true
exposed: false
indexed: false
Expand Down Expand Up @@ -938,6 +939,8 @@ ENTITIES:
# Note: link_sample_to_direct_ancestor() will always delete all the old linkages first
after_create_trigger: link_sample_to_direct_ancestor
after_update_trigger: link_sample_to_direct_ancestor
before_property_update_validators:
- validate_id_not_in_direct_ancestor
direct_ancestor:
type: json_string # dict
generated: true
Expand Down
27 changes: 27 additions & 0 deletions src/schema/schema_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,33 @@ def validate_publication_date(property_key, normalized_entity_type, request, exi
date_obj = datetime.fromisoformat(new_data_dict[property_key])
except ValueError:
raise ValueError(f"Invalid {property_key} format, must be YYYY-MM-DD")


"""
Validate that the id for the given entity is not included in the direct ancestor uuid's to prevent loops.
Parameters
----------
property_key : str
The target property key
normalized_type : str
Submission
request: Flask request object
The instance of Flask request passed in from application request
existing_data_dict : dict
A dictionary that contains all existing entity properties
new_data_dict : dict
The json data in request body, already after the regular validations
"""
def validate_id_not_in_direct_ancestor(property_key, normalized_entity_type, request, existing_data_dict, new_data_dict):
if 'uuid' not in existing_data_dict:
raise KeyError("Missing 'uuid' key in 'existing_data_dict' during calling 'validate_id_not_in_direct_ancestor()' validator method.")
entity_uuid = existing_data_dict.get("uuid")
ancestors = new_data_dict.get(property_key)
if entity_uuid in ancestors:
raise ValueError(f"Entity uuid may not be included in {property_key}.")



"""
Validate the provided value of the activity creation action. Only very specific
Expand Down

0 comments on commit 4e6d7b5

Please sign in to comment.