numberRule
numberRule<
TOptions>(options):NumberRule<TOptions>
Defined in: packages/ddd-core/domain/rules/number.rule.ts:95
Defines the rule for one number field of an aggregate: declared once, used by the aggregate to parse the value, and read by other layers (such as a request schema) for its limits.
parse(value) checks, in this order:
nullorundefined:null, or arequiredviolation;- not a number: a
typeviolation (a numeric string is not converted); NaNor an infinity: afiniteviolation;- with
integer, not a safe integer: anintegerviolation; min, thenmax.
Type Parameters
Section titled “Type Parameters”TOptions
Section titled “TOptions”TOptions extends NumberRuleOptions
Parameters
Section titled “Parameters”options
Section titled “options”TOptions
The field name, its constraints and its error.
Returns
Section titled “Returns”NumberRule<TOptions>
The frozen rule.
Throws
Section titled “Throws”TypeError when an option is invalid: an empty field, a bound that
is not a finite number, or min above max.
Example
Section titled “Example”export class Order extends RootEntity<OrderSnapshot> { static readonly rules = { quantity: numberRule({ field: 'quantity', integer: true, min: 1, max: 100, error: (violation) => new InvalidQuantityException(violation), }), } as const;
@Mutable<number>({ normalize: (value) => Order.rules.quantity.parse(value) }) private _quantity: number;}
Order.rules.quantity.parse(0);// throws InvalidQuantityException: "quantity must be at least 1."