-
Notifications
You must be signed in to change notification settings - Fork 799
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
Support validating parameter properties #350
Comments
+1 Really useful feature!!! |
You can use the definite assignment assertion ( class User {
@IsEmail()
email!: string;
} The above code will compile successfully with |
that's cool, but in my case I actually need the constructor for other places where class-validator is not used. This forces me to add an extra line for every property to get that constructor. |
I don't understand what do you mean.
Your example needs extra line, in mine the constructor can be omitted as it's empty. |
I mean that I have to include the extra line in cases where I need a constructor. I need a constructor when initializing my object in unit tests, for example. edit: to clarify further: class User {
@IsEmail()
email!: string;
} Now say that I really, really need a constructor. I'd modify your example like this class User {
@IsEmail()
email!: string;
constructor(email) {
this.email = email;
}
} But wouldn't it be nice to make this shorter? class User {
constructor(
@IsEmail
public email: string
) { }
} |
It's an entirely different scenario to validate constructor properties during class creation than validating existing classes. This library won't support that format because we have |
I'm perfectly happy validating existing classes, there's nothing that needs to happen during class creation. I just want to put the decorators from The normal decorators |
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
When using the
--strictPropertyInitialization
flag, I often assign properties in a classes constructor:To avoid repetition, I like to use parameter properties:
however, class-validator doesn't seem to support annotating these parameters. I would like to use them like this:
The text was updated successfully, but these errors were encountered: