33from typing import Any , Optional , Union
44from uuid import uuid4
55
6- from pydantic import AliasChoices , BaseModel , Field , field_serializer , field_validator
6+ from pydantic import AliasChoices , AliasGenerator , BaseModel , Field , field_serializer , field_validator
7+ from pydantic .alias_generators import to_camel
78from pydantic_extra_types .color import Color , ColorType
89
910from .options import CaptionAlignment
1011
1112
12- class Relationship (BaseModel , extra = "allow" ):
13+ def create_aliases (field_name : str ) -> AliasChoices :
14+ valid_names = [field_name ]
15+
16+ if field_name == "source" :
17+ valid_names .extend (["sourcenodeid" , "source_node_id" , "from" ])
18+ if field_name == "target" :
19+ valid_names .extend (["targetnodeid" , "target_node_id" , "to" ])
20+
21+ choices = [[choice , choice .upper (), to_camel (choice )] for choice in valid_names ]
22+
23+ return AliasChoices (* [alias for aliases in choices for alias in aliases ])
24+
25+
26+ class Relationship (
27+ BaseModel ,
28+ extra = "forbid" ,
29+ alias_generator = AliasGenerator (
30+ validation_alias = create_aliases ,
31+ serialization_alias = lambda field_name : to_camel (field_name ),
32+ ),
33+ ):
1334 """
1435 A relationship in a graph to visualize.
1536
37+ Each field is case-insensitive for input, and camelCase is also accepted.
38+ For example, "CAPTION_ALIGN", "captionAlign" are also valid inputs keys for the `caption_align` field.
39+ Upon construction however, the field names are converted to snake_case.
40+
1641 For more info on each field, see the NVL library docs: https://neo4j.com/docs/nvl/current/base-library/#_relationships
1742 """
1843
@@ -23,25 +48,19 @@ class Relationship(BaseModel, extra="allow"):
2348 #: Node ID where the relationship points from
2449 source : Union [str , int ] = Field (
2550 serialization_alias = "from" ,
26- validation_alias = AliasChoices ("source" , "sourceNodeId" , "source_node_id" , "from" ),
2751 description = "Node ID where the relationship points from" ,
2852 )
2953 #: Node ID where the relationship points to
3054 target : Union [str , int ] = Field (
3155 serialization_alias = "to" ,
32- validation_alias = AliasChoices ("target" , "targetNodeId" , "target_node_id" , "to" ),
3356 description = "Node ID where the relationship points to" ,
3457 )
3558 #: The caption of the relationship
3659 caption : Optional [str ] = Field (None , description = "The caption of the relationship" )
3760 #: The alignment of the caption text
38- caption_align : Optional [CaptionAlignment ] = Field (
39- None , serialization_alias = "captionAlign" , description = "The alignment of the caption text"
40- )
61+ caption_align : Optional [CaptionAlignment ] = Field (None , description = "The alignment of the caption text" )
4162 #: The size of the caption text
42- caption_size : Optional [Union [int , float ]] = Field (
43- None , gt = 0.0 , serialization_alias = "captionSize" , description = "The size of the caption text"
44- )
63+ caption_size : Optional [Union [int , float ]] = Field (None , gt = 0.0 , description = "The size of the caption text" )
4564 #: The color of the relationship. Allowed input is for example "#FF0000", "red" or (255, 0, 0)
4665 color : Optional [ColorType ] = Field (None , description = "The color of the relationship" )
4766 #: Additional properties of the relationship that do not directly impact the visualization
0 commit comments