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

Feature - Add ability to provide RegEx expressions #82

Closed
krptodr opened this issue Nov 24, 2020 · 5 comments
Closed

Feature - Add ability to provide RegEx expressions #82

krptodr opened this issue Nov 24, 2020 · 5 comments

Comments

@krptodr
Copy link

krptodr commented Nov 24, 2020

It would be nice to pass regular expression to the Guard.Against method where the expression is processed. Thinking further on the idea, I'm not sure how I would name these methods to help with readability.

@ardalis
Copy link
Owner

ardalis commented Dec 4, 2020

Guard.Against.StringsMatching(RegEx expression) ?

@MojtabaKiani
Copy link
Contributor

I think it must be some thing like this :

    public static string BadFormat([JetBrainsNotNull] this IGuardClause guardClause, [JetBrainsNotNull] string input, [JetBrainsNotNull] string parameterName, [JetBrainsNotNull] string regexPattern)
    {
        if (input != Regex.Match(input, regexPattern).Value)
            throw new ArgumentException($"Input {parameterName} was not in required format", parameterName);

        return input;
    }

@ardalis
Copy link
Owner

ardalis commented Dec 20, 2020

Want to PR that? Do we like Bad or Invalid format better?

@MojtabaKiani
Copy link
Contributor

yep, InvalidFormat is better, I've sent the PR

@krptodr
Copy link
Author

krptodr commented Jan 4, 2021

Here is the method I implemented. I went with one of @ardalis's early implementation of the a guard clause

public static void ForRegExMatch(string pattern, string value, string parameterName, string message = null)
        {
            if (value == null)
                return;

            if (new Regex(@pattern).IsMatch(value))
                return;

            if (string.IsNullOrEmpty(message))
                throw new ArgumentOutOfRangeException(parameterName);
            throw new ArgumentOutOfRangeException(parameterName, message);
        }

@ardalis ardalis mentioned this issue May 14, 2021
@ardalis ardalis closed this as completed May 14, 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

3 participants