Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7833845
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T12:55:41+00:00 2026-06-02T12:55:41+00:00

I want to check a certain argument value against a regex-pattern and only continue

  • 0

I want to check a certain argument value against a regex-pattern and only continue if they match. This happens in many places within my app, so I decided to let a function do the checking and call that function whenever I need it. Now, in most cases, I need that check to be performed right at the beginning of a view, so I created it as a decorator like so:

def validate(f):
    def _inner(request, argument=None):
        if argument is None:
            return HttpResponse(content="No argument given", status=400)
        elif not re.match('^SOME_REGEX$', argument):
            return HttpResponse(content="Invalid argument", status=400)
        else:
            return f(request, argument)
    return _inner

But there are other cases where I need to call that checker from within a function, as part of nested conditions. It seems I can’t call it directly, e.g. validate(argument). Is there any way I can use the same code as a decorator as well as a normal function? Or do I have to type it twice?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T12:55:45+00:00Added an answer on June 2, 2026 at 12:55 pm

    You certainly don’t have to type it twice, you can just create a validate function which takes a value and validates it:

    def validate(argument):
        return re.match('^SOME_REGEX$', argument)
    

    and then write a decorator which calls the validate function as needed:

    def requires_valid(f):
        def _inner(request, argument=None):
            if argument is None:
                return HttpResponse(content="No argument given", status=400)
            elif not validate(argument):
                return HttpResponse(content="Invalid argument", status=400)
            else:
                return f(request, argument)
        return _inner
    

    Obviously, I don’t know your use case so you might want to move the check for None into validate but the point is, you don’t have to repeat the same regex twice.

    And if you feel like delving into deeper magic and insist on using the same function both as a decorator and a verifier, you might try something like this:

    def validate(f):
        if callable(f):
            def _inner(request, argument=None):
                if argument is None:
                    return HttpResponse(content="No argument given", status=400)
                elif not validate(argument):
                    return HttpResponse(content="Invalid argument", status=400)
                else:
                    return f(request, argument)
            return _inner
        else:
            return re.match('^SOME_REGEX$', f)
    

    But I’d advise against this, since you have one function that does two very different things, depending on the type of the parameter. This results in code that is much more difficult to understand. (“You decorate a view with this function which takes a string and returns bool?!”)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to check if a certain key/value pair has been set. How do
I want to check if a certain pattern (eg. a double quoted string) matches
i want to check for certain regex expression to make sure some string is
I want to check users' subscribed dates for certain period. And send mail to
I don't want check if an item with a value exists in the arraylist,
I want to check in a script if a certain other module is already
I want to check that all links on a page contain a certain element.
I want to check a range of cells for a certain piece of text.
I want to check if the users on our domain have a certain Firefox
I basically want to check if part of a string begins with a certain

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.