Skip to content

chore(validator): use generic template for ConstraintOptions across validator constraint option types#174

Merged
imdhemy merged 7 commits into
koala-ts:2.xfrom
EmanFateen:reuse-constraintOptions-accross-options-types
May 14, 2026
Merged

chore(validator): use generic template for ConstraintOptions across validator constraint option types#174
imdhemy merged 7 commits into
koala-ts:2.xfrom
EmanFateen:reuse-constraintOptions-accross-options-types

Conversation

@EmanFateen

@EmanFateen EmanFateen commented May 13, 2026

Copy link
Copy Markdown
Contributor
Q A
License GPLv3
Issue Closes #168

Overview

This PR introduce a generic template type to be used in the ConstraintContext for ConstraintOptions.

Changes made

  1. Generalised ConstraintOptions in ConstraintContext
export type ConstraintContext<TOptions extends ConstraintOptions = ConstraintOptions> = {
  path: string;
  root: unknown;
  value: unknown;
  constraint: string;
  options: TOptions;
  runNestedRules: (value: unknown, schema: FieldSchema, path: string) => Violation[];
};
  1. It also affected the ConstraintValidator to be
export type ConstraintValidator<TOptions extends ConstraintOptions = ConstraintOptions> = (
  value: unknown,
  context: ConstraintContext<TOptions>,
) => Violation[];
  1. Used the related options type for each constraint validator
export function notBlank(value: unknown, context: ConstraintContext<NotBlankOptions>)
export function email(value: unknown, context: ConstraintContext<EmailOptions>) 
export function slug(value: unknown, context: ConstraintContext<SlugOptions>) 

Clarification:

I did not add dedicated type-only test cases because this change is validated by the TypeScript compiler during npm run build.
The runtime behavior is unchanged, and the existing behavior tests continue to cover the validator execution path.

@EmanFateen EmanFateen requested a review from imdhemy as a code owner May 13, 2026 11:20
@EmanFateen EmanFateen marked this pull request as draft May 13, 2026 11:21
@EmanFateen EmanFateen marked this pull request as ready for review May 13, 2026 14:58
@EmanFateen EmanFateen force-pushed the reuse-constraintOptions-accross-options-types branch from 2438aba to cbb3a71 Compare May 14, 2026 16:40
@EmanFateen EmanFateen changed the title chore(validator): reuse ConstraintOptions across validator constraint option types chore(validator): use generic template for ConstraintOptions across validator constraint option types May 14, 2026
@imdhemy imdhemy merged commit ea203a0 into koala-ts:2.x May 14, 2026
1 check passed
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] Narrow ConstraintOptions and reuse it across validator constraint option types

2 participants