Skip to main content

Valuable items

Value objects are objects that represent a value, such as a phone number, name, or date. These objects are immutable, that is, once created they cannot change their state. For example, an ethereum address is a value object, since it cannot change its value once created.

Primitives

Value objects encapsulate a primitive value, such as a string, number, or boolean. They make sure that the encapsulated value is valid, for example, that a phone number is in the correct format. And also that the value is not null or undefined. The StringValueObject, NumberValueObject and EnumValueObject<T> classes are used to create a new value object. They all extend from the ValueObjet<Type> class.

import { StringValueObject, NumberValueObject, EnumValueObject } from '@alfabc/sdk';

class Name extends StringValueObject {}

class Age extends NumberValueObject {}

enum GenderTypes {
Male,
female
}

class UserGender extends EnumValueObject<GenderTypes> {}

Create a new primitive value object

To create a new value object of a different type than the primitives, you must extend the ValueObject<Type> class.

import { ValueObject } from '@alfabc/sdk';

class BirthDate extends ValueObject<Date> {}

newBirthDate(newDate('1990-01-01'));