import ch.codeblock.qrinvoice.model.validation.QrInvoiceValidator;
// ...
if(QrInvoiceValidator.create().isStringValid("Robert Schneider AG")) {
// string does only contain permitted characters
// ...
}
The simplest string validation looks as follows. It checks that only permitted characters are used in a string and just returns true or false.
import ch.codeblock.qrinvoice.model.validation.QrInvoiceValidator;
// ...
if(QrInvoiceValidator.create().isStringValid("Robert Schneider AG")) {
// string does only contain permitted characters
// ...
}
If you need more details, like which character(s) at which index in the string is invalid, the following approach provides them to you.
import ch.codeblock.qrinvoice.model.validation.CharacterValidationResult;
import ch.codeblock.qrinvoice.model.validation.InvalidCharacterSequence;
import ch.codeblock.qrinvoice.model.validation.QrInvoiceValidator;
import ch.codeblock.qrinvoice.model.validation.ValidationResult;
import java.util.List;
// ...
final CharacterValidationResult validationResult = QrInvoiceValidator.create().validateString("Invalid € sign");
if(validationResult.hasInvalidCharacters()) {
final String validationErrorSummary = validationResult.getSummary();
System.out.println(validationErrorSummary);
// access each InvalidCharacterSequence on its own
final List<InvalidCharacterSequence> invalidCharacterSequences = validationResult.getInvalidCharacterSequences();
for (final InvalidCharacterSequence invalidCharacterSequence : invalidCharacterSequences) {
System.out.println(invalidCharacterSequence.getErrorSummary());
System.out.println(invalidCharacterSequence.getInvalidCharSequenceStartIndex());
System.out.println(invalidCharacterSequence.getInvalidCharSequence());
}
// ...
}
// prints:
// Invalid character(s) '€' at index 8 with a length of 1 (context: 'lid <€> sig')
// 8
// €