-
-
Notifications
You must be signed in to change notification settings - Fork 212
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
Validation (was @Mandatory annotation : fail if property is not set) #30
Comments
👍 tell me if this fits your case:
When |
I think that implementation of @mandatory would work pretty fine. Put it if the user must define it, don't if a null value is acceptable. That's good for me. If you use testng, take a look at @test(expectedExceptions = MandatoryPropertyMissingException.class) to remove the "try catch block" and the "Assert.fail()" from your test methods ;) |
I am thinking that... it may be more nice to have something like:
would be more flexible and it can do the JUnit has the |
May be it worths to implements a kind of fail-fast strategy which tries to load all config's WDYT? |
It is exactly what I thought to do: when calling ConfigFactory.create, take the created proxy and for every method declaring a Since I am implementing the automatic reload() I think that if a validation error happens during an autoreload then the user doesn't get any exception - create() is done before, and now it's just a "background" reload - and the application continue working without malfunctioning. So probably I'll need to introduce a logging interface and maybe some classes to enable support to Log4j, commons logging, slf4j, and setting java.util.logging as default. |
beware that if you declare a primitive type, they will throw NullPointerException if the property is not set or NumberFormatException if the property is set to a non-numeric value:
that's the intended behavior now, but if we introduce a validation process, the validation step can intercept this error and pass it to the user during creation. for instance:
Same concept must apply to |
I agree |
Yes, I agree that
Throw an exception? |
And the above can be easily combined to
Yes, an exception should be thrown here (ValidationException or something) Type conversion should not be an issue, we'll find the way to handle that in the way the user would expect. The thing is more complex, since we now support also Collections and Array return types, so it a Validator shall be able to validate simple values or array/collections of values as well. This must be invisible to the user, the API will check if it has to validate a simple object or the array and call the correspondant method. We need to think more in deep about this. At the moment I think that Validator will have an interface like this:
but it's just a concept to be expanded. Of course we'll provide some basic validators (I think about an class with several static inner classes atm, or a separate package) and eventually also a BaseValidator to use as an adapter for user-defined return types and checks. |
I know I am late, but why don't you integrate the bean validation specs (JSR 349) for basic validation? |
Not late at all, I've not yet started to implement this. I was looking at hibernate validator too, thanks to @ffbit suggestion (http://docs.jboss.org/hibernate/stable/validator/reference/en-US/html_single/#validator-defineconstraints-spec) and it looks what we need here. |
I'm happy to see your 1.0.4 release :) What about this validators feature? Any work in progress? |
not yet, it's something that needs to be properly designed. Yesterday I committed some XML support (not that I am a fan of XML though...), see the example in last comment I posted in issue #5. I would like to add also YAML support, since it doesn't look any hard to achieve soon. The website documentation has been totally rewritten, this took some time but I'm happy with the result. |
You're right that validation would need logging. As soon as you offer validation by annotations, I will integrate OWNER at work. I like the new website documentation, and great job for this project :) |
I can't make promises regarding time for a feature to be ready, since I'm working on this project in my free time, btw I hope it won't take it long. |
Hi @mikeChampagne . Today I released version 1.0.5. Have a look at the release notes. There is a feature called "event notification" which allows for some control over how property are changing and allow the user to rollback some unwanted change. I think this is the first stepstone to validation implementation. In fact I think I'll implement the validation annotations over the event notification mechanism. Thanks for your patience and support. |
Hi, just came across this neat library which I think is very useful. One of my first thoughts was "Couldn't this be integrated with Bean Validation?" (I'm comitting to Hibernate Validator) and apparently others got that idea as well. Integration actually shouldn't be difficult using the method validation functionality new in Bean Validation 1.1. You'd just have to extend the invocation handler of configuration proxies and invoke the In case you need any help with Bean Validation, I'd be glad to help. |
@gunnarmorling thanks! I am planning to work on this. But any help is welcome. |
Hi all, Thanks to Stack Overflow I found OWNER and I spend my evening reading its documentation, its a very nice library. Here are some thoughts about validation. Validating each field independantly is nice for expressing simple constraints but sometimes there are constraints across configuration fields.
+1 for +1 for fail-fast of course. -1 for -1 for a direct support of BeanValidation which is a huge spec (I wouldn't say it is overengineered because that's a truly difficult subject). It would be better to let users hook their usage of BeanValidation on OWNER's lightweight validation system. |
@caillette thanks for your feedback. |
Bean Validation addresses this requirement via class-level constraints.
Personally I don't think there is an advantage in creating a solution from scratch instead of delegating the task of validation to an existing and proven standard. Why bother with de-fining (and implementing) all the required validation semantics and logic when you can basically get it for free? The task may seem trivial in the beginning, but as more features are going to be added (which always is going to happen), complexity will begin to creep in. Regarding complexity of the BV spec, I wouldn't say it's huge (but then I'm biased as I'm member of the expert group ;-)); The API is very easy to use for the common cases, with enough flexibility to also address more specifc scenarios. Admittedly e.g. group sequences can be a bit hard to digest, but this doesn't affect you at all as long as you don't use it. But if you happen to have requirements for such more advanced features, they are there ready to be used, e.g. also external configuration via XML, I18N-aware message interpolation etc). Also chances are that people already use Bean Validation in other parts of their application, so they'll be happy if they can use the same semantics also for configuration property validation instead of having to deal with another solution. |
@gunnarmorling By "direct support" I mean: make Bean Validation a mandatory runtime dependency for OWNER. This has plenty of drawbacks, including potential version conflicts. An indirect support would mean plugging Bean Validation to OWNER's validation system. It makes sense to offer such an option. The simplest validation system I can think about is an interface with a single validation method validating the whole Configuration object, and returning a set of objects describing what went wrong. This can be seen as a subset of Bean Validation API, so I guess Bean Validation could easily hook on it. |
I'd make validation an optional feature. If a Bean Validation provider is present it can be used to validate the configuration, otherwise not. That's how many other techs (e.g. JPA) make use of Bean Validation. |
Hi. Is there any progress on validation? That would be a great feature! |
Nope. I am not working very much on this project; I am working for a private company and I don't have too much time left. |
Like you wrote in your faq ( http://lviggiano.github.io/owner/#faq ), we might want some properties without a default value, thus forcing the user to set it in the properties file. It would be nice to have the feature juste like you describe it.
The text was updated successfully, but these errors were encountered: