Comments (5)
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.
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.
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.
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.
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)
- Flask Marshmallow syntax error when deploying to server HOT 1
- TypeError: 'method' object is not iterable HOT 1
- How to return MarshalResult when use SQLAlchemyAutoSchema HOT 1
- Race Conditions in Marshmallow - Context Not thread safe?
- ModuleNotFoundError: No module named 'flask_marshmallow' HOT 2
- HyperlinkRelated creating invalid endpoint HOT 1
- SQLAlchemy Numeric deserialized as string with auto_field
- Python3.10: Distutils depreciation warning HOT 1
- Better Separation of Duties HOT 1
- Been a while; time for a release? HOT 3
- How do I create a new ORM object with information passed via JSON? HOT 1
- DeprecationWarning from Flask-SQLAlchemy for app.extensions["sqlalchemy"] HOT 1
- Flask deprecation warning HOT 2
- 0.15.0 creates an issue during initialization (python 3.8, aws beanstalk graviton) HOT 7
- '_SQLAlchemyState' object has no attribute 'session' HOT 3
- Building docs fails with Sphinx 7.2 and SOURCE_DATE_EPOCH environment variable
- The function decorated by the "post_load" decorator will undergo changes in parameter types as the function name changes.
- New release from dev HOT 2
- Add fields from APIFlask? HOT 8
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 flask-marshmallow.