use of org.apache.nifi.serialization.record.validation.ValidationError in project nifi by apache.
the class TestStandardSchemaValidator method testExtraFields.
@Test
public void testExtraFields() {
final List<RecordField> fields = new ArrayList<>();
fields.add(new RecordField("id", RecordFieldType.INT.getDataType()));
final RecordSchema schema = new SimpleRecordSchema(fields);
final Map<String, Object> valueMap = new LinkedHashMap<>();
valueMap.put("id", 1);
valueMap.put("name", "John Doe");
Record record = new MapRecord(schema, valueMap, false, false);
final SchemaValidationContext allowExtraFieldsContext = new SchemaValidationContext(schema, true, true);
final SchemaValidationContext forbidExtraFieldsContext = new SchemaValidationContext(schema, false, false);
StandardSchemaValidator validator = new StandardSchemaValidator(allowExtraFieldsContext);
SchemaValidationResult result = validator.validate(record);
assertTrue(result.isValid());
assertNotNull(result.getValidationErrors());
assertTrue(result.getValidationErrors().isEmpty());
validator = new StandardSchemaValidator(forbidExtraFieldsContext);
result = validator.validate(record);
assertFalse(result.isValid());
assertNotNull(result.getValidationErrors());
final Collection<ValidationError> validationErrors = result.getValidationErrors();
assertEquals(1, validationErrors.size());
final ValidationError validationError = validationErrors.iterator().next();
assertEquals("/name", validationError.getFieldName().get());
System.out.println(validationError);
}
use of org.apache.nifi.serialization.record.validation.ValidationError in project nifi by apache.
the class TestStandardSchemaValidator method testInvalidEmbeddedField.
@Test
public void testInvalidEmbeddedField() {
final List<RecordField> accountFields = new ArrayList<>();
accountFields.add(new RecordField("name", RecordFieldType.STRING.getDataType()));
accountFields.add(new RecordField("balance", RecordFieldType.DOUBLE.getDataType()));
final RecordSchema accountSchema = new SimpleRecordSchema(accountFields);
final List<RecordField> fields = new ArrayList<>();
fields.add(new RecordField("id", RecordFieldType.INT.getDataType()));
fields.add(new RecordField("account", RecordFieldType.RECORD.getRecordDataType(accountSchema)));
final RecordSchema schema = new SimpleRecordSchema(fields);
final Map<String, Object> accountValues = new HashMap<>();
accountValues.put("name", "account-1");
accountValues.put("balance", "123.45");
final Record accountRecord = new MapRecord(accountSchema, accountValues);
final Map<String, Object> valueMap = new LinkedHashMap<>();
valueMap.put("id", 1);
valueMap.put("account", accountRecord);
Record record = new MapRecord(schema, valueMap, false, false);
final SchemaValidationContext strictValidationContext = new SchemaValidationContext(schema, false, true);
final SchemaValidationContext lenientValidationContext = new SchemaValidationContext(schema, false, false);
StandardSchemaValidator validator = new StandardSchemaValidator(strictValidationContext);
SchemaValidationResult result = validator.validate(record);
assertFalse(result.isValid());
assertNotNull(result.getValidationErrors());
assertEquals(1, result.getValidationErrors().size());
final ValidationError validationError = result.getValidationErrors().iterator().next();
assertEquals("/account/balance", validationError.getFieldName().get());
validator = new StandardSchemaValidator(lenientValidationContext);
result = validator.validate(record);
assertTrue(result.isValid());
assertNotNull(result.getValidationErrors());
assertTrue(result.getValidationErrors().isEmpty());
}
use of org.apache.nifi.serialization.record.validation.ValidationError in project nifi by apache.
the class ValidateRecord method logValidationErrors.
private void logValidationErrors(final FlowFile flowFile, final int recordCount, final SchemaValidationResult result) {
if (getLogger().isDebugEnabled()) {
final StringBuilder sb = new StringBuilder();
sb.append("For ").append(flowFile).append(" Record #").append(recordCount).append(" is invalid due to:\n");
for (final ValidationError error : result.getValidationErrors()) {
sb.append(error).append("\n");
}
getLogger().debug(sb.toString());
}
}
Aggregations