This commit is contained in:
David Arranz 2025-01-30 10:24:08 +01:00
parent f5c9a0a995
commit 6649374ad8
5 changed files with 12 additions and 19 deletions

View File

@ -17,6 +17,6 @@ export class UserRoles extends ValueObject<string[]> {
}
hasRole(role: string): boolean {
return this.value.includes(role);
return this._value.includes(role);
}
}

View File

@ -16,8 +16,4 @@ export class EmailAddress extends ValueObject<string | null> {
const schema = z.string().email({ message: "Invalid email format" }).or(z.null());
return schema.safeParse(email);
}
isEmpty(): boolean {
return this.value === null;
}
}

View File

@ -30,6 +30,6 @@ export class PasswordHash extends ValueObject<string> {
}
async compare(plainPassword: string): Promise<boolean> {
return await bcrypt.compare(plainPassword, this.value);
return await bcrypt.compare(plainPassword, this._value);
}
}

View File

@ -1,13 +1,10 @@
import { shallowEqual } from "shallow-equal-object";
export abstract class ValueObject<T> {
protected readonly value: T;
protected readonly _value: T;
protected constructor(value: T) {
if (value === null || value === undefined) {
throw new Error("ValueObject value cannot be null or undefined");
}
this.value = typeof value === "object" ? Object.freeze(value) : value;
this._value = typeof value === "object" && value !== null ? Object.freeze(value) : value;
Object.freeze(this);
}
@ -17,22 +14,22 @@ export abstract class ValueObject<T> {
return false;
}
if (other.value === undefined) {
if (other._value === undefined || other._value === null) {
return false;
}
return shallowEqual(this.value, other.value);
return shallowEqual(this._value, other._value);
}
getValue(): T {
return this.value;
return this._value;
}
isEmpty(): boolean {
return this._value === null || this._value === undefined;
}
toString(): string {
return String(this.value);
}
clone(): this {
return Object.create(this);
return this._value !== null && this._value !== undefined ? String(this._value) : "";
}
}

View File