The SDK provides validators that can be used to ensure that information entered by the user is valid.

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 PTKAddressDetails).

They adopt the PTKModelValidator protocol which defines the validate method that returns a list of PTKValidationFailure objects, one for each property that has an error.

The following model validators are available:

PTKAddressDetailsValidator

This is used to validate the PTKAddressDetails object. Since address formats differ by country the context is set during initialisation of the validator.
There are two address formats currently supported, namely UK and China.

Property validators are used to:

  • PTKAliasValidator - to check the alias.
  • PTKNameValidator - to check the first name, last name and county.
  • PTKPostcodeValidator - to check that the post code is valid for the selected country.
  • PTKAddressLineValidator - to check the State, City, County, Country, line1 and line2 are valid for the selected country.
  • PTKCNPostcodeValidator - to check that a Chinese postcode is valid.
  • PTKUKPostcodeValidator - to check thet a UK postcode is valid.

The following usage example shows how to use the PTKAddressDetailsValidator:

// Values obtained from the user have been stored in addressDetails 
- (void)validateAddressWithDetails:(PTKAddressDetails *)addressDetails
{
    PTKAddressDetailsValidator *addressValidator = [[PTKAddressDetailsValidator alloc] init];
    NSArray *errors = [addressValidator validate:addressDetails];
    
    if (errors != nil) {
        for (PTKValidationFailure *validationFailure in errors) {
            NSString *propertyName = validationFailure.propertyName;
            PTKValidationError validationError = validationFailure.errorCode;
            // Display validation error
        }
    } else {
        // No issues found while validating the address details
    }
}

PTKProfileDetailsValidator

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

Property validators are used to:

  • PTKEmailValidator - to check the email address.
  • PTKMobileNumberValidator - to check that the mobile number is valid for the selected country.
  • PTKPasscodeValidator - to check that the passcode is valid.
  • PTKNameValidator - to check the first and last name are in a valid format.
  • PTKTitleValidator - to check if the title is valid.

The following usage example shows how to use the PTKProfileDetailsValidator:

- (void)validateProfileWithDetails:(PTKProfileDetails *)profileDetails
{
    PTKProfileDetailsValidator *profileDetailsValidator = [[PTKProfileDetailsValidator alloc] init];
    NSArray *errors = [profileDetailsValidator validate:profileDetails];
    
    if (errors != nil) {
        for (PTKValidationFailure *validationFailure in errors) {
            NSString *propertyName = validationFailure.propertyName;
            PTKValidationError validationError = validationFailure.errorCode;
            // Display validation error
        }
    } else {
        // No issues found while validating the profile details
    }
}

PTKPaymentMethodDetailsValidator

This is used to validate the PTKPaymentMethodDetails object.

Property validators are used to:

  • CardHolderNameValidator - to check the card holder name.
  • PTKCreditCardNumberValidator - to check that the card number is valid.
  • PTKExpiryDateValidator - to check that the expiry date is valid.
  • PTKIssueNumberValidator - to check the issue number based on the card issuer rules.
  • PTKValidFromDateValidator - to check the first and last name are in a valid format.
  • PTKNameValidator - to check if the first and last names are less that 256 characters and in the correct format

The following usage example shows how to use the PTKPaymentMethodDetailsValidator:

- (void)validatePaymentMethodWithDetails:(PTKPaymentMethodDetails *)paymentMethodDetails
{
    PTKPaymentMethodDetailsValidator *paymentMethodValidator = [[PTKPaymentMethodDetailsValidator alloc] init];
    NSArray *errors = [paymentMethodValidator validate:paymentMethodDetails];
    
    if (errors != nil) {
        for (PTKValidationFailure *validationFailure in errors) {
            NSString *propertyName = validationFailure.propertyName;
            PTKValidationError validationError = validationFailure.errorCode;
            // Display validation error
        }
    } else {
        // No issues found while validating the payment method details
    }
}

For more details each of these model validators please review the [reference]({{site.baseurl}}/tag-mobile-sdks/0.9.8/refdocs/IOS/protocol_p_t_k_model_validator-p.html){:target="_blank"} documentation



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 adopt the PTKPropertyValidator protocol which defines a method called validateValue which returns a single PTKValidationError.

The following property validators are available:

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

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

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

PTKTitleValidator: Checks if title is not an empty string.

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

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

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

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

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

PTKPostcodeValidator: Provides logic for country specific post code validators to check if the postcode conforms to a supplied format.

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

PTKCardDateValidator: Provides logic for card date validators. checks if a YearMonth is between a specified minimum and maximum date range.

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

PTKExpiryDateValidator: Checks if expiry date is valid.

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

PTKCreditCardNumberValidator: 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.

PTKDecimalValueValidator: Provides logic for decimal values. checks if greater than the minimum and less than the maximum value.

PTKDonationAmountValidator: Checks if donation amount is greater than zero.

PTKIntValueValidator: Provides logic for int values. checks if greater than the minimum and less than the maximum value.

Here is an example of using one of the validators:

PTKAddressLineValidator *requiredAddressLineValidator = [PTKAddressLineValidator addressLineValidatorWithRequired:YES];
PTKValidationError error = PTKValidationErrorNone;
error = [requiredAddressLineValidator validateValue:input.state];
if (stateError != PTKValidationErrorNone) {
	// handle error
}

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 property validators are available:

PTKFormatValidator Checks if a input string matches the specified regular expression.

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

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

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

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

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

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

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

PTKNotEmptyValidator: Checks if the input is an empty string.

PTKNotNullValidator: Checks if input object is not null.

Here is an example of using one of the validators:

// Validate the the supplied quantity
</pre>PTKMaxIntValueValidator *maxValueValidator = [PTKMaxIntValueValidator validatorWithMaxValue:1000];
if (!maxValueValidator isValid:input.quantity) {
	//handle error
};



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