Skip to content

feat(Validation): Add unique validator constraint#179

Open
EmanFateen wants to merge 22 commits into
koala-ts:2.xfrom
EmanFateen:add-unqiue-compariosn-type
Open

feat(Validation): Add unique validator constraint#179
EmanFateen wants to merge 22 commits into
koala-ts:2.xfrom
EmanFateen:add-unqiue-compariosn-type

Conversation

@EmanFateen
Copy link
Copy Markdown
Contributor

@EmanFateen EmanFateen commented May 15, 2026

Overview

In this PR, I have introduced a new validation constraint called unique.

export type UniqueOptions = ConstraintOptions<{
  message?: string;
  normalizer?: (value: unknown) => unknown;
  fields?: string[];
}>;

It's exposed through the builtInConstraints and can be used like

const rules = {
  tags: [{ unique: {} }],
  emails: [{ unique: { normalizer: value => typeof value === 'string' ? value.trim() : value } }],
  coordinates: [{ unique: { fields: ['latitude', 'longitude'] } }],
  usernames: [{ unique: { groups: ['create'] } }],
};
  • Main purpose of this constraint is to validate whether the given collection contains only unique elements.
  • By default, uniqueness is checked with strict comparison semantics.
  • The constraint supports a normalizer to transform values before uniqueness is checked.
  • The constraint also supports fields to validate uniqueness based on a selected combination of object fields.
  • This constraint preserves the current optional-field behavior, which means if the value is undefined the constraint will return no violations.

Changes made:

  1. Added the unique constraint under comparison constraints.
  2. Covered the required cases in unique.test file.
  3. Exposed the unique constraint through the builtInConstraints object.
Q A
License GPLv3
Issue Closes #170 >

@EmanFateen EmanFateen requested a review from imdhemy as a code owner May 15, 2026 14:51
EmanFateen and others added 11 commits May 18, 2026 15:32
# Conflicts:
#	src/validator/constraints/index.ts
* feat: add passing type constraint

* chore: cover type constraint violation

* chore: support matching type constraint values

* chore: support type constraint alternatives

* chore: allow optional type constraint values

* chore: support type constraint custom message

* chore: format type constraint alternatives

* chore: register type constraint

# Conflicts:
#	src/validator/constraints/index.ts

* chore: fix code style

* chore: use TypeOptions due to rebase

* Simplify type constraint message formatting

* chore: add separate test case for the undefined values.

* chore: refactor getDefaultMessageWith method.

* chore: add a list of AllowedTypes types

* chore: code format

* chore: remove redundant test case

* chore: remove redundant check

* chore: fix AllowedTypes

* chore: inline options

* chore: rename test case

* chore: use one default message

* chore: inline method

* chore: rename AllowedType

* chore: refactor  normalizedTypes

* chore: remove the undefined type case from each

* chore(validator): make constraint options generic

* chore: align type constraint options

* chore: code refactoring

* chore: fix type mismatch

---------

Co-authored-by: Dhemy <imdhemy@gmail.com>
# Conflicts:
#	src/validator/constraints/index.ts
@EmanFateen EmanFateen force-pushed the add-unqiue-compariosn-type branch from 66fbc49 to 0cc1b39 Compare May 18, 2026 12:33
@EmanFateen EmanFateen marked this pull request as draft May 18, 2026 12:55
@EmanFateen EmanFateen marked this pull request as ready for review May 19, 2026 09:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[validator] Add built-in unique comparison validator constraint

1 participant