Skip to content

Commit

Permalink
Add option to validate data on Node creation
Browse files Browse the repository at this point in the history
  • Loading branch information
kkaris committed Jun 3, 2024
1 parent 05bfa4b commit 90d6a4b
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/indra_cogex/representation.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
from indra.statements.agent import get_grounding
from indra.statements import stmts_from_json, Statement

from indra_cogex.sources.processor_util import data_validator

NodeJson = Dict[str, Union[Collection[str], Dict[str, Any]]]
RelJson = Dict[str, Union[Mapping[str, Any], Dict]]

Expand All @@ -36,6 +38,7 @@ def __init__(
db_id: str,
labels: Collection[str],
data: Optional[Mapping[str, Any]] = None,
validate_data: bool = False,
):
"""Initialize the node.
Expand All @@ -50,12 +53,23 @@ def __init__(
A collection of labels for the node.
data :
An optional data dictionary associated with the node.
validate_data :
If True, validate the data dictionary. Default: True.
"""
if not db_ns or not db_id:
raise ValueError("Missing namespace or ID.")
self.db_ns = db_ns
self.db_id = db_id
self.labels = labels

if data is not None and validate_data:
for header_key, data_value in data.items():
if ":" in header_key:
data_type = header_key.split(":")[1]
else:
# If no data type is specified, string is assumed by Neo4j
data_type = "string"
data_validator(data_type, data_value)
self.data = data if data else {}

@classmethod
Expand Down

0 comments on commit 90d6a4b

Please sign in to comment.