Search in sources :

Example 6 with SchemaValidationResult

use of org.apache.nifi.serialization.record.validation.SchemaValidationResult in project nifi by apache.

the class StandardSchemaValidator method verifyChildRecord.

private void verifyChildRecord(final DataType canonicalDataType, final Object rawValue, final DataType expectedDataType, final StandardSchemaValidationResult result, final RecordField field, final String fieldPrefix) {
    // Now that we have the 'canonical data type', we check if it is a Record. If so, we need to validate each sub-field.
    if (canonicalDataType.getFieldType() == RecordFieldType.RECORD) {
        if (!(rawValue instanceof Record)) {
            // sanity check
            result.addValidationError(new StandardValidationError(concat(fieldPrefix, field), rawValue, ValidationErrorType.INVALID_FIELD, "Value is of type " + rawValue.getClass().getName() + " but was expected to be of type " + expectedDataType));
            return;
        }
        final RecordDataType recordDataType = (RecordDataType) canonicalDataType;
        final RecordSchema childSchema = recordDataType.getChildSchema();
        final String fullChildFieldName = concat(fieldPrefix, field);
        final SchemaValidationResult childValidationResult = validate((Record) rawValue, childSchema, fullChildFieldName);
        if (childValidationResult.isValid()) {
            return;
        }
        for (final ValidationError validationError : childValidationResult.getValidationErrors()) {
            result.addValidationError(validationError);
        }
    }
}
Also used : RecordDataType(org.apache.nifi.serialization.record.type.RecordDataType) SchemaValidationResult(org.apache.nifi.serialization.record.validation.SchemaValidationResult) Record(org.apache.nifi.serialization.record.Record) ValidationError(org.apache.nifi.serialization.record.validation.ValidationError) RecordSchema(org.apache.nifi.serialization.record.RecordSchema)

Example 7 with SchemaValidationResult

use of org.apache.nifi.serialization.record.validation.SchemaValidationResult 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 8 with SchemaValidationResult

use of org.apache.nifi.serialization.record.validation.SchemaValidationResult 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 9 with SchemaValidationResult

use of org.apache.nifi.serialization.record.validation.SchemaValidationResult in project nifi by apache.

the class TestStandardSchemaValidator method testValidateCorrectSimpleTypesStrictValidation.

@Test
public void testValidateCorrectSimpleTypesStrictValidation() throws ParseException {
    final List<RecordField> fields = new ArrayList<>();
    for (final RecordFieldType fieldType : RecordFieldType.values()) {
        if (fieldType == RecordFieldType.CHOICE) {
            final List<DataType> possibleTypes = new ArrayList<>();
            possibleTypes.add(RecordFieldType.INT.getDataType());
            possibleTypes.add(RecordFieldType.LONG.getDataType());
            fields.add(new RecordField(fieldType.name().toLowerCase(), fieldType.getChoiceDataType(possibleTypes)));
        } else if (fieldType == RecordFieldType.MAP) {
            fields.add(new RecordField(fieldType.name().toLowerCase(), fieldType.getMapDataType(RecordFieldType.INT.getDataType())));
        } else {
            fields.add(new RecordField(fieldType.name().toLowerCase(), fieldType.getDataType()));
        }
    }
    final DateFormat df = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss.SSS");
    df.setTimeZone(TimeZone.getTimeZone("gmt"));
    final long time = df.parse("2017/01/01 17:00:00.000").getTime();
    final Map<String, Object> intMap = new LinkedHashMap<>();
    intMap.put("height", 48);
    intMap.put("width", 96);
    final RecordSchema schema = new SimpleRecordSchema(fields);
    final Map<String, Object> valueMap = new LinkedHashMap<>();
    valueMap.put("string", "string");
    valueMap.put("boolean", true);
    valueMap.put("byte", (byte) 1);
    valueMap.put("char", 'c');
    valueMap.put("short", (short) 8);
    valueMap.put("int", 9);
    valueMap.put("bigint", BigInteger.valueOf(8L));
    valueMap.put("long", 8L);
    valueMap.put("float", 8.0F);
    valueMap.put("double", 8.0D);
    valueMap.put("date", new Date(time));
    valueMap.put("time", new Time(time));
    valueMap.put("timestamp", new Timestamp(time));
    valueMap.put("record", null);
    valueMap.put("array", null);
    valueMap.put("choice", 48L);
    valueMap.put("map", intMap);
    final Record record = new MapRecord(schema, valueMap);
    final SchemaValidationContext validationContext = new SchemaValidationContext(schema, false, true);
    final StandardSchemaValidator validator = new StandardSchemaValidator(validationContext);
    final SchemaValidationResult 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) ArrayList(java.util.ArrayList) Time(java.sql.Time) Timestamp(java.sql.Timestamp) Date(java.sql.Date) LinkedHashMap(java.util.LinkedHashMap) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) DataType(org.apache.nifi.serialization.record.DataType) SchemaValidationResult(org.apache.nifi.serialization.record.validation.SchemaValidationResult) Record(org.apache.nifi.serialization.record.Record) MapRecord(org.apache.nifi.serialization.record.MapRecord) RecordFieldType(org.apache.nifi.serialization.record.RecordFieldType) SimpleDateFormat(java.text.SimpleDateFormat) RecordSchema(org.apache.nifi.serialization.record.RecordSchema) SimpleRecordSchema(org.apache.nifi.serialization.SimpleRecordSchema) Test(org.junit.Test)

Aggregations

Record (org.apache.nifi.serialization.record.Record)9 RecordSchema (org.apache.nifi.serialization.record.RecordSchema)9 SchemaValidationResult (org.apache.nifi.serialization.record.validation.SchemaValidationResult)9 ArrayList (java.util.ArrayList)7 LinkedHashMap (java.util.LinkedHashMap)7 SimpleRecordSchema (org.apache.nifi.serialization.SimpleRecordSchema)7 MapRecord (org.apache.nifi.serialization.record.MapRecord)7 RecordField (org.apache.nifi.serialization.record.RecordField)7 ValidationError (org.apache.nifi.serialization.record.validation.ValidationError)7 Test (org.junit.Test)7 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 Date (java.sql.Date)1 Time (java.sql.Time)1 Timestamp (java.sql.Timestamp)1 DateFormat (java.text.DateFormat)1 SimpleDateFormat (java.text.SimpleDateFormat)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 FlowFile (org.apache.nifi.flowfile.FlowFile)1