feat(validator): Add type validator constraint#173
Conversation
# Conflicts: # src/validator/constraints/index.ts
20516cd to
7da7d2c
Compare
There was a problem hiding this comment.
This PR introduces regression to the validator component’s structure. We’ve already improved the structure in #175. Could you kindly modify the changes here to align with the updated structure? @EmanFateen
I was reviewing an older version of the PR.
imdhemy
left a comment
There was a problem hiding this comment.
@EmanFateen
Thanks for moving this forward. The implementation is good and covers the issue, we just need to tighten the types and simplify the implementation.
| }; | ||
|
|
||
| export function type(value: unknown, context: ConstraintContext<TypeOptions>): Violation[] { | ||
| if (value === undefined) { |
There was a problem hiding this comment.
This case is already covered in the test cases, but it’s better to have a separate test case for it, similar to how we did for the email constraint. This approach clearly communicates the intended behavior.
There was a problem hiding this comment.
A single test case with a clear intention is sufficient. For example, “it should bypass undefined value.”
You’ve added two test cases with different descriptions of behavior.
There was a problem hiding this comment.
I kept returns no violations for undefined value and removed the redundant one.
I see returns no violations for undefined value more clear than “it should bypass undefined value.”
Done
| } | ||
|
|
||
| function getDefaultMessageWith(types: AllowedTypes[]): string { | ||
| if (Array.isArray(types) && types.length > 1) { |
There was a problem hiding this comment.
Since you’ve already declared types as an array, there’s no need to perform the check.
| }; | ||
|
|
||
| export function type(value: unknown, context: ConstraintContext<TypeOptions>): Violation[] { | ||
| if (value === undefined) { |
There was a problem hiding this comment.
A single test case with a clear intention is sufficient. For example, “it should bypass undefined value.”
You’ve added two test cases with different descriptions of behavior.
imdhemy
left a comment
There was a problem hiding this comment.
@EmanFateen
Thank you!
I made ConstraintOptions generic so generic schema code can stay flexible, while specific constraints can keep precise option shapes. The old index signature leaked into every constraint-specific options type; now only the default generic form is loose, and ConstraintOptions<{ ... }> keeps custom constraint options strict while still sharing groups.
* 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>
* 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>
Overview
In this PR, I have introduced new validation constraint called type.
It's exposed through the
builtInConstraintsand can be used likenullasnullis a real value.Changes made:
typeconstraint under basic constraint.type.testfile.typeconstraint through thebuiltInConstraintsobject.