-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Easier identification/exposition of When conditions #2114
Comments
Hi, thanks for the suggestion. The first thing I'd say is FluentValidation is specifically not intended to be a tool for generating client-side validation rules and this is something I'd consider to be very much out of scope. The type of constraints needed between server-side and client-side are generally different enough that it's better to maintain clientside rules separately from serverside as trying to use a single source for both will result in compromise and limitations on what you can use on both ends, so my strong recommendation would not be to go down this route (although I can't stop you from doing so if you really want to). Although clientside validation and serverside validation are similar, they do actually have separate goals & constraints and I think it's a mistake to try and generate the two from a single source unless it's for the simplest of use cases. With that out of the way I'll try and address the specific suggestions:
Changing the condition to an expression is something we absolutely don't want to do. Expression compilation is expensive and should be avoided unless absolutely necessary, and even with caching it's not ideal. I've done a lot of work on FluentValidation's performance over the years and handling of expressions are 100% the biggest issue - this would become significantly worse if conditions were changed to be expressions too, so I'm afraid this isn't a change that I'm willing to make.
I'm much more open to this, although I'm very wary of opening up internal methods of the library as I've been bitten before by having too much public, which then drastically increases the maintenance burden. I'll have a think about this one though. However I would still strongly encourage you not to go down this route, it leads to more headaches than it solves in the long run. |
Hi @JeremySkinner, thanks for the agreeably quick response. I get not wanting to introduce Expressions, I figured performance was going to be an issue, it's not for us in this context but hey I had to try =P Regarding the scope the FV, that's very fair too of course, although in our particular case the exporting of FV schemas to Yup schemas stemmed from a willingness to avoid duplicated code and to introduce more client side validation at basically reduced cost, simply put the exporter can't export everything, by being a bit careful it will only export what's common between sever and client validation (meaning no validation against the DbContext) and Yup schemas are easily customizable/overridable How about adding something like this on AbstractValidator ? :
No need to make public anything that isn't already, won't solve all of my problems but it should take care of the When(condition, action) case |
Butter fingers on my part, reopening the ticket |
11.8 is released with this change |
Is your feature request related to a problem? Please describe.
To give some background, we are working on an exporter à la Typegen to export FV schemas to Yup schemas, works a treat on basics schemas but I'm facing issues with when conditional rules are used.
The two problems I have are:
When
usesFunc<T, bool>
for the predicate, which makes it basically impossible to translate to javascriptIRuleComponent
only exposesHasCondition
which doesn't allow identification of the conditionThe func issue I can work around by making a custom abstract validation that's exposes a
When
with anExpression
and caching the expression. That customWhen
is called over the one with theFunc
parameter so it's transparent for the rest of the teamFor the second issue I've had to resort to reflection shenanigans by getting the internal
Rules
on the validator and invokingOnItemAdded
on it, in my customWhen
, and caching the added rules with theExpression
, lots of fun but not very sustainableDescribe the solution you'd like
Not entirely sure to be honest
Func
toExpression<Func
and exposing the condition inIRuleComponent
, would be the 'logical' solution but I realize this would be huge undertaking mostly because the condition on the RuleComponents is not the Func that was passed originallyOnItemAdded
(OnRuleAdded perhaps ?) onAbstractValidator
would help a ton, don't know what use it would be beside helping in my particular case thoughWhen
toExpression
compiling it to the use the existing internal logic but passing the originalExpression
down to theRuleComponents
and exposing itDescribe alternatives you've considered
No response
Additional Context
No response
The text was updated successfully, but these errors were encountered: