Using FluentValidation to validate on a collection

2019-08-18 22:37发布

Im trying to see if there is a way to using FluentValidation without explicitly creating a validation model for every object type in my application.

Is this technically and currently possible?

Updated

To rephrase this, is it possible for FluentValidation to validate Rules WITHOUT a IValidator context? Instead, i would like to pass in the item instance to be validated and use validation Rules which are constructed on-the-fly.

Resolved

I was able to resolve by doing a kind of a hack solution. Here are basic details of what i did:

1) I created a class (GenericModel) which has a collection of objects representing properties in a model. 2) I created a validator class that inherits from AbstractValidator. 3) Based on GenericModel's collection of "property objects" + additional metadata about each property's validation rules and error messages, i was able to add FluentValues Rules, all at run-time. 4). At the EditForm i handled the OnSubmit event. In that event handler i execute validation via FluentValidation's ValidateAsync() method. 5). Finally, i iterate thru the validation results and update each field with appropriate CSS, error messages, highlighting...etc.

1条回答
劳资没心,怎么记你
2楼-- · 2019-08-18 23:12

I was able to resolve by doing a kind of a hack solution. Here are basic details of what i did:

  1. I created a class (GenericModel) which has a collection of objects representing properties in a model.

  2. I created a validator class that inherits from AbstractValidator.

  3. Based on GenericModel's collection of "property objects" + additional metadata about each property's validation rules and error messages, i was able to add FluentValues Rules, all at run-time.

  4. At the EditForm i handled the OnSubmit event. In that event handler i execute validation via FluentValidation's ValidateAsync() method.

  5. Finally, i iterate thru the validation results and update each field with appropriate CSS, error messages, highlighting...etc.

查看更多
登录 后发表回答