Using FluentValidation to validate on a collection of Fields (rather than on a validator model)

流过昼夜 提交于 2019-12-13 02:53:55

问题


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:


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.



来源:https://stackoverflow.com/questions/57151623/using-fluentvalidation-to-validate-on-a-collection-of-fields-rather-than-on-a-v

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!