Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

xsdata dataclass generation from XSD with choice fields #1170

Unanswered
luisdanielsc asked this question in Q&A
Discussion options

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

You must be logged in to vote

Replies: 0 comments

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
1 participant

AltStyle によって変換されたページ (->オリジナル) /