Comments (3)
I'm not really clear on what the question is here, with respect to webargs
. There are several stages at which you could transform a list of strings to trim empty values.
A schema post_load
hook might be the most ergonomic. Something like:
import marshmallow as ma
class MyArgSchema(ma.Schema):
x = ma.fields.List(ma.fields.Str(), required=True)
@ma.post_load
def _strip_empty_from_x(self, data, **kwargs):
data["x"] = [s for s in data["x"] if s != ""]
return data
Or there are fancier approaches. You could define a custom field class which does this, if you find you need it in many places.
This might be a question better suited to Stack Overflow than to the webargs issue tracker. Because if I'm understanding the question correctly, the answer is "Yes, marshmallow -- and therefore webargs -- supports that in at least two pretty good ways. 🙂 "
from webargs.
This might be a question better suited to Stack Overflow
Oh i wasn't aware. I apologize. Will close this and replicate in SO.
from webargs.
Just to help those who chance upon this
original is this
from core.webarg_parsers import MyParser
parser = MyParser()
sync_greendeploy_websites_args = {
"keep": fields.List(fields.Str(), required=True),
"new": fields.List(fields.Str(), required=True),
}
@login_required
@require_http_methods(["POST"])
@parser.use_args(sync_greendeploy_websites_args, location="form")
def website_sync_post(request, request_args):
if not request.user.is_authenticated:
return redirect(f'{settings.LOGIN_URL}?next={reverse_lazy("websites-sync")}')
sanitized_args = sanitize_request_args(
request_args, valid_args=sync_greendeploy_websites_args
)
new
class WebsiteSyncSchema(Schema):
keep = fields.List(fields.Str(), required=True)
new = fields.List(fields.Str(), required=True)
@post_load
def _strip_empty_from_lists(self, data, **kwargs):
data["keep"] = [s for s in data["keep"] if s != ""]
data["new"] = [s for s in data["new"] if s != ""]
return data
@login_required
@require_http_methods(["POST"])
@parser.use_args(WebsiteSyncSchema(), location="form")
def cloudflare_website_sync_post(request, request_args):
if not request.user.is_authenticated:
return redirect(f'{settings.LOGIN_URL}?next={reverse_lazy("websites-sync")}')
# unlike using regular dict, there's no need to sanitize the request_args
# there's no need to run .load as well. everything is already sanitized properly
# request_args now will have all the empty strings removed from the list
from webargs.
Related Issues (20)
- [RFC] Revisiting our CI setup: GH Actions, RTD HOT 8
- potential documentation issue in quickstart/error-handling HOT 3
- Parse a schema that contains both file and form data HOT 2
- Tags cleanup HOT 2
- How to use partial loading with nested field HOT 2
- When setting location to `files`, custom field's `_deserialize` and `_validated` methods were not called HOT 7
- aiohttp. body argument is deprecated for http web exceptions HOT 1
- Update minimum supported versions of frameworks in next major release HOT 1
- Release version 8.2.0 HOT 4
- [Question] how to use this with line item like data? HOT 2
- Is there possible add Quixote web framework support HOT 2
- [Question] How to set up the Schema that will validate against a list of allowed enum values? HOT 1
- Dealing with empty values in `DelimitedFieldMixin` HOT 2
- Being able to handle ValidationError in Flask view methods without custom error handler HOT 4
- webargs.core.Parser should describe default for unknown either as marshmallow.EXCLUDE or "exclude" HOT 1
- RFC: support passing the whole parsed arguments as a keyword argument HOT 10
- Please consider using Mapping instead of Dict in ArgsMap to avoid type checker errors HOT 7
- Parse schema without using decorators HOT 1
- How to handle JSON data always nested / wrapped in a "body" object HOT 9
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from webargs.