Giter Site home page Giter Site logo

Comments (5)

rochacbruno avatar rochacbruno commented on June 9, 2024 3

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.

morpheus65535 avatar morpheus65535 commented on June 9, 2024 2

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.

pedro-psb avatar pedro-psb commented on June 9, 2024

@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 to 10

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.

morpheus65535 avatar morpheus65535 commented on June 9, 2024

@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.

morpheus65535 avatar morpheus65535 commented on June 9, 2024

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)

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.