8

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?

apara
  • 81
  • 1
  • 4
  • In your context, what's a model, what's a "model state", and what's validation? It might help people produce answers if they knew what you seek. – CPerkins Nov 18 '11 at 16:13
  • 1
    Why do you want to avoid Java for validation? Java seems like the most expressive language to do that, has lots of APIs available, and easily accesses Java objects. – JB Nizet Nov 18 '11 at 16:15
  • What sorts of "relationship validation" do you need to do? I doubt you're going to find anything pre-made to do something like this, but you could probably write your own validators in one of the APIs mentioned. – Nate Nov 18 '11 at 16:46
  • I updated the question to clarify what I mean by a model. – apara Nov 18 '11 at 18:51
  • Why do you need to "externalize validation logic out of the java code"? I'm not arguing against it - I can generally guess - but it would be good to hear from you to hear the specifics. In particular - _what are the metarequirements for the language the validation requirements _are_ expressed in_. E.g., readable by "general public", etc.... – Ed Staub Dec 05 '11 at 00:48

3 Answers3

1

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/

Shaun
  • 2,446
  • 19
  • 33
  • An alternative to Jess is the JBoss Project Formerly Known as Drools.What I don't like about these is 1) XML; 2) validation rules defined in a separate file; 3) validation defined in a separate XML file. – Michael Deardeuff Nov 24 '11 at 02:37
0

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

Community
  • 1
  • 1
Michael Deardeuff
  • 10,386
  • 5
  • 51
  • 74
0

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!

Ed Staub
  • 15,480
  • 3
  • 61
  • 91