Skip to content
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

Expression Guard #77

Closed
ardalis opened this issue Jul 23, 2020 · 9 comments
Closed

Expression Guard #77

ardalis opened this issue Jul 23, 2020 · 9 comments

Comments

@ardalis
Copy link
Owner

ardalis commented Jul 23, 2020

An Expression-based Guard could be used to provide flexibility and could cover any case not already taken care of by the library, without the need to write your own guard clause method.

public static T AgainstExpression<T>([JetBrainsNotNull] this IGuardClause<T> guardClause, [JetBrainsNotNull] Func<T, bool> func, [JetBrainsNotNull] string message) where T : struct
{
    if (func(guardClause.Value))
    {
        throw new ArgumentException(message);
    }
    return T;
}
@fiseni
Copy link
Contributor

fiseni commented Jul 23, 2020

This is nice. Any reason you want to limit this only for "struct"?

@dsbert
Copy link

dsbert commented Dec 1, 2020

Be aware that this can lead to performance issues if abused.

safakgur/guard#13

@ardalis
Copy link
Owner Author

ardalis commented Dec 4, 2020

Let's implement this and see if there are any issues.

@mohaanwrites
Copy link
Contributor

mohaanwrites commented Jul 5, 2021

If someone wants to deal with string properties with custom validations, this will not allow. Otherwise, I've added the required tests. Let me know. @ardalis

@wgnf
Copy link

wgnf commented Nov 4, 2021

@ardalis do you still want this to be implemented? if so, i'd love to have a go at this :)

@ardalis
Copy link
Owner Author

ardalis commented Nov 4, 2021

yes, if someone wants to implement support for it, I'd look at the PR. Just reference this issue number in the description. Thanks, @wgnf

@wgnf
Copy link

wgnf commented Nov 4, 2021

Just scrolling through code to see how things are done i stumbled upon this one:

https://github.com/ardalis/GuardClauses/blob/main/src/GuardClauses/GuardClauseExtensions.cs#L581-L589

Guard.Against.InvalidInput seems to already be the "Expression Guard" that you want to implement in this issue 🤔

@wgnf
Copy link

wgnf commented Nov 4, 2021

The only thing that is different here is that you mention a IGuardClause<T> with a Value in that (new) interface, which I'm unable to find.

Or is that the new feature you wanted to implement and I just didn't get the hint? :D @ardalis

@ardalis
Copy link
Owner Author

ardalis commented Nov 4, 2021

Hmm... yeah, that's fair. I think maybe I missed it due to its naming. Ok, let's just say this is done, then. 😃

@ardalis ardalis closed this as completed Nov 4, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants