Skip to content

Examples

Zachary Moore edited this page Jan 22, 2019 · 2 revisions

Examples

On this page you will find sample usage of the library flow excluding the QuickStart Example

Example Implementation Repo with Build Integration

Head over to JSONCustomLinrExampleImplementation for full implementation example.

Step above QuickStart

In this example we are checking if a JSONObject:

  1. Has a type field which a value of boolean
  2. Has a name field with a value that is a String and starts with has
  3. Has a closest key value of fields
  4. 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");
    }
}

Array Size enforcer

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();
    }
}

Illegal Key Enforcer

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();
    }
}

Illegal String enforcer

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();
    }
}

Clone this wiki locally