-
Notifications
You must be signed in to change notification settings - Fork 2
Examples
Zachary Moore edited this page Jan 22, 2019
·
2 revisions
On this page you will find sample usage of the library flow excluding the QuickStart Example
Head over to JSONCustomLinrExampleImplementation for full implementation example.
In this example we are checking if a JSONObject:
- Has a
typefield which a value ofboolean - Has a
namefield with a value that is aStringand starts withhas - Has a closest key value of
fields - Has a parent object that is a
JSONArray
Example
Bad
{
"fields" : [
{
"name": "hasX",
"type": "boolean"
}]
}class Example {
public static void setupLint() {
LintImplementation<JSONObject> lintImplementation = new LintImplementation<JSONObject>() {
@Override
public Class<JSONObject> getClazz() {
return JSONObject.class;
}
@Override
public boolean shouldReport(JSONObject jsonObject) {
boolean hasBooleanType = hasKeyAndValueEqualTo(jsonObject, "type", "boolean");
WrappedPrimitive name = safeGetWrappedPrimitive(jsonObject, "name");
boolean nameStartsWithHas = false;
if (name != null && name.getValue() instanceof String) {
nameStartsWithHas = ((String) name.getValue()).startsWith("has");
}
boolean originatingKeyIsFields = isOriginatingKeyEqualTo(jsonObject, "fields");
boolean isParentArray = isParentOfType(jsonObject, JSONArray.class);
setReportMessage("This is a bad one:\t" + jsonObject);
return reduceBooleans(hasBooleanType, nameStartsWithHas, originatingKeyIsFields, isParentArray);
}
};
LintRule rule = new LintRule.Builder()
.setLevel(LintLevel.ERROR)
.setImplementation(lintImplementation)
.setIssueId("BOOLEAN_NAME_STARTS_WITH_HAS")
.build();
LintRegister register = new LintRegister();
register.register(rule);
LintRunner lintRunner = new LintRunner(register, "./models");
ReportRunner reportRunner = new ReportRunner(lintRunner);
reportRunner.report("build/reports");
}
}public class ArraySizeRule {
private static final int MAX_ARRAY_SIZE = 3;
private static final String ISSUE_ID = "Array Size Rule";
public static LintRule getArraySizeRule() throws LintRule.Builder.LintRuleBuilderException {
LintImplementation<JSONArray> maxArraySizeImplementation = new LintImplementation<JSONArray>() {
@Override
public Class<?> getClazz() {
return JSONArray.class;
}
@Override
public boolean shouldReport(JSONArray jsonArray) {
return jsonArray.toList().size() > MAX_ARRAY_SIZE;
}
@Override
public String report(JSONArray jsonArray) {
return "JSONArray with contents " + jsonArray.toString() + " has more than " + MAX_ARRAY_SIZE + " objects";
}
};
return new LintRule.Builder()
.setImplementation(maxArraySizeImplementation)
.setIssueId(ISSUE_ID)
.setLevel(LintLevel.ERROR)
.build();
}
}public class JSONObjectKeyRule {
private static final String[] DISALLOWED_KEYS = {"test", "x", "y", "phone"};
private static final String ISSUE_ID = "JSON Object Key Rule";
public static LintRule getJsonObjectKeyRule() throws LintRule.Builder.LintRuleBuilderException {
LintImplementation<JSONObject> lintImplementation = new LintImplementation<JSONObject>() {
@Override
public Class<?> getClazz() {
return JSONObject.class;
}
@Override
public boolean shouldReport(JSONObject jsonObject) {
if (jsonObject.toMap().containsKey(DISALLOWED_KEYS[0])
|| jsonObject.toMap().containsKey(DISALLOWED_KEYS[1])
|| jsonObject.toMap().containsKey(DISALLOWED_KEYS[2])
|| jsonObject.toMap().containsKey(DISALLOWED_KEYS[3])) {
setReportMessage("JSONObject with content " + jsonObject.toString()
+ " has keys "
+ jsonObject.toMap().keySet()
+ " disallowed keys are "
+ Arrays.toString(DISALLOWED_KEYS));
return true;
}
return false;
}
};
return new LintRule.Builder()
.setImplementation(lintImplementation)
.setLevel(LintLevel.WARNING)
.setIssueId(ISSUE_ID)
.build();
}
}public class DisallowedStringRule {
private static String ISSUE_ID = "Dissalowed String Rule";
private static String DISALLOWED_STRING = "test";
public static LintRule getDissalowedStringRule() throws LintRule.Builder.LintRuleBuilderException {
LintImplementation<WrappedPrimitive<String>> lintImplementation = new LintImplementation<WrappedPrimitive<String>>() {
@Override
public Class<?> getClazz() {
return String.class;
}
@Override
public boolean shouldReport(WrappedPrimitive<String> stringWrappedPrimitive) {
return stringWrappedPrimitive.equals(DISALLOWED_STRING);
}
@Override
public String report(WrappedPrimitive<String> wrappedPrimitive) {
return "Found " + DISALLOWED_STRING + " with parent " + wrappedPrimitive.getParentObject().toString();
}
};
return new LintRule.Builder()
.setImplementation(lintImplementation)
.setIssueId(ISSUE_ID)
.setLevel(LintLevel.ERROR)
.build();
}
}