Name: fdns-rules-engine
Owner: Centers for Disease Control and Prevention Surveillance Strategy
Description: This the repository with the Java Library for the Business Rules Engine, will also need to publish to Maven.
Created: 2018-04-03 14:35:29.0
Updated: 2018-04-24 13:53:09.0
Pushed: 2018-04-24 13:53:07.0
Homepage: null
Size: 56
Language: Java
GitHub Committers
User | Most Recent Commit | # Commits |
---|
Other Committers
User | Most Recent Commit | # Commits |
---|
This is the repository with the Java Library for the Business Rules Engine.
Add this into your pom.xml
to start using the rules engine:
add the dependency to your project -->
endency>
roupId>gov.cdc</groupId>
rtifactId>fdns-rules-engine</artifactId>
ersion>1.0.0</version>
pendency>
add GitHub CDCgov to repositories -->
ositories>
epository>
<id>github-cdcgov</id>
<url>https://github.com/CDCgov/maven-repository/raw/master/</url>
repository>
positories>
The $all
operator validates the objects where the value of a field is an array that contains all the specified elements.
Syntax:
all": { "<jsonPath1>" : <expression1>, "<jsonPath2>" : <expression2>, ... } }
The <expression>
can be:
<jsonPath>
Example:
"$all": {
"$.contacts[*]": {
"hobbies": [
"Cinema",
"Ski"
]
}
}
The $and
operator performs a logical AND operation on an array of two or more <rules>
and validates objects that satisfy all business rules.
Syntax:
and": [ <rule1>, <rule2>, ... { <ruleN> } ] }
The $dateFormat
command validates objects where string values match a date format.
Syntax:
dateFormat": { "<jsonPath1>" : <expression1>, "<jsonPath2>" : <expression2>, ... } }
The <expression>
must be a String value acceptable by Java (reference: https://docs.oracle.com/javase/8/docs/api/java/text/SimpleDateFormat.html).
Example:
"$dateFormat": {
"$.contacts": {
"$.dob": "yyyyMMdd"
}
}
The $eq
operator validates objects where the value of a field equals the specified value.
Syntax:
eq": { "<jsonPath1>" : <expression1>, "<jsonPath2>" : <expression2>, ... } }
The <expression>
can be:
null
value.<jsonPath>
Example:
"$eq": {
"$.contacts[*]": {
"age": 33,
"score": 0.6,
"enabled": true,
"ip": null
}
}
The $emptyOrNull
command validates objects where the value of the field doesn't exist, is null or is an empty string.
Syntax:
emptyOrNull": { "<jsonPath1>" : <expression1>, "<jsonPath2>" : <expression2>, ... } }
The <expression>
can be:
true
or false
<jsonPath>
Example:
"$emptyOrNull": {
"$.items[*]": {
"$.val-1": false,
"$.val-2": true,
"$.val-3": true,
"$.val-4": false
}
}
The $exists
command validates object that contain the field, including object where the field value is null.
Syntax:
exists": { "<jsonPath1>" : <expression1>, "<jsonPath2>" : <expression2>, ... } }
The <expression>
can be:
<jsonPath>
Example:
"$exists": {
"$.contacts": true,
"$.contacts[*]": {
"name": true,
"age": false
}
}
$gt
validates those objects where the value of the field is greater than (i.e. >
) the specified value.
Syntax:
gt": { "<jsonPath1>" : <expression1>, "<jsonPath2>" : <expression2>, ... } }
The <expression>
can be:
<jsonPath>
Example:
"$gt": {
"$.contacts[*]": {
"age": 18,
"score": 0.5,
"enabled": false
}
}
$gte
validates those objects where the value of the field is greater than or equal to (i.e. >=
) the specified value.
Syntax:
gte": { "<jsonPath1>" : <expression1>, "<jsonPath2>" : <expression2>, ... } }
The <expression>
can be:
<jsonPath>
Example:
"$gte": {
"$.contacts[*]": {
"age": 18,
"score": 0.5,
"enabled": false
}
}
The $in
operator validates the objects where the value of a field equals any value in the specified array.
Syntax:
in": { "<jsonPath1>" : <expression1>, "<jsonPath2>" : <expression2>, ... } }
The <expression>
can be:
null
value.<jsonPath>
Example:
"$in": {
"$.contact.name": [
"Ben",
"John",
"Harry"
],
"$.contact.age": [
30,
31,
32,
33,
"N/a"
],
"$.contact.score": [
0,
0.6
],
"$.contact.enabled": [
true,
false
],
"$.contact.ip": [
null,
"0.0.0.0"
]
}
$lt
validates the objects where the value of the field is less than (i.e. <
) the specified value.
Syntax:
lt": { "<jsonPath1>" : <expression1>, "<jsonPath2>" : <expression2>, ... } }
The <expression>
can be:
<jsonPath>
Example:
"$lt": {
"$.contacts[*]": {
"age": 18,
"score": 0.5,
"enabled": false
}
}
$lte
validates the objects where the value of the field is less than or equal (i.e. <=
) the specified value.
Syntax:
lte": { "<jsonPath1>" : <expression1>, "<jsonPath2>" : <expression2>, ... } }
The <expression>
can be:
<jsonPath>
Example:
"$lte": {
"$.contacts[*]": {
"age": 18,
"score": 0.5,
"enabled": true
}
}
Validate objects where the value of a field divided by a divisor has the specified remainder (i.e. perform a modulo operation to validate objects).
Syntax:
mod": { "<jsonPath1>" : <expression1>, "<jsonPath2>" : <expression2>, ... } }
The <expression>
can be:
<jsonPath>
Example:
"$mod": {
"$.contact.age": [3, 0]
}
$ne
validates the objects where the value of the field is not equal (i.e. !=
) to the specified value. This validates objects that do not contain the field.
Syntax:
ne": { "<jsonPath1>" : <expression1>, "<jsonPath2>" : <expression2>, ... } }
The <expression>
can be:
null
value<jsonPath>
Example:
"$ne": {
"$.contact.name": "System",
"$.contact.age": 18,
"$.contact.score": 0.5,
"$.contact.enabled": false,
"$.contact.ip": null
}
$nin
validates the objects where: (a) the field value is not in the specified array or (b) the field does not exist.
Syntax:
nin": { "<jsonPath1>" : <expression1>, "<jsonPath2>" : <expression2>, ... } }
The <expression>
can be:
null
value<jsonPath>
Example:
"$nin": {
"$.contact.name": [
"System",
"Admin",
"N/a"
],
"$.contact.age": [
18,
19,
20,
"N/a"
],
"$.contact.score": [
0,
0.5
],
"$.contact.enabled": [
false
],
"$.contact.ip": [
null,
"0.0.0.0"
]
}
$nor
performs a logical NOR operation on an array of one or more business rules and validates objects that fail all the rules in the array.
Syntax:
nor": [ <rule1>, <rule2>, ... { <ruleN> } ] }
$not
performs a logical NOT operation on the specified <rule>
and validates the objects that do not match the <rule>
.
Syntax:
not": <rule> }
The $or
operator performs a logical OR operation on an array of two or more <rules>
and validates the object if at least one rule is satisfied.
Syntax:
or": [ <rule1>, <rule2>, ... { <ruleN> } ] }
Provides regular expression capabilities for pattern matching strings in object validations. If you need help to write your regex pattern, consult this page: https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html.
Syntax:
regex": { "<jsonPath1>" : <expression1>, "<jsonPath2>" : <expression2>, ... } }
The <expression>
can be:
<jsonPath>
Example:
"$regex": {
"$.contacts[*]": {
"name": "[A-Z][a-z]+"
}
}
The $size
operator validates any array with the number of elements specified by the argument.
Syntax:
size": { "<jsonPath1>" : <expression1>, "<jsonPath2>" : <expression2>, ... } }
The <expression>
can be:
<jsonPath>
Example:
"$size": {
"$.contacts[*]": {
"hobbies": 3
}
}
The $type
command validate objects where the value of the field is an instance of the specified type.
Syntax:
type": { "<jsonPath1>" : <expression1>, "<jsonPath2>" : <expression2>, ... } }
The <expression>
can be:
<jsonPath>
Example:
"$type": {
"$.contacts[*]": {
"name": "String",
"age": "Number",
"score": "Number",
"hobbies": "Array"
}
}
It's possible as well to create your own custom command in Java. If you want to create a command that checks if an array don't contain duplicates. Just create a Java class like this one that implements the abstract class AbstractCommand
:
age gov.cdc.engine.commands;
rt java.util.HashSet;
rt java.util.Set;
rt org.json.JSONObject;
rt gov.cdc.engine.ValidatorException;
rt gov.cdc.engine.result.CompoundValidationResult;
rt gov.cdc.engine.result.ValidationResult;
he $noDuplicate operator validates any array that contains only unique
alues.
author Ben Chevallereau
ic class NoDuplicateCommand extends SingleCommand {
blic String getKeyword() {
return "$noDuplicate";
verride
otected void isValid(JSONObject rule, String jsonPath, Object value, boolean found, CompoundValidationResult result) throws ValidatorException {
// If the field doesn't exist in the object
if (!found) {
result.addResult(ValidationResult.create(false, rule.toString(), getKeyword()));
} else if (rule.get(jsonPath) instanceof Boolean) {
Boolean parameter = (Boolean) rule.get(jsonPath);
boolean valid = false;
if (value instanceof net.minidev.json.JSONArray) {
net.minidev.json.JSONArray v = (net.minidev.json.JSONArray) value;
Set<Object> foundValues = new HashSet<Object>();
for (int i = 0; i < v.size(); i++)
foundValues.add(v.get(i));
valid = foundValues.size() == v.size();
} else
throw new ValidatorException("The retrieved value is not a JSON array: " + value);
if (!parameter)
valid = !valid;
result.addResult(ValidationResult.create(valid, rule.toString(), getKeyword()));
} else if (rule.get(jsonPath) instanceof JSONObject) {
// If the parameter is a sub-rule
JSONObject subrule = rule.getJSONObject(jsonPath);
// Recurse
result.addAllResults(recurse(subrule, value));
} else
throw new ValidatorException("Don't understand the following parameter: " + rule.get(jsonPath));
Then, you can use your new rule keyword in your JSON configuration:
"$noDuplicate": {
"$.contacts[*]": {
"hobbies": true
}
}
This repository constitutes a work of the United States Government and is not subject to domestic copyright protection under 17 USC § 105. This repository is in the public domain within the United States, and copyright and related rights in the work worldwide are waived through the CC0 1.0 Universal public domain dedication. All contributions to this repository will be released under the CC0 dedication. By submitting a pull request you are agreeing to comply with this waiver of copyright interest.
The repository utilizes code licensed under the terms of the Apache Software License and therefore is licensed under ASL v2 or later.
This source code in this repository is free: you can redistribute it and/or modify it under the terms of the Apache Software License version 2, or (at your option) any later version.
This soruce code in this repository is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Apache Software License for more details.
You should have received a copy of the Apache Software License along with this program. If not, see http://www.apache.org/licenses/LICENSE-2.0.html
The source code forked from other open source projects will inherit its license.
This repository contains only non-sensitive, publicly available data and information. All material and community participation is covered by the Surveillance Platform Disclaimer and Code of Conduct. For more information about CDC's privacy policy, please visit http://www.cdc.gov/privacy.html.
Anyone is encouraged to contribute to the repository by forking and submitting a pull request. (If you are new to GitHub, you might start with a basic tutorial.) By contributing to this project, you grant a world-wide, royalty-free, perpetual, irrevocable, non-exclusive, transferable license to all users under the terms of the Apache Software License v2 or later.
All comments, messages, pull requests, and other submissions received through CDC including this GitHub page are subject to the Presidential Records Act and may be archived. Learn more at http://www.cdc.gov/other/privacy.html.
This repository is not a source of government records, but is a copy to increase collaboration and collaborative potential. All government records will be published through the CDC web site.
Please refer to CDC's Template Repository for more information about contributing to this repository, public domain notices and disclaimers, and code of conduct.