We should add a built-in unique constraint under comparison constraints to validate that array elements are unique.
Proposal
Add a new built-in constraint named unique with options shaped like the existing built-ins, including shared validator metadata such as groups:
type UniqueOptions = ConstraintOptions & {
message?: string;
normalizer?: (value: unknown) => unknown;
fields?: string[];
};
Example usage:
const rules = {
tags: [{ unique: {} }],
emails: [{ unique: { normalizer: value => typeof value === 'string' ? value.trim() : value } }],
coordinates: [{ unique: { fields: ['latitude', 'longitude'] } }],
usernames: [{ unique: { groups: ['create'] } }],
};
Expected behavior
unique should live in the comparison constraint group.
- It should be exported from the validator constraints public API and registered in
builtInConstraints.
- It should support shared validator metadata exposed through
ConstraintOptions, including groups.
- It should validate arrays for duplicate elements.
- By default, comparison should be strict, so
'7' and 7 are considered different values.
- It should return no violations when all array elements are unique.
- It should return one violation when at least one duplicate is found.
- It should support a custom
message option, consistent with the existing constraints.
normalizer should be applied to each array element before uniqueness is checked.
fields should allow uniqueness checks based on a combination of fields for arrays of objects.
- It should preserve current optional-field semantics used by other constraints:
undefined should be considered valid unless presence is enforced separately.
null handling should be defined explicitly and covered by tests. [Failure, as null is not a list]
undefined handling should be defined explicitly and covered by tests. [ByPass validation]
- Non-array values should be handled explicitly and covered by tests.
Default message
A sensible default would be:
This collection should contain only unique elements.
We should add a built-in
uniqueconstraint undercomparisonconstraints to validate that array elements are unique.Proposal
Add a new built-in constraint named
uniquewith options shaped like the existing built-ins, including shared validator metadata such asgroups:Example usage:
Expected behavior
uniqueshould live in thecomparisonconstraint group.builtInConstraints.ConstraintOptions, includinggroups.'7'and7are considered different values.messageoption, consistent with the existing constraints.normalizershould be applied to each array element before uniqueness is checked.fieldsshould allow uniqueness checks based on a combination of fields for arrays of objects.undefinedshould be considered valid unless presence is enforced separately.nullhandling should be defined explicitly and covered by tests. [Failure, asnullis not a list]undefinedhandling should be defined explicitly and covered by tests. [ByPass validation]Default message
A sensible default would be: