owner icon indicating copy to clipboard operation
owner copied to clipboard

How to force a mandatory value, without doing null checks?

Open drapostolos opened this issue 10 years ago • 3 comments

How can I force my users to provide a value for a specific property?

I have defined a Config (as below), where I would want the OWNER library to throw an exception (with good error explanation), if the user does not provide a property (either through property file, or system property). Is it possible?

Or even better, is it possible to configure OWNER to throw exception when any property is missing instead of returning null? Following the fail fast principle [1], to avoid boiler plate null checks in the code. Possibly List/Array types should return empty lists/arrays (instead of throwing exception).

import org.aeonbits.owner.Config;
import org.aeonbits.owner.Config.Sources;
import org.aeonbits.owner.ConfigFactory;

@Sources({ "classpath:some.properties" })
public interface MyConfig extends Config {

    String mandatory();

    public static void main(String[] args) {
        MyConfig cfg = ConfigFactory.create(MyConfig.class, System.getProperties());
        System.out.println(cfg.mandatory());
    }
}

[1] http://martinfowler.com/ieeeSoftware/failFast.pdf

drapostolos avatar Sep 15 '14 08:09 drapostolos

This is not possible at the moment.

This would be possible when we implement some validation mechanism which isn't available with current version. But I'd like to add this feature (when I'll have time for this).

PropertyChangeListeners may come handy to implement some kind of validation, events get triggered only after the config object is created; so this won't help your case.

If somebody wants to help with a valdiation mechanism, I can provide informations on how this can be implemented. At the moment I am not doing regular work on this project, so I cannot provide estimation on when this will be available.

lviggiano avatar Sep 23 '14 12:09 lviggiano

Yes, please provide information how to implement this and I'll have a look at it when I have time.

drapostolos avatar Sep 24 '14 08:09 drapostolos

Will write some spec on the wiki pages, as soon as I can. Thanks for your patience.

lviggiano avatar Oct 01 '14 08:10 lviggiano