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:
nullorundefined:null, or arequiredviolation;- not a string: a
typeviolation; - an unpaired surrogate: a
charactersviolation, since it cannot be stored as UTF-8; - Unicode NFC normalization, then trimming; blank text is
null, or arequiredviolation; - a control character (tab and line breaks too, unless
multiline): acharactersviolation; minLength,maxLength, thenpattern.
Type Parameters
Section titled “Type Parameters”TOptions
Section titled “TOptions”TOptions extends TextRuleOptions
Parameters
Section titled “Parameters”options
Section titled “options”TOptions
The field name, its constraints and its error.
Returns
Section titled “Returns”TextRule<TOptions>
The frozen rule.
Throws
Section titled “Throws”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.
Example
Section titled “Example”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