Coder Social home page Coder Social logo

Comments (3)

sirosen avatar sirosen commented on June 5, 2024 1

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.

simkimsia avatar simkimsia commented on June 5, 2024

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.

simkimsia avatar simkimsia commented on June 5, 2024

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)

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.