Replies: 1 comment
-
Technically we don't support union types, in versions before v4 we even let data throw an exception when an union type was used, that restriction was removed because I think in future versions we want to support this. But it is quite complicated since we need to rethink the casting, transforming, validation process. The problem lies here: laravel-data/src/DataPipes/CastPropertiesDataPipe.php Lines 84 to 93 in 7eda411 Since we allow data objects to be constructed from anything: arrays, strings, int's it is quite difficult to decide what should happen in this case. A MoneyData object could be totally valid to be constructed from a single int. We could check if a magic constructor exists for the property value but that would probably cost a lot of time. What we could do, is trying to create the data object and if it fails just use the value. I've implemented this but it will not work with collectables since that's a whole other story. (6075b43) |
Beta Was this translation helpful? Give feedback.
-
It seems that union types that include a
Data
class will always try to normalize the other type intoData
. The following throws an error:I would expect that if
int
is an accepted type, and if the passed value is anint
, this package would not try to cast it into theTaxData
object.I understand that I can use a custom cast, or even load the model and cast it to
TaxData
in the constructor, but given that there may be 100s ofOrderItem
s for a single order, it may mean that the sameTax
models is loaded 100s of times from the DB. Instead, I'd like to skip loading theTax
models when receiving input from frontend, and only useTaxData
when sending the data to the frontend.Any thoughts?
Beta Was this translation helpful? Give feedback.
All reactions