-
Notifications
You must be signed in to change notification settings - Fork 8
Validation
ybonnel edited this page Aug 13, 2012
·
3 revisions
In this page, you will find all informations for the usage of validation in CsvEngine.
@CsvFile
public class DogValid {
// The column "name" is mandatory.
@CsvColumn(value = "name", mandatory = true)
private String name;
// The column "race" is validate with a specific adaptor (ValidatorRace).
@CsvValidation(ValidatorRace.class)
@CsvColumn("race")
private String race;
// The column proprietary has two validator
// ValidatorSize to validate proprietary on size of it :
// minimum : 2 characters,
// maximum : 30 characters.
// ValidatorRegExp to validate proprietary with a regexp :
// the proprietary must contain a list of words separate with a whitespace,
// each word begin with a upper characters and and follow by at least 1 lower characters.
@CsvValidations({
@CsvValidation(value = ValidatorSize.class, params = {
@CsvParam(name = ValidatorSize.PARAM_MIN_SIZE, value = "2"),
@CsvParam(name = ValidatorSize.PARAM_MAX_SIZE, value = "30")
}),
@CsvValidation(value = ValidatorRegExp.class, params = {
@CsvParam(name = ValidatorRegExp.PARAM_PATTERN,
value = "\\p{javaUpperCase}\\p{javaLowerCase}+(\\p{javaWhitespace}\\p{javaUpperCase}\\p{javaLowerCase}+)*")
})
})
@CsvColumn("proprietary")
private String proprietary;
@Override
public String toString() {
return "DogValid{" +
"name='" + name + '\'' +
", race='" + race + '\'' +
", proprietary='" + proprietary + '\'' +
'}';
}
}@CsvFile
public class DogValid {
// The column "name" is mandatory.
@CsvColumn(value = "name", mandatory = true)
private String name;
// The column "race" is validate with a specific adaptor (ValidatorRace).
@CsvValidation(ValidatorRace.class)
@CsvColumn("race")
private String race;
// The column proprietary has two validator
// ValidatorSize to validate proprietary on size of it :
// minimum : 2 characters,
// maximum : 30 characters.
// ValidatorRegExp to validate proprietary with a regexp :
// the proprietary must contain a list of words separate with a whitespace,
// each word begin with a upper characters and and follow by at least 1 lower characters.
@CsvValidations({
@CsvValidation(value = ValidatorSize.class, params = {
@CsvParam(name = ValidatorSize.PARAM_MIN_SIZE, value = "2"),
@CsvParam(name = ValidatorSize.PARAM_MAX_SIZE, value = "30")
}),
@CsvValidation(value = ValidatorRegExp.class, params = {
@CsvParam(name = ValidatorRegExp.PARAM_PATTERN,
value = "\\p{javaUpperCase}\\p{javaLowerCase}+(\\p{javaWhitespace}\\p{javaUpperCase}\\p{javaLowerCase}+)*")
})
})
@CsvColumn("proprietary")
private String proprietary;
@Override
public String toString() {
return "DogValid{" +
"name='" + name + '\'' +
", race='" + race + '\'' +
", proprietary='" + proprietary + '\'' +
'}';
}
}private static void readIncorrectLine() {
// With an incorrect line
// Not name,
// Bad race
// Too long proprietary
String incorrectLine = ",Bad Race,Bad proprietary";
InputStream stream = new StringStream(HEADER + "\n" + incorrectLine);
System.out.println("**************");
System.out.println("validation with an incorrect line");
System.out.println("**************");
try {
engine.parseInputStream(stream, DogValid.class).getObjects();
} catch (CsvErrorsExceededException errors) {
System.out.println("Exception message : " + errors.getMessage());
for (Error error : errors.getErrors()) {
System.out.println("CsvLine : " + error.getCsvLine());
for (String message : error.getMessages()) {
System.out.println("\t" + message);
}
}
}
System.out.println();
}You can see samples in CsvEngineSample.