Feature request: CoerceToNonGeneric for TOut types
See original GitHub issueSystem Details
- FluentValidation version: 8.4.2
Issue Description
I’m writing some (generic) custom validators, and I was inspired by the way you wrote the LengthValidator
. However, I was wondering, why you have multiple CoerceToNonGeneric
extensions but not just one like the one below:
public static Func<object, TOut> CoerceToNonGeneric<TIn, TOut>(this Func<TIn, TOut> func)
{
return x => func((TIn)x);
}
Less code, and supports all flavors you could wish for out of the box. Or am I missing something?
(I can create a pull request if you like)
Issue Analytics
- State:
- Created 4 years ago
- Comments:8 (4 by maintainers)
Top Results From Across the Web
What Can Coerce, and Where, in Rust
Rust supports a number of type coercions, which implicitly convert one type to another. As in any language with coercion, there is a ......
Read more >Documentation - Narrowing
The “true” branch narrows x 's types which have either an optional or required property value , and the “false” branch narrows to...
Read more >colinhacks/zod: TypeScript-first schema validation ...
With Zod, you declare a validator once and Zod will automatically infer the static TypeScript type. It's easy to compose simpler types into...
Read more >Treating Smart Pointers Like Regular References with the ...
We'll explore how implementing the Deref trait makes it possible for smart pointers to work in ways similar to references. Then we'll look...
Read more >2515-type_alias_impl_trait - The Rust RFC Book
impl Trait type aliases may contain generic parameters just like any other type alias. The type alias must contain the same type parameters...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Guidelines for extending FV are covered here: https://fluentvalidation.net/custom-validators this shows the several different ways you can implement custom validation. Is there anything else you think that should cover?
You definitely shouldn’t do that. A validator stores all of its configuration, so for example if you call
WithMessage
on this validator, it will affect theNoIPBasedEmailAddressValidator
across all validator, which you definitely don’t want (and also isn’t thread-safe). You should instantiate it each time.I think this is already pretty clear in the docs. Under the section on property validator classes section it says “This is an advanced technique that is usually unnecessary.” Basically: property validator classes are how FluentValidation itself is implemented. Typically using this lower-level API would be unnecessary for consumers, and breaks you out of the “fluent” world, but the option is there. I don’t feel that offering advice on when to use one approach over the other is necessarily something I want to do - I present both options in the docs and it’s really up to you as the developer to decide which approach you prefer.
Member is a more generic term that covers both fields and properties. You can of course use FluentValidation against both (as well as even method calls if you really want to, but that’s not a great idea as it bypasses all caching), but in the .NET world generally fields are not exposed publicly, but are encapsulated behind properties. Because FluentValidation is designed to validate the public surface of an object, it makes more sense to refer to properties (as this is the intended use case) as this is what users are more likely to be exposing publicly.
Hope that makes sense