Skip to content

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:

  1. null or undefined: null, or a required violation;
  2. not a number: a type violation (a numeric string is not converted);
  3. NaN or an infinity: a finite violation;
  4. with integer, not a safe integer: an integer violation;
  5. min, then max.

TOptions extends NumberRuleOptions

TOptions

The field name, its constraints and its error.

NumberRule<TOptions>

The frozen rule.

TypeError when an option is invalid: an empty field, a bound that is not a finite number, or min above max.

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."