Skip to content

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.

  • Strings: Trimmed and compared case-insensitively.
  • Other types: Compared using strict equality (===).

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.

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.

SchemaPath<S>

The SchemaPath to the ArrayBlock or raw array containing the items to validate.

ErrorOption & object & object

Configuration options for the validator.

void

// Basic usage with strings (case-insensitive, trimmed by default)
schema<ArrayBlock<string>>(path => {
unique(path);
});
// Usage with raw arrays
schema<string[]>(path => {
unique(path);
});
// Custom equality function and error message
interface 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 destination
const dest = signal<'container' | 'items'>('items');
schema<ArrayBlock<string>>(path => {
unique(path, { destination: dest });
});