Skip to content

textRule

textRule<TOptions>(options): TextRule<TOptions>

Defined in: packages/ddd-core/domain/rules/text.rule.ts:118

Defines the rule for one text 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 string: a type violation;
  3. an unpaired surrogate: a characters violation, since it cannot be stored as UTF-8;
  4. Unicode NFC normalization, then trimming; blank text is null, or a required violation;
  5. a control character (tab and line breaks too, unless multiline): a characters violation;
  6. minLength, maxLength, then pattern.

TOptions extends TextRuleOptions

TOptions

The field name, its constraints and its error.

TextRule<TOptions>

The frozen rule.

TypeError when an option is invalid: an empty field, a length that is not a non-negative integer, minLength above maxLength, or a pattern with a g or y flag.

export class User extends RootEntity<UserSnapshot> {
static readonly rules = {
username: textRule({
field: 'username',
minLength: 3,
maxLength: 255,
error: (violation) => new InvalidUsernameException(violation),
}),
} as const;
@Mutable<string>({ normalize: (value) => User.rules.username.parse(value) })
private _username: string;
}
User.rules.username.parse(' Ana María '); // 'Ana María'
z.string().trim().min(User.rules.username.minLength); // a request schema