I'm using the fixed length parser to import files and java beans with annotations as mapping classes. How can I define a field as required? Couldn't find an annotation. Could I implement a custom converter for such a validation?
UPDATE:
Version 2.7.0 introduces the @Validate
annotation. By default it disallows null or blank values.
Use it like this:
public static class A {
@Parsed(index = 0)
@Validate(nullable = true)
public String nullNotBlank;
@Parsed(index = 1)
@Validate(oneOf = {"a", "b"})
public String aOrB;
@Parsed(index = 2)
@Validate(oneOf = {"a"}, noneOf = "b")
public String aNotB;
@Parsed(index = 3)
@Validate(allowBlanks = true, oneOf = {"a", "b"})
public String aOrBOrNull;
}
Another way would be to put the annotation in your setter method.
@Parsed
public void setMyField(String data) {
if(data == null){
throw new IllegalArgumentException("MyField can't be null");
}
this.myField = data;
}
If you have this requirement for many different fields, you can create a custom conversion. I've updated the tutorial yesterday so have look there.
More specifically, check the Using your own conversions in annotations section of the tutorial.
To avoid repeating adding such annotation everywhere refer to section Avoiding repetitive annotations in the same page.
Hope this helps.