-
Notifications
You must be signed in to change notification settings - Fork 17
Validation Engine
See latest version in maven central.
Maven
<!-- The dependency for doov-core -->
<dependency>
<groupId>io.doov</groupId>
<artifactId>doov-core</artifactId>
<version>LATEST</version>
</dependency>
<!-- The dependency on your generated DSL -->
<dependency>
<groupId>io.doov</groupId>
<artifactId>doov-sample-generated</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
Gradle
dependencies {
compile project(':doov-sample-generated')
}
This documentation is based on the sample project in
dOOv. You can replace
the package name io.doov.sample
by your package name as com.example.myapp
and classes name Sample
with MyApp
.
To use the dOOv validation engine, you just need to pass your instanciated model to the rule.
package io.dooov.sample.validation;
import static io.doov.sample.validation.SampleRules.RULE_EMAIL;
public class Execute {
public static void main(String... args) {
Account account = new Account();
account.setEmail("[email protected]");
Model model = new Model();
model.setAccount(account);
System.out.println("Validation model " + model +
" with rule " + Validation.email.readable());
Result result = RULE_EMAIL.executeOn(model);
System.out.println("> " + result.isTrue());
if (result.isFalse()) {
System.out.println("Failure cause is: " + result.getFailureCause());
}
}
}
The rules provides an AST that can be printed as a human readable format with
the Readable#readable
method that is available on any DSL object. By default
the output is from AstLineVisitor
that outputs the string in plain text.
System.out.println(io.doov.sample.validation.SampleRules.RULE_EMAIL.readable());
> When user email matches '\w+[@]\w+\.com', validate with empty message
You can print the syntax tree by calling (or creating your own)
MetadataVisitor
, such as AstHtmlVisitor
. The method to call is
ValidationRule#accept(MetadataVisitor)
. For example, the HTML renderer will
output a tree of tokenized conditions.
<div class='dsl-validation-rule'>
<ul>
<li><span class='dsl-token-rule'>Rule</span>
<ul>
<li><span class='dsl-token-when'>When</span></li>
<ul>...</ul>
<li>validate with message <span class='dsl-validation-message'>empty</span></li></ul>
</ul>
</div>
Predicate evaluation in dOOv is very similar to Java's lambda predicate
evaluation. It differs from bare metal or
/ and
since there is no operator
precedence in dOOv, only tree evaluation order precedence. In general, you can
consider:
- predicates evaluates dept first, left to right (that means multiple chained and / or predicates might not evaluate the same way as bare metal Java)
- predicates short-circuits the same way as Java logical operators and streams do (but that can be deactivated)
If you want to deactivate short-circuiting, you can do it with
ValidationRule#withShortCircuit
and use false
(it is true
by default). It
will impact performance, but it is useful if you want complete runtime metrics
("what nodes trigger on my production server", etc.).
TODO
See Testing Rules