class-validator icon indicating copy to clipboard operation
class-validator copied to clipboard

fix: overwrite validation decorators from parent class when defined in child

Open NickKelly1 opened this issue 4 years ago • 14 comments

Description

Fixes a bug that caused decorated properties which are also decorated in inherited classes, to skip their inherited validation.

Checklist

  • [x] the pull request title describes what this PR does (not a vague title like Update index.md)
  • [x] the pull request targets the default branch of the repository (develop)
  • [x] the code follows the established code style of the repository
    • npm run prettier:check passes
    • npm run lint:check passes
  • [x] tests are added for the changes I made (if any source code was modified)
  • [x] documentation added or updated
  • [x] I have run the project locally and verified that there are no errors

Fixes

fixes #633 , fixes #622

NickKelly1 avatar Sep 07 '20 22:09 NickKelly1

Please don't merge this yet, I've added some additional tests and found some issues. I Will comment again once resolved.

NickKelly1 avatar Sep 07 '20 23:09 NickKelly1

This PR is now ready for merge

Please note:

Currently in v0.12.2, validator-decorated properties of subclasses remove validators registered against those properties from parent classes (see 633). It appears this has been a problem for a long time. Validator equality is done by comparing a validators type, which in v0.12.2 is "customValidation" for almost every single validator.

The bug became apparent after v0.11.1 (https://github.com/typestack/class-validator/commit/11a7b8bb59c83d55bc723ebb236fdca912f49d88#diff-6a5d045641599b923bd58ec30032f09dL1) wherein most validators had a unique ValidationType allowing subclassing to override them (note that custom validators still had type "customValidation", and thus would all override each-other by subclassing).

This PR changes equality to be based on name, rather than type, and falling back to type if name is not given. This makes subclassing to work closer to as described in the documentation. If name is now used to uniquely identify a validator, that implies it should be a required property. To not break backwards compatibility, it's left optional here with TODO's to add it where necessary if it becomes required later.

NickKelly1 avatar Sep 08 '20 01:09 NickKelly1

Any update on this? I got hit by it again today. It's quite confusing at the moment as some decorators are inherited (e.g. @IsOptional) and others aren't.

cduff avatar Feb 09 '21 08:02 cduff

Hey folks, any expectations on merging this change? It seems to me the semantic commit + PR is blocking it. Thanks!

rafaelbattesti avatar Apr 01 '21 16:04 rafaelbattesti

Please merge this fix, more than one is forced to use version 0.11.1. Thanks!

@NoNameProvided

FacundoVazquez avatar Feb 11 '22 17:02 FacundoVazquez

Hi! I pinned the tab, I will take a look next week.

NoNameProvided avatar Feb 12 '22 21:02 NoNameProvided

Any update on the merge?

StefanSafeguard avatar Mar 04 '22 18:03 StefanSafeguard

Looking forward to this to be merged as well. In the meantime, I worked around the problem using ValidationTypes.isValid = () => true;, but it broke validation (everything is valid now). I'd like to use the type for a project that seems to depend on this PR completely.

Clashsoft avatar Jun 05 '22 19:06 Clashsoft

@NickKelly1 Any chance you can have a look at the conflicts, please? Dying to get this merged and released. Please.

slavco86 avatar Jul 25 '22 20:07 slavco86

@NoNameProvided , @NickKelly1 , did you decide to do not merge it?

pahuta avatar May 23 '23 07:05 pahuta

👋🏻 Hi there, just wondering if this fix will land or if y'all have decided not to support this?

kaseyreed avatar Sep 05 '23 21:09 kaseyreed

This should really be merged... this is obviously a gap/bug, which anyone requiring this functionality would expect to be working? I mean this is a recursiveness problem, which should not be present in a production grade package. Quite surprised it's still not merged/fixed 😢

slavco86 avatar Sep 05 '23 22:09 slavco86

Any updates on this PR? Would it merge soon?

AdirTrichter avatar Apr 03 '24 08:04 AdirTrichter

Does anyone know a workaround that can be done so it would inherit the validations from the parent till this PR merges? *other than rewriting the validations from the inherited class ofc 🙏

AdirTrichter avatar Apr 04 '24 12:04 AdirTrichter