Skip to content

Commit 8a1ceb5

Browse files
committed
Make JSON key for the record ID value configurable (#443)
1 parent f182204 commit 8a1ceb5

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

metafacture-json/src/main/java/org/metafacture/json/JsonValidator.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,21 +45,24 @@
4545
*/
4646
@Description("Validate JSON against a given schema, send only valid input to the receiver. Pass the schema location to validate against. " +
4747
"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').")
4950
@In(String.class)
5051
@Out(String.class)
5152
@FluxCommand("validate-json")
5253
public final class JsonValidator extends DefaultObjectPipe<String, ObjectReceiver<String>> {
5354

5455
private static final Logger LOG = LoggerFactory.getLogger(JsonValidator.class);
5556
private static final String DEFAULT_SCHEMA_ROOT = "/";
57+
private static final String DEFAULT_ID_KEY = "id";
5658
private String schemaUrl;
5759
private Schema schema;
5860
private long fail;
5961
private long success;
6062
private FileWriter writeInvalid;
6163
private FileWriter writeValid;
6264
private String schemaRoot = DEFAULT_SCHEMA_ROOT;
65+
private String idKey = DEFAULT_ID_KEY;
6366

6467
/**
6568
* @param url The URL of the schema to validate against.
@@ -89,6 +92,13 @@ public void setWriteInvalid(final String writeInvalid) {
8992
this.writeInvalid = fileWriter(writeInvalid);
9093
}
9194

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+
92102
@Override
93103
public void process(final String json) {
94104
final JSONObject object;
@@ -149,7 +159,7 @@ private FileWriter fileWriter(final String fileLocation) {
149159

150160
private void handleInvalid(final String json, final JSONObject object,
151161
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);
153163
++fail;
154164
write(json, writeInvalid);
155165
}

0 commit comments

Comments
 (0)