question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Feature request: CoerceToNonGeneric for TOut types

See original GitHub issue

System 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:closed
  • Created 4 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
JeremySkinnercommented, Jul 12, 2019

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?

0reactions
JeremySkinnercommented, Jul 13, 2019

Is reusing the same instance tricky here?

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 the NoIPBasedEmailAddressValidator across all validator, which you definitely don’t want (and also isn’t thread-safe). You should instantiate it each time.

I think generic documentation on when to implement a custom validator, and when to just extend on existing ones in an extension method make more sense would help developers a lot.

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.

My mistake. I meant Properties only. And of course (if you use expressions on fields, they work to, I noticed), but in your naming you went for “Property”, not for “Member”.

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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found