The SDK provides validators that can be used to ensure that information entered by the user is valid.
All validators can be found in the package com.powatag.android.sdk.validators

There are three types of validators:

Model Validators

These validators are used to ensure that all properties of a model are valid (e.g. validating AddressDetails).

They implement the ModelValidator interface which defines the validate method that returns a list of ValidationFailure objects, one for each property that has an error.

The following model validators are available:

AddressDetailsValidator

This is used to validate the AddressDetails object. Since address formats differ by country the context is set during initialisation of the validator through the use of the CountryAwareAddressDetailContext which will provide country specific property validators.
There are three address formats currently supported, namely United Kingdom, Denmark and Norway..

Property validators are used to:

  • AliasValidator - to check the alias.
  • NameValidator - to check the first name, last name and county.
  • PostcodeValidator - to check that the post code is valid for the selected country.
  • CountryValidator - to check the country.
  • AddressLineValidator - to check the State, City, line1 and line2 are valid for the selected country.
  • CountyValidator - to check the county is valid for the selected country.
  • NameValidator - to check the first and last name are in a valid format.

The following usage example shows how to use the AddressDetailsValidator:

// Values obtained from the user have been stored in addressDetails 
//set the country to validate for
AddressDetailsValidator addressDetailsValidator = new AddressDetailsValidator(CountryAwareAddressDetailContext.UK); //set the country to validate for
List<ValidationFailure> errors = addressDetailsValidator.validate(addressDetails);
if(errors != null){
    for (int s = 0; s < errors.size(); s++) {
		ValidationFailure validationFailure = errors.get(s);
		String property = validationFailure.getPropertyName();
		ValidationError errorCode = validationFailure.getErrorCode();
		// Display validation to user and obtain updated value
	}
} else {
	// No issues found while validating the address details
}

ProfileDetailsValidator

This is used to validate the ProfileDetails object. During instantiation the country code is set so that the country specific property validators are used.

Property validators are used to:

  • EmailValidator - to check the email address.
  • MobileNumberValidator - to check that the mobile number is valid for the selected country.
  • PasscodeValidator - to check that the passcode is valid.
  • NameValidator - to check the first and last name are in a valid format.

The following usage example shows how to use the ProfileDetailsValidator:

// The user's country is obtained and set in userCountry
ProfileDetailsValidator profileDetailsValidator = new ProfileDetailsValidator(userCountry);
// Use the profileDetails obtain in an earlier step
List errors = profileDetailsValidator.validate(profileDetails);
if (!errors.isEmpty()) {
	for (int s = 0; s < errors.size(); s++) {
		ValidationFailure validationFailure = errors.get(s);
		String property = validationFailure.getPropertyName();
		ValidationError errorCode = validationFailure.getErrorCode();
		// Display validation to user and obtain an updated value
	}
} else {
	// No issues found while validating the profile details
}

PaymentMethodDetailsValidator

This is used to validate the PaymentMethodDetails object.

Property validators are used to:

  • CardHolderNameValidator - to check the card holder name.
  • CardNumberValidator - to check that the card number is valid.
  • ExpiryDateValidator - to check that the expiry date is valid.
  • IssueNumberValidator - to check the issue number based on the card issuer rules.
  • ValidFromDateValidator - to check the first and last name are in a valid format.

The following usage example shows how to use the PaymentMethodDetailsValidator:

PaymentMethodDetailsValidator paymentMethodDetailsValidator = new PaymentMethodDetailsValidator();
List<ValidationFailure> errors = paymentMethodDetailsValidator.validate(paymentMethodDetails);
if (errors != null) {
	for (int s = 0; s < errors.size(); s++) {
		ValidationFailure validationFailure = errors.get(s);
		String property = validationFailure.getPropertyName();
		ValidationError errorCode = validationFailure.getErrorCode();
		// Display validation to user and obtain an updated value
	}
} else {
	// No issues found while validating the payment details
}



Property Validators

These validators are used to ensure that a single property is valid, often using multiple low level validators (e.g. validating the address line1 property).

They implement the PropertyValidator interface which defines a method called validate which returns a single ValidationError.

The following property validators are available:

TextValidator: Checks if string is greater than the minimum and less than the maximum lengths, and if it matches a specified format.

IssueNumberValidator: Checks if the card issue number is 4 digits long and that only digits 0-9 are used.

AliasValidator: Checks if the alias is less than 256 characters long.

TitleValidator: Checks if title is not an empty string.

EmailValidator: Checks if an email address is in the correct format.

CardHolderNameValidator: Checks if card holder name is less than 256 characters and in the correct format.

AddressLineValidator: Checks if an address line is less than 256 characters.

CountyValidator: Checks if county is less than 256 characters.

CvvValidator: Checks if CVV is correct length for the card issuer and that only digits 0-9 are used.

NameValidator: Checks if the name is less than 256 characters and is in the correct format.

CountryValidator: Checks if country is 2 characters in length.

PostcodeValidator: An abstract class that provides logic for country specific post code validators to check if the postcode conforms to a supplied format.

UkPostcodeValidator: Checks if a postcode is valid based on the UK Format.

DenmarkPostcodeValidator: Checks if a postcode is in a valid format for Denmark.

NorwayPostcodeValidator: Checks if a postcode is in a valid format for Norway.

CardDateValidator: An abstract class that provides logic for card date validators. checks if a YearMonth is between a specified minimum and maximum date range.

ValidFromDateValidator: Checks if "Valid From" date is valid.

ExpiryDateValidator: Checks if expiry date is valid.

MobileNumberValidator: Checks if a mobile number is valid for a specific country.

PasscodeValidator: Checks if the passcode is 6 digits long.

CardNumberValidator: Checks if the card number is not null, is greater than the minimum and less than the maximum value and in the correct format based on the card issuer.

DecimalValueValidator: An abstract class providing logic for decimal values. checks if greater than the minimum and less than the maximum value.

DonationAmountValidator: Checks if donation amount is greater than zero.

IntValueValidator: An abstract class providing logic for int values. checks if greater than the minimum and less than the maximum value.

Here is a usage example for one of the property validators:

// Set the conditions for the validator
TextValidator textValidator = new TextValidator(isRequired, minLength, maxLegnth, format);
// Validate the the supplied string
ValidationError error = textValidator.validate(input.getDescription());
if (error != null) {
	// handle error
} else {
	// No issues found while validating the string
}

For more details on the property validators please review the API documentation.

Validators

These are low level validators which are used to check aspects of a data type (e.g. NotNull, MinLength).

They implement the Validator interface which defines methods isValid and getError.

The following validators are available:

FormatValidator: Checks if a input string matches the specified regular expression.

MaxLengthValidator: Checks whether the input string is not longer than the maximum length.

MinDecimalValueValidator: Checks that the input value is not less than the minimum allowed decimal value.

MinIntValueValidator: Checks that the input value is not less than the minimum allowed int value.

MinLengthValidator: Checks whether the input string is not shorter than the minimum length.

LuhnValidator: Checks a credit card number using the Luhn algorithm.

MaxDecimalValueValidator: Checks that an input value is not greater than the maximum allowed decimal value.

MaxIntValueValidator: Checks that an input value is not greater than the maximum allowed int value.

NotEmptyValidator: Checks if the input is an empty string.

NotNullValidator: Checks if input object is not null.

Here is a usage example for one of the validators:

MaxIntValueValidator maxIntValueValidator = new MaxIntValueValidator(1000);
// Validate the the supplied int number 
if (!maxIntValueValidator.isValid(input.getQuantity()) {
	ValidationError error = maxIntValueValidator.getError();
	// handle error		
}

For more details on the property validators please review the API documentation.