You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I was trying to learn a bit more about Fluent Validation and was playing with some concepts of practical rules that could be used in a request.
I'm going to use the Product/Brand examples in the code to explain what I was trying to do first, granted they are not great "practical" examples but here goes:
Let's just say that I want to set a rule that says "if brand is Armani, then check that the rate is at greater than 100", I know funny rule and not very practical, but its an example.
First I looked at SetInheritance from Fluent but quickly realized that would only apply if I was creating the brand simultaneously and it would only be validation rules set at the brand that only applied to the brand.
Ok so now what? Then I thought of exposing the T delegate by adding a property as follows:
private T _request = default(T);
public T Request
{
get { return _request; }
}
This works because i can write an if statement and then add the rule, to the rate field, simular to this:
The problem is that all I have in the CreateProductRequest is the BrandId.
This got me thinking, how would we do validations like this? Not sure if Fluent Validator has anything else that I'm missing (very possible).
Other than that I have conceptualized two options, just not sure which one would be ideal:
a) Expose the Brand object in the request and build data loading in the service or the validator.
b) Just create a local Brand variable do a call to the brand service and get by Id and do it that way.
c) am I making this way too complicated and there is an obvious simple solution to do this type of validation?
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Hi Mukesh,
I was trying to learn a bit more about Fluent Validation and was playing with some concepts of practical rules that could be used in a request.
I'm going to use the Product/Brand examples in the code to explain what I was trying to do first, granted they are not great "practical" examples but here goes:
Let's just say that I want to set a rule that says "if brand is Armani, then check that the rate is at greater than 100", I know funny rule and not very practical, but its an example.
First I looked at SetInheritance from Fluent but quickly realized that would only apply if I was creating the brand simultaneously and it would only be validation rules set at the brand that only applied to the brand.
Ok so now what? Then I thought of exposing the T delegate by adding a property as follows:
This works because i can write an if statement and then add the rule, to the rate field, simular to this:
The problem is that all I have in the CreateProductRequest is the BrandId.
This got me thinking, how would we do validations like this? Not sure if Fluent Validator has anything else that I'm missing (very possible).
Other than that I have conceptualized two options, just not sure which one would be ideal:
a) Expose the Brand object in the request and build data loading in the service or the validator.
b) Just create a local Brand variable do a call to the brand service and get by Id and do it that way.
c) am I making this way too complicated and there is an obvious simple solution to do this type of validation?
Thank you,
Jeff
Beta Was this translation helpful? Give feedback.
All reactions