Comments (5)
Right there is apply_default_on_none
which forces default to take place when value is None
we can add a new option named apply_default_on_failure
so we follow the same semantic.
Validator("key", **options, default=1, apply_default_on_failure=True)
This will basically be a way to silence validation errors
from dynaconf.
Until you can put something clean in place, here's what I've used so far:
settings.validators.register(*validators)
failed_validator = True
while failed_validator:
try:
settings.validators.validate_all()
failed_validator = False
except ValidationError as e:
current_validator_details = e.details[0][0]
if hasattr(current_validator_details, 'default') and current_validator_details.default is not empty:
settings[current_validator_details.names[0]] = current_validator_details.default
else:
logging.critical(f"Value for {current_validator_details.names[0]} doesn't pass validation and there's no "
f"default value. This issue must be reported. This software won't works until it's been fixed.")
os._exit(0)
It's far from perfect but if it could help others!
from dynaconf.
@morpheus65535, I may be missing something because I don't use validators a lot, but I've done some experiments on this case.
Given
- a file that loads
foo=1
- a condition that
foo>9
- on condition failure,
foo
is set to10
Considerations
# doesnt work (kinda expected?):
# 'default' is used when key-value hasnt been loaded, not when validation fails
Validator("foo", gt=9, default=10)
# suggestion:
# implement different kwarg for default on validation failure to preserve 'default' original behavior
Validator("foo", gt=9, on_failure=10)
# should work (but does not):
# As the first validation failed, "foo" should not be set
# As "foo" was not set (because was invalidated), default should apply
Validator("foo", gt=9) | Validator("foo", default=10)
Workaround
You could try using a helper function like below, but I cannot guarantee it will work well on more complex cases.
from dynaconf import Dynaconf, Validator
import dynaconf
# helper function
def default_if_fails(
setting_obj,
key,
default,
**kwargs
):
"""Utility to set default value on Validator failure"""
setting_obj.validators.register(Validator(key, **kwargs))
try:
settings.validators.validate()
except dynaconf.validator.ValidationError:
settings.set(key, default)
# usage
settings = Dynaconf(foo=1)
default_if_fails(settings, key="foo", default=10, gt=9)
assert settings.foo == 10
from dynaconf.
@pedro-psb thanks for you answer!
@rochacbruno having that option would be very nice. I'm migrating from basic .ini file configuration and have more than 190 different settings in a single config.yaml file. This config file is deployed on about 40 000 instances around the globe since the last 5 years. I hope to not get invalid settings but I expect the worst and would prefer to get default value than preventing the software from starting because of a validation errors. ;-)
from dynaconf.
Having this as a global instance option would be really nice instead of having to add it to all my Validators.
from dynaconf.
Related Issues (20)
- Merging doesn't appear to work with load_file method but does with settings=[] in the constructor
- [bug] Django and Dynaconf: Can't merge INSTALLED_APPS from external settings_file into settings.py HOT 2
- [RFC] add `@get` converter HOT 3
- Improve documentation on cast and default HOT 3
- [bug] Deleting entry raises an error
- [RFC] Pydantic Schema Validation HOT 3
- Centralized config package | External hooks for `platformdirs`, __package__, package_dir, etc. HOT 1
- [bug] Environment Variable Overrides Not Working with Nested .toml Values HOT 1
- [bug] Dynaconf.load_file() no error on missing file(s)
- Documentation used to be clear on purpose of global, and the default environment HOT 2
- Multiple cast validators get discarded
- Broken link to source code in docs HOT 1
- Standard docstrings style for the codebase HOT 3
- Validator default string parsed to number HOT 3
- [bug] Validation on Dynaconf instantiation not working HOT 1
- Validation doc section "On instantiation" improvement HOT 2
- [CI] New release process HOT 2
- [RFC] Add `as_dict` alias to `to_dict` for `DynaBox` for consistency between `LazySettings` and `DynaBox` objs HOT 1
- [CI] Update codecov configuration file
- [RFC] Add FORCE_SETTINGS_FILES to LazySettings.configure() for pytest
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 dynaconf.