ValueObject
Can be used to measure or describe things (name, description, amount, height, date, time, range, address, etc.)
ValueObjects are only identified by their values, (for example money is a value object as long as we are not tracking individual banknotes, if we need to track individual banknotes then it should be a banknote entity)
Basically, they are all the attributes you need to validate the status and they don't have a unique ID
import { Result, ValueObject } from 'types-ddd';
interface Prop {
value: string;
}
class EmailValueObject extends ValueObject<Prop> {
private constructor(prop: Prop) {
super(prop);
}
get value(): string {
return this.props.value;
}
public static isValidEmail (email: string): boolean {
const regex = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/g;
return regex.test(email);
}
public static create(value: string): Result<EmailValueObject> {
const isValid = EmailValueObject.isValidEmail(value);
if (!isValid) {
return Result.fail<EmailValueObject>('Invalid email');
}
return Result.ok<EmailValueObject>(
new EmailValueObject({ value: value.toLowerCase() }),
);
}
}
Lets test our ValueObject
Now lets see a fail example
How to get the ValueObject value
You always have to validate if the result is a failure before getting the result
Last updated
Was this helpful?