For example I have class:
class ExampleModel extends Model {
@MinLength(10)
@IsAlphanumeric()
public password: string;
@IsEmail()
@IsDefined()
public email: string;
}
Can I validate only email or password, without validating other fields?
I'd also like to be able to do this; to reuse the validation for each field in a web form.
It should work just the way you wrote it @Horat1us. class-validator checks only know properties of an object and will leave the others untouched.
@NoNameProvided I want to validate only one attribute, when other filled.
For example I have object:
let model: Model = {
a: 'someValidValue',
b: 'invalidValue',
}
In this case I want to validate only a property, without validating b, because it may be slow and use HTTP request for validation.
Hi @Horat1us ,
Can you explain a little bit more your use case? Why is not suitable group validation for this case?
````
class MyDto {
@Length(10, {
groups: [ 'my-custom-case-1', 'my-custom-case-2' ]
})
a: string,
@Length(20, {
groups: [ 'my-custom-case-1' ]
})
b: string
}
````
In your case, you only need to validate group 'my-custom-case-2' against incoming dto.
I hope it helps
Can you please elaborate on your use-case a little more? If you dont want to validate it, just do not decorate it:
class ExampleModel extends Model {
@MinLength(10)
@IsAlphanumeric()
public password: string;
public email: string;
}
In this example, email will pop-up in intellisense but won't be validated.
@ericzon Now I use groups for this case. But if I have 20 attributes it really redundant to create unique group for each attribute.
@NoNameProvided I want to validate email, but another time. Groups is redundant in case 1 group = 1 attribute
@NoNameProvided I have a similar problem. From my UI, I need to call validate on blur and in this case I'd only want to validate the field I just blurred from. And blur from next field I'd want to validate that field only and so on. If only we could pass in property name or better yet the path to property (to handle nested objects) in ValidatorOptions, it would be possible to avoid unnecessary extra validations. As @Horat1us suggested, currently a way around to achieve this is to use groups but having 1 group per field just so that we can target it only is a lot of code bloat.
@ajitgauli do you have suggestions how we can implement it?
I would really like this feature as well. Rather than passing in the instance of my class, I just want to be able to validate a SINGLE property on that class when I do an onBlur for instance as @ajitgauli as stated above. thanks.
Any update on this?
Most helpful comment
@NoNameProvided I have a similar problem. From my UI, I need to call validate on blur and in this case I'd only want to validate the field I just blurred from. And blur from next field I'd want to validate that field only and so on. If only we could pass in property name or better yet the path to property (to handle nested objects) in ValidatorOptions, it would be possible to avoid unnecessary extra validations. As @Horat1us suggested, currently a way around to achieve this is to use groups but having 1 group per field just so that we can target it only is a lot of code bloat.