Skip to content

fix: preserve distinct inherited validation constraints - #2695

Open
KAPUIST wants to merge 1 commit into
typestack:developfrom
KAPUIST:fix/inherited-validation-constraints
Open

KAPUIST wants to merge 1 commit into
typestack:developfrom
KAPUIST:fix/inherited-validation-constraints

Conversation

@KAPUIST

@KAPUIST KAPUIST commented Sep 18, 2026

Copy link
Copy Markdown

When a subclass adds a different validator to an inherited property, the parent's validators are incorrectly discarded because they share the customValidation metadata type. For example, adding @IsNotEmpty() in a subclass can silently remove the parent's @MinLength() check.

Compare custom validation metadata by validator name, falling back to the constraint class for unnamed @Validate() metadata. This preserves distinct inherited validators while retaining child overrides of the same validator and the existing handling of other metadata types.

Fixes #2609.

Validation:

  • Added five regression tests covering distinct validators across inheritance levels, simultaneous parent/child errors, same-validator overrides, custom constraint classes, and validation groups.
  • Three regression tests failed before the fix; all 811 tests across 15 suites pass after the fix.
  • CommonJS build, source lint, changed-file formatting, and git diff --check pass.
  • A separate runtime check with NestJS PartialType confirms inherited constraints are retained and optional fields remain optional.

This change addresses inherited constraint loss; it does not change PartialType optionality or deduplication between multiple ancestor classes.

KAPUIST commented Sep 26, 2026

Copy link
Copy Markdown
Author

Context on prior work: #748 and #2641 address the same underlying inheritance/deduplication bug and are still unmerged. #2641 is essentially an updated/rebased version of #748.

This PR takes a narrower approach: it changes deduplication only for customValidation metadata in MetadataStorage, using validator name (with constraintCls as a fallback for unnamed @Validate() constraints), while leaving the handling of other metadata types unchanged. I opened it separately because #2609 is still reproducible on current develop, and this smaller change adds focused regression coverage for the current behavior.

If maintainers would prefer this work to be folded into #2641 instead, I’m happy to adjust.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

fix: Inheritance and overriding fields

1 participant