-
-
Notifications
You must be signed in to change notification settings - Fork 79
xsdata dataclass generation from XSD with choice fields #1170
Unanswered
luisdanielsc
asked this question in
Q&A
-
How is it possible using xsdata for the generation of the model (dataclass) from an XSD with choice elements, to perform validations on those choices, so that it only allows one of them to be set?
XSD fragment
<xs:complexType name="ThirdPartyType">
<xs:choice>
<xs:element name="LegalEntity" type="LegalEntityType"/>
<xs:element name="Individual" type="IndividualType"/>
</xs:choice>
</xs:complexType>
I have searched the web and found that this requirement could be validated using the __post_init__ method; but would it be possible to add some hook to xsdata, so that it automatically includes the implementation of the __post_init__ method when generating the model with xsdata?
@dataclass
class ThirdPartyType:
legal_entity: Optional[LegalEntityType] = field(
default=None,
metadata={
"name": "LegalEntity",
"type": "Element",
"namespace": "",
},
)
individual: Optional[IndividualType] = field(
default=None,
metadata={
"name": "Individual",
"type": "Element",
"namespace": "",
},
)
def __post_init__(self):
filled = sum(v is not None for v in [self.legal_entity, self.individual])
if filled > 1:
raise ValueError("Only one of ['legal_entity', 'individual'] can be defined")
Note: I am running the command from the CLI
Beta Was this translation helpful? Give feedback.
All reactions
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment