- 
                Notifications
    You must be signed in to change notification settings 
- Fork 838
          feat: add @InheritValidation decorator to copy validation metadata
          #161
        
          New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
  
    feat: add @InheritValidation decorator to copy validation metadata
  
  #161
              Conversation
| Regarding the Travis CI failure:  | 
| @NoNameProvided Hi! Could you give me your opinion about this decorator/idea when you get a chance? Thanks! (I can remove the Jest Tests and rewrite them for Mocha, so the Travis CI will succeed, if you agree with the feature). | 
| The idea is great thanks for the contribution! 🎉 I have one question: const toPropertyName: string = toProperty instanceof Symbol ?
      typeof toProperty :
      toProperty;This way every  Otherwise, it looks good for me for the first glance, so it will be good to go as we can fix these: 
 | 
| Hi @NoNameProvided | 
9640a37    to
    7d5d199      
    Compare
  
    | @VinceOPS What is status of this PR? Are you still interested? | 
| This feature is awesome! Nice to have! | 
| Related: tannerntannern/ts-mixer#15 | 
| Tips:  | 
| could we make the decorator type-safe so that we don't accidentially have typos in the property name? e.g. maybe something like this: function InheritValidation<T>(fromClass: ClassType<T>, fromProperty?: keyof T) | 
| This PR no longer works in v0.12+ | 
| Hi @VinceOPS Thanks for this contribution and sorry for the delayed response. Are still interested in this PR? If so can you rebase these changes to the current develop branch? We have 
 After rebase I will do a review in a timely manner. | 
@InheritValidation decorator to copy validation metadata
      | This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. | 
Hi,
Using nestjs recently, we've been through the frustration of duplicating a lot of validation decorators in our DTO's as we're adding more endpoints for our users, in order to do "partial updates" (and other operations requiring partial DTO's).
Explanations (using fake codes, for the sake of the example):
Let's assume this is our complete User DTO.
At some point, we'll propose our users to update, for instance, their password. We can either create a DTO with all these fields set as Optional, and copy/paste all other validation rules. Or we can create a new DTO with only the fields we expect to be used by the client app, but we still need to copy/paste all other validation rules. And for our administrators (using the backoffice), we'll have to create different DTO's for different fields, again.
By the way, we enjoy using specific classes as return-types for our Controllers, and it helps a lot using Nestjs's swagger module.
The issue concerns the duplication of the decorators and the maintainance of all the rules.
This PR intends to fix it using a new decorator used to copy the
ValidationMetadataof a given property.Following our previous example, we would simply do:
But it could also be used to "share" validation metadatas between properties of the same class (let's say, "firstName" and "lastName").
I also shared my unit tests while I was at it. However, I'm using Jest (and ts-jest) in most of my projects, so this won't work with Mocha. If you like the idea and want to accept it, I'll rewrite my unit tests to work with Mocha.
(In the mean time, you can check the unit tets work (they do :-) ) by running jest against the file inherit-validation.spec;ts).
Thanks for all your work!
Regards