I have following task for my project: I need to validate a java object, based on rules in a script (for example javascript).
Why in javascript? Because in javascript I can be flexible and create rules as I want (e.g. validating a combination of fields like validate(tax,recipient))
Now here is what I have:
1) I have validation rules defined in a javascript file, Rules.js
function checkPrice(price){
if(price < 0){
return false;
}
}
2) I have a plain Java Object (Invoice). And it must stay plain!
public class Invoice implements Serializable {
private String details;
private String tax;
private String recipient;
private double price;
//getter and setter
}
3) And I have a ValidatorObject. This can be a java or javascript object. Depending on your suggestion.
This ValidatorObject has a method validate, which has the Javascript Rules File (see Point 1) and the Java Object, Invoice, (see Point 2) as parameters.
validate(Rules.js, Invoice i){
//here it must take the Rules.js and use the rules inside to validate the Invoice i
}
So my question would be: Are there any frameworks that I can use to validate a Java Object based on rules defined in a javascript file? Or any tutorials, videos or suggestions?
Or how can I read a javascript file into a java object? Are there any getters or setters for javascript?
Anything would be nice!
Regards, Dave
be flexible and create rules as I want
? The example you gave would be simpler in Java.