Mercurial > libervia-backend
annotate libervia/backend/models/types.py @ 4324:e1fcf4dd9012
core (models/types): add types for `domish.Element`.
author | Goffi <goffi@goffi.org> |
---|---|
date | Wed, 20 Nov 2024 11:29:36 +0100 |
parents | ffc43219e0b2 |
children |
rev | line source |
---|---|
4296
ffc43219e0b2
core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1 #!/usr/bin/env python3 |
ffc43219e0b2
core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
2 |
ffc43219e0b2
core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
3 # Libervia models custom types |
ffc43219e0b2
core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
4 # Copyright (C) 2009-2024 Jérôme Poisson (goffi@goffi.org) |
ffc43219e0b2
core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
5 |
ffc43219e0b2
core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
6 # This program is free software: you can redistribute it and/or modify |
ffc43219e0b2
core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
7 # it under the terms of the GNU Affero General Public License as published by |
ffc43219e0b2
core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
8 # the Free Software Foundation, either version 3 of the License, or |
ffc43219e0b2
core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
9 # (at your option) any later version. |
ffc43219e0b2
core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
10 |
ffc43219e0b2
core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
11 # This program is distributed in the hope that it will be useful, |
ffc43219e0b2
core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
ffc43219e0b2
core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
ffc43219e0b2
core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
14 # GNU Affero General Public License for more details. |
ffc43219e0b2
core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
15 |
ffc43219e0b2
core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
16 # You should have received a copy of the GNU Affero General Public License |
ffc43219e0b2
core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
17 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
ffc43219e0b2
core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
18 |
ffc43219e0b2
core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
19 from typing import TYPE_CHECKING, Annotated, Any, TypeAlias, cast |
ffc43219e0b2
core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
20 |
ffc43219e0b2
core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
21 from pydantic import GetCoreSchemaHandler, GetJsonSchemaHandler |
ffc43219e0b2
core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
22 from pydantic.json_schema import JsonSchemaValue |
ffc43219e0b2
core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
23 from pydantic_core import core_schema |
ffc43219e0b2
core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
24 from twisted.words.protocols.jabber.jid import JID |
4324
e1fcf4dd9012
core (models/types): add types for `domish.Element`.
Goffi <goffi@goffi.org>
parents:
4296
diff
changeset
|
25 from twisted.words.xish.domish import Element |
e1fcf4dd9012
core (models/types): add types for `domish.Element`.
Goffi <goffi@goffi.org>
parents:
4296
diff
changeset
|
26 from libervia.backend.tools.xml_tools import parse |
e1fcf4dd9012
core (models/types): add types for `domish.Element`.
Goffi <goffi@goffi.org>
parents:
4296
diff
changeset
|
27 |
e1fcf4dd9012
core (models/types): add types for `domish.Element`.
Goffi <goffi@goffi.org>
parents:
4296
diff
changeset
|
28 |
e1fcf4dd9012
core (models/types): add types for `domish.Element`.
Goffi <goffi@goffi.org>
parents:
4296
diff
changeset
|
29 class _DomishElementType: |
e1fcf4dd9012
core (models/types): add types for `domish.Element`.
Goffi <goffi@goffi.org>
parents:
4296
diff
changeset
|
30 """Use Twisted's domish.Element in Python type, and serialize it to str |
e1fcf4dd9012
core (models/types): add types for `domish.Element`.
Goffi <goffi@goffi.org>
parents:
4296
diff
changeset
|
31 |
e1fcf4dd9012
core (models/types): add types for `domish.Element`.
Goffi <goffi@goffi.org>
parents:
4296
diff
changeset
|
32 In Python, both Element and str are accepted, str are converted to Element. |
e1fcf4dd9012
core (models/types): add types for `domish.Element`.
Goffi <goffi@goffi.org>
parents:
4296
diff
changeset
|
33 Serialization is done using toXml() method, and parsing using xml_tools.parse. |
e1fcf4dd9012
core (models/types): add types for `domish.Element`.
Goffi <goffi@goffi.org>
parents:
4296
diff
changeset
|
34 """ |
e1fcf4dd9012
core (models/types): add types for `domish.Element`.
Goffi <goffi@goffi.org>
parents:
4296
diff
changeset
|
35 |
e1fcf4dd9012
core (models/types): add types for `domish.Element`.
Goffi <goffi@goffi.org>
parents:
4296
diff
changeset
|
36 @staticmethod |
e1fcf4dd9012
core (models/types): add types for `domish.Element`.
Goffi <goffi@goffi.org>
parents:
4296
diff
changeset
|
37 def validate_element(value: str) -> Element: |
e1fcf4dd9012
core (models/types): add types for `domish.Element`.
Goffi <goffi@goffi.org>
parents:
4296
diff
changeset
|
38 return parse(value) |
e1fcf4dd9012
core (models/types): add types for `domish.Element`.
Goffi <goffi@goffi.org>
parents:
4296
diff
changeset
|
39 |
e1fcf4dd9012
core (models/types): add types for `domish.Element`.
Goffi <goffi@goffi.org>
parents:
4296
diff
changeset
|
40 @staticmethod |
e1fcf4dd9012
core (models/types): add types for `domish.Element`.
Goffi <goffi@goffi.org>
parents:
4296
diff
changeset
|
41 def serialize_element(element: Element) -> str: |
e1fcf4dd9012
core (models/types): add types for `domish.Element`.
Goffi <goffi@goffi.org>
parents:
4296
diff
changeset
|
42 return element.toXml() |
e1fcf4dd9012
core (models/types): add types for `domish.Element`.
Goffi <goffi@goffi.org>
parents:
4296
diff
changeset
|
43 |
e1fcf4dd9012
core (models/types): add types for `domish.Element`.
Goffi <goffi@goffi.org>
parents:
4296
diff
changeset
|
44 @classmethod |
e1fcf4dd9012
core (models/types): add types for `domish.Element`.
Goffi <goffi@goffi.org>
parents:
4296
diff
changeset
|
45 def __get_pydantic_core_schema__( |
e1fcf4dd9012
core (models/types): add types for `domish.Element`.
Goffi <goffi@goffi.org>
parents:
4296
diff
changeset
|
46 cls, source_type: Any, handler: GetCoreSchemaHandler |
e1fcf4dd9012
core (models/types): add types for `domish.Element`.
Goffi <goffi@goffi.org>
parents:
4296
diff
changeset
|
47 ) -> core_schema.CoreSchema: |
e1fcf4dd9012
core (models/types): add types for `domish.Element`.
Goffi <goffi@goffi.org>
parents:
4296
diff
changeset
|
48 return core_schema.json_or_python_schema( |
e1fcf4dd9012
core (models/types): add types for `domish.Element`.
Goffi <goffi@goffi.org>
parents:
4296
diff
changeset
|
49 json_schema=core_schema.no_info_after_validator_function( |
e1fcf4dd9012
core (models/types): add types for `domish.Element`.
Goffi <goffi@goffi.org>
parents:
4296
diff
changeset
|
50 cls.validate_element, core_schema.str_schema() |
e1fcf4dd9012
core (models/types): add types for `domish.Element`.
Goffi <goffi@goffi.org>
parents:
4296
diff
changeset
|
51 ), |
e1fcf4dd9012
core (models/types): add types for `domish.Element`.
Goffi <goffi@goffi.org>
parents:
4296
diff
changeset
|
52 python_schema=core_schema.union_schema( |
e1fcf4dd9012
core (models/types): add types for `domish.Element`.
Goffi <goffi@goffi.org>
parents:
4296
diff
changeset
|
53 [ |
e1fcf4dd9012
core (models/types): add types for `domish.Element`.
Goffi <goffi@goffi.org>
parents:
4296
diff
changeset
|
54 core_schema.is_instance_schema(Element), |
e1fcf4dd9012
core (models/types): add types for `domish.Element`.
Goffi <goffi@goffi.org>
parents:
4296
diff
changeset
|
55 core_schema.no_info_after_validator_function( |
e1fcf4dd9012
core (models/types): add types for `domish.Element`.
Goffi <goffi@goffi.org>
parents:
4296
diff
changeset
|
56 cls.validate_element, core_schema.str_schema() |
e1fcf4dd9012
core (models/types): add types for `domish.Element`.
Goffi <goffi@goffi.org>
parents:
4296
diff
changeset
|
57 ), |
e1fcf4dd9012
core (models/types): add types for `domish.Element`.
Goffi <goffi@goffi.org>
parents:
4296
diff
changeset
|
58 ] |
e1fcf4dd9012
core (models/types): add types for `domish.Element`.
Goffi <goffi@goffi.org>
parents:
4296
diff
changeset
|
59 ), |
e1fcf4dd9012
core (models/types): add types for `domish.Element`.
Goffi <goffi@goffi.org>
parents:
4296
diff
changeset
|
60 serialization=core_schema.plain_serializer_function_ser_schema( |
e1fcf4dd9012
core (models/types): add types for `domish.Element`.
Goffi <goffi@goffi.org>
parents:
4296
diff
changeset
|
61 cls.serialize_element, when_used="json" |
e1fcf4dd9012
core (models/types): add types for `domish.Element`.
Goffi <goffi@goffi.org>
parents:
4296
diff
changeset
|
62 ), |
e1fcf4dd9012
core (models/types): add types for `domish.Element`.
Goffi <goffi@goffi.org>
parents:
4296
diff
changeset
|
63 ) |
e1fcf4dd9012
core (models/types): add types for `domish.Element`.
Goffi <goffi@goffi.org>
parents:
4296
diff
changeset
|
64 |
e1fcf4dd9012
core (models/types): add types for `domish.Element`.
Goffi <goffi@goffi.org>
parents:
4296
diff
changeset
|
65 @classmethod |
e1fcf4dd9012
core (models/types): add types for `domish.Element`.
Goffi <goffi@goffi.org>
parents:
4296
diff
changeset
|
66 def __get_pydantic_json_schema__( |
e1fcf4dd9012
core (models/types): add types for `domish.Element`.
Goffi <goffi@goffi.org>
parents:
4296
diff
changeset
|
67 cls, schema: core_schema.CoreSchema, handler: GetJsonSchemaHandler |
e1fcf4dd9012
core (models/types): add types for `domish.Element`.
Goffi <goffi@goffi.org>
parents:
4296
diff
changeset
|
68 ) -> JsonSchemaValue: |
e1fcf4dd9012
core (models/types): add types for `domish.Element`.
Goffi <goffi@goffi.org>
parents:
4296
diff
changeset
|
69 json_schema = handler(schema) |
e1fcf4dd9012
core (models/types): add types for `domish.Element`.
Goffi <goffi@goffi.org>
parents:
4296
diff
changeset
|
70 json_schema.update( |
e1fcf4dd9012
core (models/types): add types for `domish.Element`.
Goffi <goffi@goffi.org>
parents:
4296
diff
changeset
|
71 { |
e1fcf4dd9012
core (models/types): add types for `domish.Element`.
Goffi <goffi@goffi.org>
parents:
4296
diff
changeset
|
72 "format": "xml", |
e1fcf4dd9012
core (models/types): add types for `domish.Element`.
Goffi <goffi@goffi.org>
parents:
4296
diff
changeset
|
73 "description": "A valid XML element that can be parsed into a " |
e1fcf4dd9012
core (models/types): add types for `domish.Element`.
Goffi <goffi@goffi.org>
parents:
4296
diff
changeset
|
74 "domish.Element", |
e1fcf4dd9012
core (models/types): add types for `domish.Element`.
Goffi <goffi@goffi.org>
parents:
4296
diff
changeset
|
75 } |
e1fcf4dd9012
core (models/types): add types for `domish.Element`.
Goffi <goffi@goffi.org>
parents:
4296
diff
changeset
|
76 ) |
e1fcf4dd9012
core (models/types): add types for `domish.Element`.
Goffi <goffi@goffi.org>
parents:
4296
diff
changeset
|
77 return json_schema |
e1fcf4dd9012
core (models/types): add types for `domish.Element`.
Goffi <goffi@goffi.org>
parents:
4296
diff
changeset
|
78 |
e1fcf4dd9012
core (models/types): add types for `domish.Element`.
Goffi <goffi@goffi.org>
parents:
4296
diff
changeset
|
79 |
e1fcf4dd9012
core (models/types): add types for `domish.Element`.
Goffi <goffi@goffi.org>
parents:
4296
diff
changeset
|
80 class _StrictDomishElementType(_DomishElementType): |
e1fcf4dd9012
core (models/types): add types for `domish.Element`.
Goffi <goffi@goffi.org>
parents:
4296
diff
changeset
|
81 """Strict version of DomishElementType which only accept Element in Python.""" |
e1fcf4dd9012
core (models/types): add types for `domish.Element`.
Goffi <goffi@goffi.org>
parents:
4296
diff
changeset
|
82 |
e1fcf4dd9012
core (models/types): add types for `domish.Element`.
Goffi <goffi@goffi.org>
parents:
4296
diff
changeset
|
83 @classmethod |
e1fcf4dd9012
core (models/types): add types for `domish.Element`.
Goffi <goffi@goffi.org>
parents:
4296
diff
changeset
|
84 def __get_pydantic_core_schema__( |
e1fcf4dd9012
core (models/types): add types for `domish.Element`.
Goffi <goffi@goffi.org>
parents:
4296
diff
changeset
|
85 cls, source_type: Any, handler: GetCoreSchemaHandler |
e1fcf4dd9012
core (models/types): add types for `domish.Element`.
Goffi <goffi@goffi.org>
parents:
4296
diff
changeset
|
86 ) -> core_schema.CoreSchema: |
e1fcf4dd9012
core (models/types): add types for `domish.Element`.
Goffi <goffi@goffi.org>
parents:
4296
diff
changeset
|
87 return core_schema.json_or_python_schema( |
e1fcf4dd9012
core (models/types): add types for `domish.Element`.
Goffi <goffi@goffi.org>
parents:
4296
diff
changeset
|
88 json_schema=core_schema.no_info_after_validator_function( |
e1fcf4dd9012
core (models/types): add types for `domish.Element`.
Goffi <goffi@goffi.org>
parents:
4296
diff
changeset
|
89 cls.validate_element, core_schema.str_schema() |
e1fcf4dd9012
core (models/types): add types for `domish.Element`.
Goffi <goffi@goffi.org>
parents:
4296
diff
changeset
|
90 ), |
e1fcf4dd9012
core (models/types): add types for `domish.Element`.
Goffi <goffi@goffi.org>
parents:
4296
diff
changeset
|
91 python_schema=core_schema.is_instance_schema(Element), |
e1fcf4dd9012
core (models/types): add types for `domish.Element`.
Goffi <goffi@goffi.org>
parents:
4296
diff
changeset
|
92 serialization=core_schema.plain_serializer_function_ser_schema( |
e1fcf4dd9012
core (models/types): add types for `domish.Element`.
Goffi <goffi@goffi.org>
parents:
4296
diff
changeset
|
93 cls.serialize_element, when_used="json" |
e1fcf4dd9012
core (models/types): add types for `domish.Element`.
Goffi <goffi@goffi.org>
parents:
4296
diff
changeset
|
94 ), |
e1fcf4dd9012
core (models/types): add types for `domish.Element`.
Goffi <goffi@goffi.org>
parents:
4296
diff
changeset
|
95 ) |
4296
ffc43219e0b2
core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
96 |
ffc43219e0b2
core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
97 |
ffc43219e0b2
core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
98 class _JIDType: |
ffc43219e0b2
core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
99 """Use Twisted's JID in Python type, and serialize it to str |
ffc43219e0b2
core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
100 |
ffc43219e0b2
core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
101 In Python, both JID and str are accepted, str are converted to JID. |
ffc43219e0b2
core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
102 """ |
ffc43219e0b2
core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
103 |
ffc43219e0b2
core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
104 @staticmethod |
ffc43219e0b2
core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
105 def validate_jid(value: str) -> JID: |
ffc43219e0b2
core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
106 return JID(value) |
ffc43219e0b2
core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
107 |
ffc43219e0b2
core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
108 @staticmethod |
ffc43219e0b2
core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
109 def serialize_jid(jid: JID) -> str: |
ffc43219e0b2
core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
110 return str(jid) |
ffc43219e0b2
core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
111 |
ffc43219e0b2
core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
112 @classmethod |
ffc43219e0b2
core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
113 def __get_pydantic_core_schema__( |
ffc43219e0b2
core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
114 cls, source_type: Any, handler: GetCoreSchemaHandler |
ffc43219e0b2
core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
115 ) -> core_schema.CoreSchema: |
ffc43219e0b2
core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
116 return core_schema.json_or_python_schema( |
ffc43219e0b2
core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
117 json_schema=core_schema.no_info_after_validator_function( |
ffc43219e0b2
core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
118 cls.validate_jid, core_schema.str_schema() |
ffc43219e0b2
core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
119 ), |
ffc43219e0b2
core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
120 python_schema=core_schema.union_schema( |
ffc43219e0b2
core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
121 [ |
ffc43219e0b2
core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
122 core_schema.is_instance_schema(JID), |
ffc43219e0b2
core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
123 core_schema.no_info_after_validator_function( |
ffc43219e0b2
core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
124 cls.validate_jid, core_schema.str_schema() |
ffc43219e0b2
core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
125 ), |
ffc43219e0b2
core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
126 ] |
ffc43219e0b2
core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
127 ), |
ffc43219e0b2
core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
128 serialization=core_schema.plain_serializer_function_ser_schema( |
ffc43219e0b2
core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
129 cls.serialize_jid, when_used="json" |
ffc43219e0b2
core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
130 ), |
ffc43219e0b2
core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
131 ) |
ffc43219e0b2
core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
132 |
ffc43219e0b2
core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
133 @classmethod |
ffc43219e0b2
core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
134 def __get_pydantic_json_schema__( |
ffc43219e0b2
core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
135 cls, schema: core_schema.CoreSchema, handler: GetJsonSchemaHandler |
ffc43219e0b2
core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
136 ) -> JsonSchemaValue: |
ffc43219e0b2
core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
137 json_schema = handler(schema) |
ffc43219e0b2
core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
138 json_schema.update( |
ffc43219e0b2
core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
139 { |
ffc43219e0b2
core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
140 "format": "jid", |
ffc43219e0b2
core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
141 "description": "A valid Jabber ID (JID) in the format " |
ffc43219e0b2
core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
142 '"user@domain/resource"', |
ffc43219e0b2
core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
143 } |
ffc43219e0b2
core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
144 ) |
ffc43219e0b2
core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
145 return json_schema |
ffc43219e0b2
core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
146 |
ffc43219e0b2
core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
147 |
ffc43219e0b2
core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
148 class _StrictJIDType(_JIDType): |
ffc43219e0b2
core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
149 """Strict version of JIDType which only accept JID in Python.""" |
ffc43219e0b2
core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
150 |
ffc43219e0b2
core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
151 @classmethod |
ffc43219e0b2
core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
152 def __get_pydantic_core_schema__( |
ffc43219e0b2
core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
153 cls, source_type: Any, handler: GetCoreSchemaHandler |
ffc43219e0b2
core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
154 ) -> core_schema.CoreSchema: |
ffc43219e0b2
core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
155 return core_schema.json_or_python_schema( |
ffc43219e0b2
core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
156 json_schema=core_schema.no_info_after_validator_function( |
ffc43219e0b2
core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
157 cls.validate_jid, core_schema.str_schema() |
ffc43219e0b2
core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
158 ), |
ffc43219e0b2
core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
159 python_schema=core_schema.is_instance_schema(JID), |
ffc43219e0b2
core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
160 serialization=core_schema.plain_serializer_function_ser_schema( |
ffc43219e0b2
core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
161 cls.serialize_jid, when_used="json" |
ffc43219e0b2
core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
162 ), |
ffc43219e0b2
core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
163 ) |
ffc43219e0b2
core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
164 |
ffc43219e0b2
core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
165 |
ffc43219e0b2
core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
166 # We annotate the types so static type checker understand them as JID. |
ffc43219e0b2
core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
167 JIDType = Annotated[JID, _JIDType] |
ffc43219e0b2
core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
168 StrictJIDType = Annotated[JID, _StrictJIDType] |
4324
e1fcf4dd9012
core (models/types): add types for `domish.Element`.
Goffi <goffi@goffi.org>
parents:
4296
diff
changeset
|
169 DomishElementType = Annotated[Element, _DomishElementType] |
e1fcf4dd9012
core (models/types): add types for `domish.Element`.
Goffi <goffi@goffi.org>
parents:
4296
diff
changeset
|
170 StrictDomishElementType = Annotated[Element, _StrictDomishElementType] |