|
45 | 45 | */ |
46 | 46 | @Description("Validate JSON against a given schema, send only valid input to the receiver. Pass the schema location to validate against. " + |
47 | 47 | "Set 'schemaRoot' for resolving sub-schemas referenced in '$id' or '$ref' (defaults to the classpath root: '/'). " + |
48 | | - "Write valid and/or invalid output to locations specified with 'writeValid' and 'writeInvalid'.") |
| 48 | + "Write valid and/or invalid output to locations specified with 'writeValid' and 'writeInvalid'." + |
| 49 | + "Set the JSON key for the record ID value with 'idKey' (for logging output, defaults to 'id').") |
49 | 50 | @In(String.class) |
50 | 51 | @Out(String.class) |
51 | 52 | @FluxCommand("validate-json") |
52 | 53 | public final class JsonValidator extends DefaultObjectPipe<String, ObjectReceiver<String>> { |
53 | 54 |
|
54 | 55 | private static final Logger LOG = LoggerFactory.getLogger(JsonValidator.class); |
55 | 56 | private static final String DEFAULT_SCHEMA_ROOT = "/"; |
| 57 | + private static final String DEFAULT_ID_KEY = "id"; |
56 | 58 | private String schemaUrl; |
57 | 59 | private Schema schema; |
58 | 60 | private long fail; |
59 | 61 | private long success; |
60 | 62 | private FileWriter writeInvalid; |
61 | 63 | private FileWriter writeValid; |
62 | 64 | private String schemaRoot = DEFAULT_SCHEMA_ROOT; |
| 65 | + private String idKey = DEFAULT_ID_KEY; |
63 | 66 |
|
64 | 67 | /** |
65 | 68 | * @param url The URL of the schema to validate against. |
@@ -89,6 +92,13 @@ public void setWriteInvalid(final String writeInvalid) { |
89 | 92 | this.writeInvalid = fileWriter(writeInvalid); |
90 | 93 | } |
91 | 94 |
|
| 95 | + /** |
| 96 | + * @param idKey The JSON key for the record ID value. |
| 97 | + */ |
| 98 | + public void setIdKey(final String idKey) { |
| 99 | + this.idKey = idKey; |
| 100 | + } |
| 101 | + |
92 | 102 | @Override |
93 | 103 | public void process(final String json) { |
94 | 104 | final JSONObject object; |
@@ -149,7 +159,7 @@ private FileWriter fileWriter(final String fileLocation) { |
149 | 159 |
|
150 | 160 | private void handleInvalid(final String json, final JSONObject object, |
151 | 161 | final String errorMessage) { |
152 | | - LOG.info("Invalid JSON: {} in {}", errorMessage, object != null ? object.opt("id") : json); |
| 162 | + LOG.info("Invalid JSON: {} in {}", errorMessage, object != null ? object.opt(idKey) : json); |
153 | 163 | ++fail; |
154 | 164 | write(json, writeInvalid); |
155 | 165 | } |
|
0 commit comments