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.

Option to throw error on extra arguments

See original GitHub issue

When a request has more arguments than are specified in the parser it silently ignores them. I see a related discussion in #28.

Personally I don’t think silently ignoring is the correct thing to do, but changing the default behavior would be backwards incompatible. How about parse takes a strict option that throws an error in cases like this?

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
tuukkamustonencommented, Apr 12, 2016

@sloria I sketched something from your pointers. It’s possibly close to what @ParthGandhi has been doing:

class NoExtrasSchema(StrictSchema):

    class Meta:
        strict = True

    @pre_load
    def validate_extra(self, in_data):
        if not isinstance(in_data, dict):
            return

        extra_args = []
        for key in in_data.keys():
            if key not in self.fields:
                extra_args.append(key)

        if extra_args:
            raise ValidationError('Extra arguments passed: {}'.format(extra_args))

class StrictFlaskParser(FlaskParser):

    def _parse_request(self, schema, req, locations):
        parsed = super(StrictFlaskParser, self)._parse_request(schema, req, locations)

        # Add remaining fields
        for location in locations:
            if location == 'json':
                force = is_json_request(req)
                json_ = req.get_json(force=force, silent=True)
                if not isinstance(json_, dict):
                    continue
                source = json_
            elif location == 'querystring' or location == 'query':
                source = req.args
            elif location == 'headers':
                source = req.headers
            elif location == 'cookies':
                source = req.cookies
            elif location == 'form':
                source = req.form
            elif location == 'files':
                source = req.files

            for k, v in six.iteritems(source):
                parsed[k] = parsed.get(k, v)

        return parsed

The implementation is bound to Flask now, and I do see the problem of making it cross-framework compatible. I only tested JSON location yet, so not sure if the others really work.

Also, this “no extras” feature is probably only wanted for JSON payloads, forms, files and maybe query parameters. It doesn’t really make sense for cookies and headers, imo.

In any case, I would expect support for this to exist in the library out of the box. Do you see any reasonable possibilities to maybe generalize the implementation (like adding more parse_ methods for returning the collections or missing keys)?

0reactions
tuukkamustonencommented, Apr 11, 2016

@ParthGandhi I would be curious to see your solution as well. It seems the flexibility in supporting multiple/varying locations per argument / set of arguments makes this one problematic…

Read more comments on GitHub >

github_iconTop Results From Across the Web

Raise custom Exception with arguments - Stack Overflow
In case A, raise will call FooError.new('argh') , but your code expects an optional foo , not a message. This is bad. What...
Read more >
Should I raise an exception/error when an optional argument ...
If an argument is mandatory, then you should raise an exception when it's not passed. However, if it's optional, then you should either...
Read more >
TypeError: More arguments needed - JavaScript | MDN
The JavaScript exception "more arguments needed" occurs when there is an error with how a function is called. More arguments need to be ......
Read more >
How to pass argument to an Exception in Python?
Using arguments for Exceptions in Python is useful for the following reasons: It can be used to gain additional information about the error ......
Read more >
Best Practices for exceptions - .NET - Microsoft Learn
All exceptions derive from the Exception class. More derived exceptions aren't handled by a catch clause that's preceded by a catch clause for...
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