Coder Social home page Coder Social logo

Comments (3)

ahopkins avatar ahopkins commented on June 22, 2024 1

So, what makes this one a little different is the Optional. If you left that part off it should work. A fix is coming shortly though for the optional.

from sanic-ext.

miss85246 avatar miss85246 commented on June 22, 2024

@weiztech Hi, there.
In GET method, One parameter can have multiple values, like this:

http://localhost:3000/?q=1&q=2&q=3

Most of the time, you don't need to use this feature. one parameter can have only one value.

So, sanic do some process in RequestParameters object.

def get(self, name: str, default: Optional[Any] = None) -> Optional[Any]:
    """Return the first value, either the default or actual"""
    return super().get(name, [default])[0]


def getlist(self, name: str, default: Optional[Any] = None) -> Optional[Any]:
    """
    Return the entire list
    """
    return super().get(name, default)
print(request.args.get("q"))
# 1

print(request.args.getlist("q"))
# [1, 2, 3]

Of course, all of these are based on the RequestParameters operations.

If you print request.args, you will see:

print(request.args)
# {'q': ['1', '2']}
# {'q': ['1']}

It's a dict whose value is a list.

And In sanic-ext validator, the do_validation function use request.args as the source of data. You got the validation Error is merited.

Now, you can use dataclass like this to solve this problem temporarily.

@dataclass
class SearchParams:
    q: Optional[List[str]] = None

I hope this will help you.

from sanic-ext.

weiztech avatar weiztech commented on June 22, 2024

@miss85246 ,

Thanks for let me know the temporary fix. it's really help me 😊😊
Hope the issue will be fixed in future

from sanic-ext.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.