Skip to content

Commit

Permalink
Added to field validation for field names. Checks for valid length an…
Browse files Browse the repository at this point in the history
…d valid SQL syntax.
  • Loading branch information
estherbuchwalter committed Nov 24, 2021
1 parent 881d465 commit 56d8f6e
Showing 1 changed file with 18 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.regex.Pattern;

@JsonTypeName(FixedwidthFormatPlugin.DEFAULT_NAME)
@JsonInclude(JsonInclude.Include.NON_DEFAULT)
Expand Down Expand Up @@ -168,10 +169,11 @@ public void validateFieldInput(){
List<Integer> fieldWidths = this.getFieldWidths();
List<String> fieldNames = this.getFieldNames();
List<TypeProtos.MinorType> fieldTypes = this.getFieldTypes();
int width = 0;
int prevIndexAndWidth = -1;

// Validate Field Name - Ensure field is not empty, no two fields have the same name, and field is valid SQL syntax
/* Validate Field Name - Ensure field is not empty, does not exceed maximum length,
is valid SQL syntax, and no two fields have the same name
*/
for (String name : this.getFieldNames()){
if (name.length() == 0){
throw UserException
Expand All @@ -180,6 +182,20 @@ public void validateFieldInput(){
.addContext("Plugin", FixedwidthFormatPlugin.DEFAULT_NAME)
.build(logger);
}
if (name.length() > 1024) {
throw UserException
.validationError()
.message("Exceeds maximum length of 1024 characters: " + name.substring(0, 1024))
.addContext("Plugin", FixedwidthFormatPlugin.DEFAULT_NAME)
.build(logger);
}
if (!Pattern.matches("[a-zA-Z]\\w*", name)) {
throw UserException
.validationError()
.message("Invalid input: " + name)
.addContext("Plugin", FixedwidthFormatPlugin.DEFAULT_NAME)
.build(logger);
}
if (uniqueNames.contains(name)){
throw UserException
.validationError()
Expand Down

0 comments on commit 56d8f6e

Please sign in to comment.