Coder Social home page Coder Social logo

Comments (5)

sloria avatar sloria commented on August 17, 2024

See comment in #222

Thanks for the PR. I think I'm going to have to pass on this since the use case here is too specific and makes too many assumptions to justify the added API surface.

If you need marshmallow validation for your configuration and you're using environment variables per the 12 factor methodology, I recommend taking a look at environs.

from flask-marshmallow.

mjpieters avatar mjpieters commented on August 17, 2024

I think you misunderstood the use case here.

This is used to expose configuration stored in the standard Flask config to other consumers such as an SPA.

In the specific case were I created this field implementation, the front-end is rather TypeScript heavy but is configured using JSON embedded in a <script> tag, and we used Marshmallow schemas to define their structure.

The support to set Flask configuration with a FlaskConfig field was only implemented to keep the field symmetrical, is disabled by default and was kept primitive on purpose (hence the warnings in the documentation).

On a meta level: I would have appreciated discussion on this much, much earlier before I had put in the time to create a PR for this. I posted the FR 2 months ago. It would have saved us both some time.

from flask-marshmallow.

sloria avatar sloria commented on August 17, 2024

I understand the use case. I still think it's too specific to justify the added code and API surface. The use case can be met without too much trouble using existing API:

from flask import Flask
from flask_marshmallow import Marshmallow

app = Flask(__name__)
app.config["SOME_ACCESS_TOKEN"] = "public-token"
ma = Marshmallow(app)


class MyConfig(ma.Schema):
    my_field = ma.Str()
    some_access_token = ma.Constant(app.config["SOME_ACCESS_TOKEN"], dump_only=True)


print(MyConfig().dump(dict(my_field="foo")))
# {'some_access_token': 'public-token', 'my_field': 'foo'}

You could easily write a helper to improve the ergonomics a bit:

def app_config_field(config_key, **kwargs):
    kwargs.setdefault("dump_only", True)
    return ma.Constant(app.config[config_key], **kwargs)


class MyConfig(ma.Schema):
    my_field = ma.Str()
    some_access_token = app_config_field("SOME_ACCESS_TOKEN")

I would have appreciated discussion on this much, much earlier before I had put in the time to create a PR for this. I posted the FR 2 months ago. It would have saved us both some time.

That would have been ideal, yes. Unfortunately, I've not been able to spend a lot of time on this lib the past couple months due to personal and professional priorities.

from flask-marshmallow.

mjpieters avatar mjpieters commented on August 17, 2024
class MyConfig(ma.Schema):
    my_field = ma.Str()
    some_access_token = ma.Constant(app.config["SOME_ACCESS_TOKEN"], dump_only=True)

Unfortunately, that doesn't work with an app factory, as there is no app object available at import time. This is the norm for most real-world Flask apps, you'd have to generate the schema from the app factory, directly or via a Blueprint.record() hook, if you have a Blueprint to associate the schema with).

It also precludes the possibility of sharing the schema between apps (as a supporting library). I'm currently building a set of related APIs, where there is overlap in models and schemas that we'll put into a shared library, and FlaskConfig() covers that case much better.

from flask-marshmallow.

sloria avatar sloria commented on August 17, 2024

To support app factories and blueprints, you could use a Function field.

def app_config_field(config_key, **kwargs):
    kwargs.setdefault("dump_only", True)
    return ma.Function(lambda _: current_app.config[config_key], **kwargs)

class MyConfig(ma.Schema):
    my_field = ma.Str()
    some_access_token = app_config_field("SOME_ACCESS_TOKEN")

But if your FlaskConfig field works best for your use case, then go ahead and use it. At work, we have a shared utility library with marshmallow Field and Schema classes that are shared across our services; this is a common practice.

from flask-marshmallow.

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.