You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+22-2Lines changed: 22 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,8 @@
1
1
# XML dataclasses
2
2
3
-
This is a very rough prototype of how a library might look like for (de)serialising XML into Python dataclasses. XML dataclasses build on normal dataclasses from the standard library and [`lxml`](https://pypi.org/project/lxml/) elements. Loading and saving these elements is left to the consumer for flexibility of the desired output.
This is a prototype of how a library might look like for (de)serialising XML into Python dataclasses. XML dataclasses build on normal dataclasses from the standard library and [`lxml`](https://pypi.org/project/lxml/) elements. Loading and saving these elements is left to the consumer for flexibility of the desired output.
4
6
5
7
It isn't ready for production if you aren't willing to do your own evaluation/quality assurance. I don't recommend using this library with untrusted content. It inherits all of `lxml`'s flaws with regards to XML attacks, and recursively resolves data structures. Because deserialisation is driven from the dataclass definitions, it shouldn't be possible to execute arbitrary Python code. But denial of service attacks would very likely be feasible.
6
8
@@ -46,10 +48,16 @@ class Container:
46
48
rootfiles: RootFiles
47
49
# WARNING: this is an incomplete implementation of an OPF container
48
50
51
+
defxml_validate(self):
52
+
ifself.version !="1.0":
53
+
raiseValueError(f"Unknown container version '{self.version}'")
* Lists of child elements are supported, as are unions and lists or unions
65
73
* Inheritance does work, but has the same limitations as dataclasses. Inheriting from base classes with required fields and declaring optional fields doesn't work due to field order. This isn't recommended
66
74
* Namespace support is decent as long as correctly declared. I've tried on several real-world examples, although they were known to be valid. `lxml` does a great job at expanding namespace information when loading and simplifying it when saving
75
+
* Post-load validation hook `xml_validate`
67
76
68
77
## Patterns
69
78
@@ -117,6 +126,17 @@ Children can be renamed via the `rename` function, however attempting to set a n
117
126
118
127
If a class has children, it cannot have text content.
119
128
129
+
### Defining post-load validation
130
+
131
+
Simply implement an instance method called `xml_validate` with no parameters, and no return value (if you're using type hints):
132
+
133
+
```python
134
+
defxml_validate(self) -> None:
135
+
pass
136
+
```
137
+
138
+
If defined, the `load` function will call it after all values have been loaded and assigned to the XML dataclass. You can validate the fields you want inside this method. Return values are ignored; instead raise and catch exceptions.
0 commit comments