-
-
Notifications
You must be signed in to change notification settings - Fork 53
Description
I am using 'ts-money' and it freezes the objects as last line of its constructor:
This is its simplified constructor:
constructor(amount, currency) {
this.amount = amount;
this.currency = currency.code;
Object.freeze(this);
}
As there are no serializr decorations on the Money object, I created the following schema for it:
import { Currencies, Money, type Currency } from 'ts-money'
createModelSchema(
Money,
{
amount: primitive(),
currency: primitive()
},
(context: Context): Money => new Money(context.json.amount, context.json.currency)
);
But when I serialize, then I get the following error:
FAIL src/util/serializr.test.ts > get a first impression of serializr > test a map with Money objects
TypeError: Cannot assign to read only property 'amount' of object '[object Object]'
❯ node_modules/.pnpm/serializr@3.0.2/node_modules/serializr/src/core/deserialize.ts:215:51
❯ node_modules/.pnpm/serializr@3.0.2/node_modules/serializr/src/core/Context.ts:64:17
❯ node_modules/.pnpm/serializr@3.0.2/node_modules/serializr/src/utils/utils.ts:16:26
❯ onAfterDeserialize node_modules/.pnpm/serializr@3.0.2/node_modules/serializr/src/core/deserialize.ts:201:9
❯ node_modules/.pnpm/serializr@3.0.2/node_modules/serializr/src/core/deserialize.ts:223:17
❯ Object.deserializer node_modules/.pnpm/serializr@3.0.2/node_modules/serializr/src/types/primitive.ts:25:25
❯ deserializeProp node_modules/.pnpm/serializr@3.0.2/node_modules/serializr/src/core/deserialize.ts:217:17
❯ callbackDeserialize node_modules/.pnpm/serializr@3.0.2/node_modules/serializr/src/core/deserialize.ts:253:17
❯ onBeforeDeserialize node_modules/.pnpm/serializr@3.0.2/node_modules/serializr/src/core/deserialize.ts:175:9
❯ _loop_2 node_modules/.pnpm/serializr@3.0.2/node_modules/serializr/src/core/deserialize.ts:256:9
Would be great if serializr would support the creation of immutable objects.
Would also be great to state the fact, it doesn't support immutable objects in the document. Have wasted several hours now.
Edit: I just have realized, I can use a before deserialization handler to prevent the error to happen and still get the full object tree. Maybe this request ist obsolete now, but I hae the feeling it's a bit hacky to misuse the handler in this case.
Maybe it would be enough to check after the call of factory method, whether the returned object is frozen or not.
If not continue as normal, if frozen, just call it a day.
Thanks for considering.