Validation

The key aspect of Enum-types is validation. To illustrate this, take a look at the following example:


var WithoutEnum = Class({

    'public string payment_type' : null,

    // Overwrite the setter to perform some custom validation
    'public setPaymentType(string)': function(type) {
        // We somehow need to keep track of valid types...
        var accepted_types = ['PayPal', 'Visa', 'MasterCard', 'Maestro'];

        // Check if type is valid.
        if (accepted_types.indexOf(type) === -1) {
            throw new Error(type + ' is not a valid payment type.');
        }

        // ... Do things
    }
});

var we = new WithoutEnum();
we.setPaymentType('DirectDebit'); // Error: DirectDebit is not a valid payment type.

Using an Enum for the acceptable payment types, we don't have to program any logic when it comes to validation.

// Start by creating an Enum-type for acceptable payment types.
Enum('PaymentType', {
    PAYPAL     : 'PayPal',
    VISA       : 'Visa',
    MASTERCARD : 'MasterCard',
    MAESTRO    : 'Maestro'
});

// Define the class using the PaymentType enum
var WithEnum = Class({
    'public PaymentType payment_type' : null
});

var we = new WithEnum();
we.setPaymentType(PaymentType.PAYPAL); // OK
we.setPaymentType('MasterCard');       // OK
we.setPaymentType('DirectDebit');      // Error: setPaymentType: 'DirectDebit' is not a member of enum PaymentType.

contains

In some cases you want to do some manual validation if a value is a valid member of an Enum-type. This can be done using the contains method on the Enum-type, which returns true or false whether the given value exists within the Enum-type.

PaymentType.contains('PayPal');               // true
PaymentType.contains('Bitcoin');              // false
PaymentType.contains(PaymentType.MASTERCARD); // true

Custom validation example

When we take the setPaymentType method from the first example and validate it properly using an Enum-type, it would look something like this:

var Checkout = Class({

    // Declare the payment_type property
    'public PaymentType payment_type' : null,

    // Override the default setter for payment_type
    'public setPaymentType' : function (type) {

        // Is the given type a valid PaymentType?
        if (! PaymentType.contains(type)) {
            throw new Error('Invalid payment type "' + type + '" specified.');
        }

        this.payment_type = type;
    },

    // If you specify an enum as a method parameter type, it does the validation for you
    'public myMethod(PaymentType)' : function (type) {
        // Type has to be a valid payment type in order for this method to have been called
        this.payment_type = type;
    }

});

// Taking the same code as the example above:
var c = new Checkout();
c.setPaymentType(PaymentType.PAYPAL); // OK
c.setPaymentType('MasterCard');       // OK
c.setPaymentType('DirectDebit');      // Error: Invalid payment type "DirectDebit" specified.

c.myMethod(PaymentType.PAYPAL);       // OK
c.myMethod('MasterCard');             // OK
c.myMethod('DirectDebit');            // Error: Couldn't find a function handler to match: myMethod(string).