NIFI-4080: Restored customValidate in ValidateCSV for schema property with no EL

Signed-off-by: Pierre Villard <pierre.villard.fr@gmail.com>

This closes #2226.
This commit is contained in:
Matthew Burgess 2018-02-06 11:47:33 -05:00 committed by Pierre Villard
parent ba192d22da
commit 16e56ccfca
2 changed files with 32 additions and 1 deletions

View File

@ -23,6 +23,7 @@ import java.io.OutputStream;
import java.io.Reader; import java.io.Reader;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
@ -40,6 +41,9 @@ import org.apache.nifi.annotation.documentation.CapabilityDescription;
import org.apache.nifi.annotation.documentation.Tags; import org.apache.nifi.annotation.documentation.Tags;
import org.apache.nifi.components.AllowableValue; import org.apache.nifi.components.AllowableValue;
import org.apache.nifi.components.PropertyDescriptor; import org.apache.nifi.components.PropertyDescriptor;
import org.apache.nifi.components.PropertyValue;
import org.apache.nifi.components.ValidationContext;
import org.apache.nifi.components.ValidationResult;
import org.apache.nifi.flowfile.FlowFile; import org.apache.nifi.flowfile.FlowFile;
import org.apache.nifi.logging.ComponentLog; import org.apache.nifi.logging.ComponentLog;
import org.apache.nifi.processor.AbstractProcessor; import org.apache.nifi.processor.AbstractProcessor;
@ -117,7 +121,7 @@ public class ValidateCsv extends AbstractProcessor {
+ allowedOperators.toString() + ". Note: cell processors cannot be nested except with Optional.") + allowedOperators.toString() + ". Note: cell processors cannot be nested except with Optional.")
.required(true) .required(true)
.expressionLanguageSupported(true) .expressionLanguageSupported(true)
.addValidator(StandardValidators.NON_EMPTY_VALIDATOR) .addValidator(StandardValidators.NON_EMPTY_EL_VALIDATOR)
.build(); .build();
public static final PropertyDescriptor HEADER = new PropertyDescriptor.Builder() public static final PropertyDescriptor HEADER = new PropertyDescriptor.Builder()
@ -209,6 +213,31 @@ public class ValidateCsv extends AbstractProcessor {
return properties; return properties;
} }
@Override
protected Collection<ValidationResult> customValidate(ValidationContext context) {
PropertyValue schemaProp = context.getProperty(SCHEMA);
String schema = schemaProp.getValue();
String subject = SCHEMA.getName();
if (context.isExpressionLanguageSupported(subject) && context.isExpressionLanguagePresent(schema)) {
return Collections.singletonList(new ValidationResult.Builder().subject(subject).input(schema).explanation("Expression Language Present").valid(true).build());
}
// If no Expression Language is present, try parsing the schema
try {
this.parseSchema(schema);
} catch (Exception e) {
final List<ValidationResult> problems = new ArrayList<>(1);
problems.add(new ValidationResult.Builder().subject(subject)
.input(schema)
.valid(false)
.explanation("Error while parsing the schema: " + e.getMessage())
.build());
return problems;
}
return super.customValidate(context);
}
public CsvPreference getPreference(final ProcessContext context, final FlowFile flowFile) { public CsvPreference getPreference(final ProcessContext context, final FlowFile flowFile) {
// When going from the UI to Java, the characters are escaped so that what you // When going from the UI to Java, the characters are escaped so that what you
// input is transferred over to Java as is. So when you type the characters "\" // input is transferred over to Java as is. So when you type the characters "\"

View File

@ -264,7 +264,9 @@ public class TestValidateCsv {
runner.setProperty(ValidateCsv.END_OF_LINE_CHARACTER, "\r\n"); runner.setProperty(ValidateCsv.END_OF_LINE_CHARACTER, "\r\n");
runner.setProperty(ValidateCsv.QUOTE_CHARACTER, "\""); runner.setProperty(ValidateCsv.QUOTE_CHARACTER, "\"");
runner.setProperty(ValidateCsv.HEADER, "false"); runner.setProperty(ValidateCsv.HEADER, "false");
runner.assertNotValid();
// We
runner.setProperty(ValidateCsv.SCHEMA, "RequireSubString(\"test\")"); runner.setProperty(ValidateCsv.SCHEMA, "RequireSubString(\"test\")");
runner.assertNotValid(); runner.assertNotValid();