Skip to content

Custom property validator

rsamec edited this page Jul 24, 2014 · 1 revision

How to create a custom property validator.

To create a custom validation rule you have to implement IPropertyValidator interface.

        export interface IPropertyValidator{
            isAcceptable(s: any): boolean;
            tagName?:string;
        }

Notice, that inteface has only one method required (isAccepable(any)->boolean).

Let's create validator that enables to compare two dates. We want to parametrize the validator with operators less, equal or greater.

First we create enumaration for compare operators that enables us to parametrize validator.

    export enum CompareOperator {
        //must be less than
        LessThan,

        //cannot be more than
        LessThanEqual,

        //must be the same as
        Equal,

        //must be different from
        NotEqual,

        //cannot be less than
        GreaterThanEqual,

        //must be more than
        GreaterThan
    }

Next we import dependencies - we need only some utilities.

    import moment = require("moment");
    import moment = require("underscore");

Now we create the validator and implements the method is isAcceptable(any)->boolean.

class DateCompareValidator implements Validation.IPropertyValidator{

    public isAcceptable(s:any){
        var isValid = false;

        //if value is not date -> there is nothing to compare
        if (!_.isDate(s)) return false;

        //if date to compare is not specified - defaults to compare against now
        if (this.CompareTo == undefined) Date.now();

        var now = moment(this.CompareTo);
        var then =  moment(s);

        var diffs:number = then.diff(now);
        if (this.IgnoreTime) diffs = moment.duration(diffs).days();

        if (diffs < 0) {
            isValid = this.CompareOperator == Validation.CompareOperator.LessThan
                || this.CompareOperator == Validation.CompareOperator.LessThanEqual
                || this.CompareOperator == Validation.CompareOperator.NotEqual;
        }
        else if (diffs > 0) {
            isValid = this.CompareOperator == Validation.CompareOperator.GreaterThan
                || this.CompareOperator == Validation.CompareOperator.GreaterThanEqual
                || this.CompareOperator == Validation.CompareOperator.NotEqual;
        }
        else {
            isValid = this.CompareOperator == Validation.CompareOperator.LessThanEqual
                || this.CompareOperator == Validation.CompareOperator.Equal
                || this.CompareOperator == Validation.CompareOperator.GreaterThanEqual;
        }
        return isValid;
    }

    /**
     * Set the time of compare between passed date and CompareTo date.
     */
    public CompareOperator:Validation.CompareOperator;

    /**
     * The datetime against the compare is done.
     * If CompareTo is not set, then comparison is done against actual datetime.
     */
    public CompareTo:Date;

    /**
     * It forces to ignore time part of date by date compare.
     * @type {boolean}
     */
    public IgnoreTime:boolean = false;

    tagName = 'dateCompare';
}

export = DateCompareValidator;

Use custom validation rule

Let`s use the validator

    import dateCompareValidator = require('DateCompareValidator');

    var validator = new dateCompareValidator();
    validator.CompareTo = new Date(2000,2,2);
    validator.CompareOperator = Validation.CompareOperator.LessThanEqual;

    //less dates -> return true
    var result = validator.isAcceptable(2000,1,1)

    //greater dates -> return false
    var result = validator.isAcceptable(2000,2,3)