apara
apara

Reputation: 81

Java model validation

We are looking for a Java library/system/package which not only does basic validation but also can do relationship validation. We need to be able to express validation criteria which is based on multiple related entities.

Most of the validation models Spring Validation, JSR303 are specifically targeted at validation of bean's attributes. But we need something that would go across beans.

Our requirements are to come up with a method of validating a model state while externalizing validation logic out of the java code.

In the above definition a Bean is just a POJO, and a model is a collection of related Beans. So, for example, if Account has a collection of Addresses and the Account.countryOfResidence is set to USA, I would like to have a validation rule that will ensure that all Addresses have a country of USA in them.

So during the "operation" of adding an Address to the Account, a validation would kick off ensuring that Address.country is the same as Account.countryOfResidence.

We were looking into DRULES, but wanted to see if there were other options available.

Any suggestions on how to proceed?

Upvotes: 8

Views: 2920

Answers (3)

Ed Staub
Ed Staub

Reputation: 15690

I'm disappointed by what's available in this space - probably because I want an impossible combination of features - having cake and eating it. So this is more by way of a lengthy comment than what I, at least consider an answer. I marked this question as a "favorite", hoping someone else would point us at a majick bullet. But I guess there are no wizards in the house.

One possibility is OCL, especially with the support provided with the relevant Eclipse plugins. I've looked at it and rejected it because it didn't seem worth the effort - but your mileage may differ.

Another possibility (maybe!!!) is to leverage pieces of one of the tools that have been written for BDD. However, these are written for testing, not production code - "given scenario x, I expect y to happen". I'm referring to things like easyb, jbehave, and the cuke4duke port of cucumber - especially easyb. I think jbehave is too Java-heavy for you, and I haven't seriously looked at cucumber yet.

If you find something that works out for you, please come back and write your own answer. Note that you got 7 upvotes and 4 favorites on this - enquiring minds want to know!

Upvotes: 0

Michael Deardeuff
Michael Deardeuff

Reputation: 10697

I think this is possible to do in JSR 303 with the ScriptAssert validator or a custom validator.

This question has several custom cross-field validators that could be used as a basis for creating your own.

And here is a an example of the @ScriptAssert validator.

@ScriptAssert(lang="javascript", script="_this.countryOfResidence.equals(_this.address.country)")
// @ScriptAssert(lang="jexl", script="_this.countryOfResidence == _this.address.country")
public class Account {
    @NotNull @Valid
    private Address adddress;

    private String countryOfResidence;
}

public class Address {
    ...
    @NotNull
    private String country;
}

This is certainly a lot less code than a custom validator.


Note: validators can also be created in XML (if that's the way you roll).

Upvotes: 0

Shaun
Shaun

Reputation: 2476

Not sure how active this project is, but I used it a while ago and it provided capabilities to do what you're describing - take a look and see if it might be helpful:

http://i-screen.org/docs/index.html

And of course there's Jess: http://www.jessrules.com/jess/

It also might be worth taking a look at this, although I don't know much about it. Vlad: http://www.sapia-oss.org/projects/vlad/

Upvotes: 1

Related Questions