Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
drapostolos opened this issue Sep 15, 2014 · 3 comments
Open

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

drapostolos opened this issue Sep 15, 2014 · 3 comments
Labels
Milestone

Comments

@drapostolos
Copy link
Contributor

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] https://martinfowler.com/ieeeSoftware/failFast.pdf

@lviggiano
Copy link
Collaborator

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.

@drapostolos
Copy link
Contributor Author

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

@lviggiano
Copy link
Collaborator

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants