1717 StrictInt ,
1818 StringConstraints ,
1919 field_validator ,
20+ model_validator ,
2021)
2122from pydantic .config import JsonDict
2223
7475UnitStr : TypeAlias = Annotated [str , StringConstraints (strip_whitespace = True )]
7576
7677
77- class NodeLockStatus (StrAutoEnum ):
78+ class NodeLockReason (StrAutoEnum ):
7879 OPENING = auto ()
7980 OPENED = auto ()
8081 CLOSING = auto ()
@@ -91,16 +92,47 @@ class NodeLockState(BaseModel):
9192 locked_by : Annotated [
9293 GroupID | None ,
9394 Field (description = "Group that owns locked the node, None if not locked" ),
94- ]
95+ ] = None
9596
9697 locked_reason : Annotated [
97- NodeLockStatus | None ,
98+ NodeLockReason | None ,
9899 Field (
99100 description = "Reason why the node is locked, None if not locked" ,
100101 ),
101- ]
102+ ] = None
102103
103- model_config = ConfigDict (extra = "forbid" )
104+ @model_validator (mode = "after" )
105+ def _validate_lock_state (self ) -> "NodeLockState" :
106+ if self .is_locked and (self .locked_by is None or self .locked_reason is None ):
107+ msg = "If the node is locked, both 'locked_by' and 'locked_reason' must be set"
108+ raise ValueError (msg )
109+ if not self .is_locked and (
110+ self .locked_by is not None or self .locked_reason is not None
111+ ):
112+ msg = "If the node is not locked, both 'locked_by' and 'locked_reason' must be None"
113+ raise ValueError (msg )
114+ return self
115+
116+ @staticmethod
117+ def _update_json_schema_extra (schema : JsonDict ) -> None :
118+ schema .update (
119+ {
120+ "examples" : [
121+ {
122+ "is_locked" : False ,
123+ },
124+ {
125+ "is_locked" : True ,
126+ "locked_by" : 666 ,
127+ "locked_reason" : "OPENING" ,
128+ },
129+ ]
130+ }
131+ )
132+
133+ model_config = ConfigDict (
134+ extra = "forbid" , json_schema_extra = _update_json_schema_extra
135+ )
104136
105137
106138class NodeState (BaseModel ):
@@ -136,6 +168,10 @@ class NodeState(BaseModel):
136168 ),
137169 ] = 0
138170
171+ lock_state : Annotated [NodeLockState , Field (description = "the node's lock state" )] = (
172+ NodeLockState (is_locked = False , locked_by = None , locked_reason = None )
173+ )
174+
139175 model_config = ConfigDict (
140176 extra = "forbid" ,
141177 populate_by_name = True ,
0 commit comments