3
3
from typing import Any , Optional , Union
4
4
from uuid import uuid4
5
5
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
7
8
from pydantic_extra_types .color import Color , ColorType
8
9
9
10
from .options import CaptionAlignment
10
11
11
12
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
+ ):
13
34
"""
14
35
A relationship in a graph to visualize.
15
36
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
+
16
41
For more info on each field, see the NVL library docs: https://neo4j.com/docs/nvl/current/base-library/#_relationships
17
42
"""
18
43
@@ -23,25 +48,19 @@ class Relationship(BaseModel, extra="allow"):
23
48
#: Node ID where the relationship points from
24
49
source : Union [str , int ] = Field (
25
50
serialization_alias = "from" ,
26
- validation_alias = AliasChoices ("source" , "sourceNodeId" , "source_node_id" , "from" ),
27
51
description = "Node ID where the relationship points from" ,
28
52
)
29
53
#: Node ID where the relationship points to
30
54
target : Union [str , int ] = Field (
31
55
serialization_alias = "to" ,
32
- validation_alias = AliasChoices ("target" , "targetNodeId" , "target_node_id" , "to" ),
33
56
description = "Node ID where the relationship points to" ,
34
57
)
35
58
#: The caption of the relationship
36
59
caption : Optional [str ] = Field (None , description = "The caption of the relationship" )
37
60
#: 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" )
41
62
#: 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" )
45
64
#: The color of the relationship. Allowed input is for example "#FF0000", "red" or (255, 0, 0)
46
65
color : Optional [ColorType ] = Field (None , description = "The color of the relationship" )
47
66
#: Additional properties of the relationship that do not directly impact the visualization
0 commit comments