Skip to main content

minLength()

🧭 API map​

I want to…Details
See every accepted call styleSignatures
See common and advanced usageUsage and behavior
Customize messagesMessage configuration
Understand reactive constraintsReactive behavior
Return to the complete catalogBuilt-in validators

πŸ“ Signatures​

minLength(minimum)
minLength(minimum, message)
minLength(minimum, options)

Reactive constraint arguments use a zero-argument function. The function may read signals and, where supported, return undefined to disable the constraint temporarily.

πŸ“– Usage and behavior​

Requires a numeric length or size to meet a minimum, with optional empty text by default:

min-length.example.ts
import { field, form, minLength, required } from '@ngblocks/form-nodes';

const profile = form({
username: field('', [minLength(3)]),
nickname: field<string>(null, [minLength(3)]),
displayName: field('', [required, minLength(3)]),
});

profile.username(); // ''
profile.username.valid(); // true
profile.nickname.valid(); // true: null has no length to check
profile.displayName.errors().map(error => error.kind); // ['required']

if (!profile.username.valid() || !profile.nickname.valid()
|| profile.displayName.errors().map(error => error.kind).join(',') !== 'required') {
throw new Error('Empty text and null must pass minimum length; required must reject empty text.');
}

profile.username.set('Ada');
profile.displayName.set('Ada');
profile.valid(); // true
if (!profile.valid()) throw new Error('Strings meeting the minimum must be valid.');

profile.nickname.set('');
profile.nickname.valid(); // true: empty text remains optional
if (!profile.nickname.valid()) throw new Error('An empty nickname must remain valid.');

profile.nickname.setValidators([minLength(3, { allowEmptyString: false })]);
profile.nickname.getError('minLength')?.actual; // 0
if (!profile.nickname.hasError('minLength')) throw new Error('Explicit empty-string validation must measure zero.');

It supports strings, arrays, sets, maps, and other values with numeric length or size. null, undefined, and empty strings pass by default. Empty collections have length zero and fail a positive minimum. minLength(0) permits empty collections. Whitespace counts without trimming. Explicitly undefined-valued fields such as field<string>(undefined, [minLength(3)]) are also supported.

A failure is { kind: 'minLength', minLength, actual, message }. The resolved limit contributes to minLength() metadata, but does not mark the node as required.

Empty strings pass; empty arrays must meet the minimum

With minLength(3), '' is valid, but [] is invalid. An empty string skips the length check by default; an empty array is measured as zero items. Nonempty strings such as 'a' must meet the minimum and are invalid when shorter than three characters.

min-length-empty-values.example.ts
import { field, form, minLength } from '@ngblocks/form-nodes';

const profile = form({
name: field('', [minLength(3)]),
tags: field<string[]>([], [minLength(3)]),
});

profile.name.valid(); // true: '' is allowed by default
profile.tags.valid(); // false: [] has fewer than three items
profile.tags.getError('minLength')?.actual; // 0
if (!profile.name.valid() || profile.tags.valid() || profile.tags.getError('minLength')?.actual !== 0) {
throw new Error('Empty text must pass while an empty array fails the same minimum.');
}

profile.name.set('a');
profile.name.valid(); // false: populated text must have at least three characters
if (profile.name.valid()) throw new Error('Nonempty text must meet the minimum.');

profile.name.set('Ada');
profile.tags.set(['angular', 'forms', 'signals']);
profile.valid(); // true: both populated values meet the minimum
if (!profile.valid()) throw new Error('Text and arrays meeting the minimum must pass.');

This follows Angular Signal Forms. Reactive Forms also allows '', but additionally skips empty collections. Add required to reject empty text and nullish values; an empty string then produces only the required error with the default options.

Optional empty text​

An empty string passes without a condition, while populated text must meet the minimum:

min-length-optional.example.ts
import { field, form, minLength } from '@ngblocks/form-nodes';

const profile = form({
nickname: field('', [
minLength(3),
]),
});

profile.nickname.valid(); // true: empty text is allowed by default
if (!profile.valid()) throw new Error('An optional empty nickname must be valid.');

profile.nickname.set('Al');
profile.nickname.hasError('minLength'); // true
if (!profile.nickname.hasError('minLength')) throw new Error('A populated nickname must meet the minimum.');

profile.nickname.set('Alex');
profile.nickname.valid(); // true
if (!profile.valid()) throw new Error('A sufficiently long nickname must be valid.');

profile.nickname.set('');
profile.nickname.minLength(); // 3: the constraint remains configured
if (!profile.valid() || profile.nickname.minLength() !== 3) {
throw new Error('Clearing an optional nickname must preserve its constraint without an error.');
}

The configured minimum remains in minLength() metadata and on bound controls, even for ''. A false when condition instead removes both the errors and the rule's constraint metadata.

Validate empty strings​

Set allowEmptyString: false to measure '' as zero, retaining the previous behavior:

minLength(3, { allowEmptyString: false })

allowEmptyString is a static boolean, defaults to true, and only affects empty strings. Nullish values still pass, and empty collections still fail a positive minimum in either mode. With allowEmptyString: false, combining required and minLength produces both errors for ''.

πŸ’¬ Message configuration​

Every failure has a default English message. Pass a string as the final argument or use an options object for a static or reactive message:

minLength(3, 'Enter at least three characters.')

A message function may read signals. Returning undefined continues through node, Angular provider, process-wide, and built-in message fallbacks. See Validator messages.

⚑ Reactive behavior​

The minimum may be a signal or a zero-argument function. Changes update its metadata even while the string is empty; populated strings revalidate. Returning undefined disables the constraint:

min-length-reactive.example.ts
import { signal } from '@angular/core';
import { field, form, minLength } from '@ngblocks/form-nodes';

const minimum = signal<number | undefined>(3);
const profile = form({
nickname: field('Al', [minLength(minimum)]),
});

profile.nickname.valid(); // false
if (profile.valid()) throw new Error('Text shorter than the minimum must fail.');

minimum.set(0);
profile.nickname.valid(); // true
if (!profile.valid()) throw new Error('A zero minimum must allow empty text.');

minimum.set(3);
profile.nickname.getError('minLength')?.actual; // 2
if (!profile.nickname.hasError('minLength')) throw new Error('A changed minimum must revalidate populated text.');

minimum.set(undefined);
profile.nickname.valid(); // true: the constraint is disabled
if (!profile.valid() || profile.nickname.minLength() !== null) {
throw new Error('An undefined minimum must disable validation and constraint metadata.');
}

Reactive constraint functions and message functions track the signals they read. When a resolved constraint becomes unavailable, validators that support optional constraint sources temporarily stop contributing their error and metadata.

The validator runs synchronously as part of its node's validator source. Disabled, readonly, and hidden nodes skip validation until they become interactive again.

For an inclusive range in one validator, use lengthBetween().