Search in sources :

Example 6 with ValidationError

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);
}
Also used : SimpleRecordSchema(org.apache.nifi.serialization.SimpleRecordSchema) MapRecord(org.apache.nifi.serialization.record.MapRecord) RecordField(org.apache.nifi.serialization.record.RecordField) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) SchemaValidationResult(org.apache.nifi.serialization.record.validation.SchemaValidationResult) Record(org.apache.nifi.serialization.record.Record) MapRecord(org.apache.nifi.serialization.record.MapRecord) ValidationError(org.apache.nifi.serialization.record.validation.ValidationError) RecordSchema(org.apache.nifi.serialization.record.RecordSchema) SimpleRecordSchema(org.apache.nifi.serialization.SimpleRecordSchema) Test(org.junit.Test)

Example 7 with 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());
}
Also used : SimpleRecordSchema(org.apache.nifi.serialization.SimpleRecordSchema) MapRecord(org.apache.nifi.serialization.record.MapRecord) RecordField(org.apache.nifi.serialization.record.RecordField) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) SchemaValidationResult(org.apache.nifi.serialization.record.validation.SchemaValidationResult) Record(org.apache.nifi.serialization.record.Record) MapRecord(org.apache.nifi.serialization.record.MapRecord) ValidationError(org.apache.nifi.serialization.record.validation.ValidationError) RecordSchema(org.apache.nifi.serialization.record.RecordSchema) SimpleRecordSchema(org.apache.nifi.serialization.SimpleRecordSchema) Test(org.junit.Test)

Example 8 with ValidationError

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());
    }
}
Also used : ValidationError(org.apache.nifi.serialization.record.validation.ValidationError)

Aggregations

ValidationError (org.apache.nifi.serialization.record.validation.ValidationError)8 Record (org.apache.nifi.serialization.record.Record)7 RecordSchema (org.apache.nifi.serialization.record.RecordSchema)7 SchemaValidationResult (org.apache.nifi.serialization.record.validation.SchemaValidationResult)7 ArrayList (java.util.ArrayList)5 LinkedHashMap (java.util.LinkedHashMap)5 SimpleRecordSchema (org.apache.nifi.serialization.SimpleRecordSchema)5 MapRecord (org.apache.nifi.serialization.record.MapRecord)5 RecordField (org.apache.nifi.serialization.record.RecordField)5 Test (org.junit.Test)5 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 FlowFile (org.apache.nifi.flowfile.FlowFile)1 SchemaNotFoundException (org.apache.nifi.schema.access.SchemaNotFoundException)1 SchemaValidationContext (org.apache.nifi.schema.validation.SchemaValidationContext)1 StandardSchemaValidator (org.apache.nifi.schema.validation.StandardSchemaValidator)1 MalformedRecordException (org.apache.nifi.serialization.MalformedRecordException)1 RecordReader (org.apache.nifi.serialization.RecordReader)1