Skip to content

unique

unique<S, T>(fieldPath, options?): void

Defined in: projects/ngx-signal-schema/src/lib/validators/unique.ts:77

The unique validator checks if all items within an 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 each individual item that is part of a duplicate set. This can be configured using the destination option.

S extends T[] | null | undefined

The type of the schema path, extending T[], or being null/undefined.

T

The type of the elements in the array.

SchemaPath<S>

The SchemaPath to the 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<string[]>(path => {
unique(path);
});
// Custom equality function and error message
interface User { id: number; name: string; }
schema<User[]>(path => {
unique(path, {
equalFn: (a, b) => a.id === b.id,
error: { message: 'User IDs must be unique' }
});
});
// Attach errors only to the array container
schema<string[]>(path => {
unique(path, { destination: 'container' });
});
// Using a signal for dynamic destination
const dest = signal<'container' | 'items'>('items');
schema<string[]>(path => {
unique(path, { destination: dest });
});