unique
unique<
S,T>(fieldPath,options?):void
Defined in: projects/ngx-signal-schema/src/lib/validators/unique.ts:86
The unique validator checks if all items within an ArrayBlock or a raw array are unique.
If duplicates are found, it generates validation errors.
Default behavior
Section titled “Default behavior”- Strings: Trimmed and compared case-insensitively.
- Other types: Compared using strict equality (
===).
Error reporting
Section titled “Error reporting”By default, errors are attached to both the container (ArrayBlock or the array itself)
and each individual item that is part of a duplicate set. This can be configured using the destination option.
Type Parameters
Section titled “Type Parameters”S extends ArrayBlock<T> | T[] | null | undefined
The type of the schema path, extending ArrayBlock, T[], or being null/undefined.
T
The type of the elements in the array.
Parameters
Section titled “Parameters”fieldPath
Section titled “fieldPath”SchemaPath<S>
The SchemaPath to the ArrayBlock or raw array containing the items to validate.
options?
Section titled “options?”ErrorOption & object & object
Configuration options for the validator.
Returns
Section titled “Returns”void
Examples
Section titled “Examples”// Basic usage with strings (case-insensitive, trimmed by default)schema<ArrayBlock<string>>(path => { unique(path);});// Usage with raw arraysschema<string[]>(path => { unique(path);});// Custom equality function and error messageinterface User { id: number; name: string; }schema<ArrayBlock<User>>(path => { unique(path, { equalFn: (a, b) => a.id === b.id, error: { message: 'User IDs must be unique' } });});// Attach errors only to items container (items containers)schema<ArrayBlock<string>>(path => { unique(path, { destination: 'container' });});// Using a signal for dynamic destinationconst dest = signal<'container' | 'items'>('items');schema<ArrayBlock<string>>(path => { unique(path, { destination: dest });});