A ValueObject is a wrapper for a primitive value.
It extends the value to include the ability to set constraints on creation and to
validate compliance with the constraints. Furthermore, they ensure that the value is
immutable and thus cannot be changed.
every ValueObject has an options parameter for create(), validate() and fromList().
These options can be used to define constraints for the passed value(s).
There are some common options that are used independently of the individual ValueObject:
// All ValueObjects [all methods] interfaceCreationOptions { name?: string; // the name of the ValueObject to identify in a possible ErrorMessage. }
// Numeric & (string) ValueObjects [all methods] interfaceIntervalCreationOptionsextendsCreationOptions { min?: number; // the lower bound of the interval the value (length) has to be in max?: number; // the upper bound of the interval the value (length) has to be in }
// All ValueObjects [fromList(value, OPTIONS)] interfaceListCreationOptions { forbidUndefined?: boolean; // is undefined forbidden as a valid input listSize?: { min?: number; // minimum amount of values inside of the list max?: number; // maximum amount of values inside of the list fix?: number; // fix amount of values inside of the list. This serves as a shortcut for min = max. } }
Most ValueObjects have additional options depending on their individual needs. Those are always called <ValueObjectName>Options
Information
A ValueObject is a wrapper for a primitive value. It extends the value to include the ability to set constraints on creation and to validate compliance with the constraints. Furthermore, they ensure that the value is immutable and thus cannot be changed.
Remarks
create(value, options)
method.validate(value, options)
method is automatically called, which can also be used separately.API
every ValueObject inherits from the
abstract ValueObject
:.value
- the getter for the internal primitive value.toJSON()
- which returns a readable representation of the value (commonly just the value).equals(...)
- for comparison of ValueObjectsadditionally every ValueObject has at least a static implementation for:
.create(value, options)
- to create (and validate) the ValueObject.validate(value, options)
- to validate the constraints on the value.fromList(values, options)
- to create a list of ValueObjects from a list of primitives (this is the list equivalent of.create()
).toList(values)
- to transform a list of ValueObjects to a list of primitives (this is the list equivalent of.value
)some of the ValueObjects (especially the composed ones) have additional useful methods.
Options
every ValueObject has an
options
parameter forcreate()
,validate()
andfromList()
. These options can be used to define constraints for the passed value(s).There are some common options that are used independently of the individual ValueObject:
Most ValueObjects have additional options depending on their individual needs. Those are always called
<ValueObjectName>Options
Basic ValueObjects
numeric
Float
- the common wrapper fornumber
Integer
- an extension ofFloat
without decimal digitsFloatString
- also aFloat
, but can be created from astring
IntegerString
- also anInteger
, but can be created from astring
NumericId
- an extension ofInteger
with the inbuilt constraints to be positive and not round-ablestring
OptionalString
- the common wrapper forstring
, but can be created from falsy values (if required)NonEmptyString
- an extension ofOptionalString
, but can only be created from strings that are not emptyother (pseudo) primitive
SafeBoolean
- the common wrapper forboolean
, that can also be created fromundefined
orstring
(if required)SafeDate
- the common wrapper for JS'sDate