4lessandrodev / rich-domain

A lib to help you create a robust project based on domain driven-design (ddd) principles with typescript and zero dependencies.
https://www.npmjs.com/package/rich-domain
MIT License
122 stars 5 forks source link

Error When Cloning ValueObject #152

Closed 4lessandrodev closed 5 months ago

4lessandrodev commented 5 months ago

Description:

When a ValueObject is cloned using the clone() method, the value of the cloned object is not properly maintained. When attempting to access the value using the get('value') method, the return is null instead of the original value.

Steps to Reproduce:

Expected Behavior:

The clone() method should create an exact copy of the ValueObject, keeping the original value intact. Therefore, when accessing the value using the get('value') method on the cloned instance (copy), the returned value should be the same as on the original instance (main).

Observed Behavior:

After cloning the ValueObject, the value returned when accessing get('value') on the cloned instance is null, instead of maintaining the original value.

Environment:

Language: TypeScript Framework/Dependency: rich-domain (version v1.23.0) Operating System: linux Browser/Platform: nodejs v20

Sample Code:


import { ValueObject } from 'rich-domain';

class Branch extends ValueObject<string>{
    private constructor(value: string){
        super(value);
    }

    public static init(value: string): Branch {
        return new Branch(value);
    }
}

const main = Branch.init('main'); // props: 'main'
const copy = main.clone(); // props: { '0': 'm', '1': 'a', '2': 'i', '3': 'n' }

console.log(main.get('value')); // main
console.log(copy.get('value')); // null