Simple Validator

Simply Validating

Validation conjures up thoughts of input form validation, but validation could also pop up elsewhere when there is user input such as the case for parameters in jQuery plugins.

The following article will describe a build-up of a modular validate function.

The validation will perform as follows

Format and Example
1
2
//validate(inputData, attr, attrRule, againstValue);
ex: validate(10, number, greaterThan, 11);

The example function will determine whether 10 as a number is greater than 11, which turns out to be false. The output of validate will be a string that states the error.

We start off with the validate variable as an immediately-invoking function with the variables that are to be accepted in the validate function

Variables Accepted
1
2
3
var validate = (function() {
var inputData, againstValue, attr, attrRule;
});

The validate function will have the blocks within

General Function Layout
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
var validate = (function() {
var inputData, againstValue, attr, attrRule;

//Validation Messages
var validationMessage = {};

//Validation Definitions
var validationDefinitions = {};

//Run Check
var validationCheck = function() {};

//Input Gather
return function() {

};
});

Accept Variables Input

The ‘Input Gather’ function will expose the arguments to the variables that are open within the validate function. The main comparision is done with the validationDefinition function, before its boolean outcome is handed off and check by validationCheck.

Input Gather
1
2
3
4
5
6
7
8
9
10
11
//Start off the validation
return function() {
var args = arguments;
attr = args[1];
attrRule = args[2];
inputData = args[0];
againstValue = args[3];

//Kicks off the validation check with supplied data
return validationCheck(validationDefinitions[attr][attrRule]());
};

Validation Rules

The validationDefinitions variable is an object that has a mapping of attr: {attrRule: function(){}}. The actual validation process is done by calling upon the attrRule definition function.

Validation Definitions
1
2
3
4
5
6
7
8
9
//Validation patterns
//Passed-in data are compared against the desired value (againstValue)
var validationDefinitions = {
number: {
greaterThan: function() {
return inputData > againstValue;
}
}
};

Determining Validness

The result of the validation helps determine if a validation message needs to be sent

Validation Check
1
2
3
4
5
6
7
//Run the checks against the validation patterns
var validationCheck = function(passedCheck) {
//Only generate a message value was not validated
if(!passedCheck) {
return validationMessage[attr][attrRule]();
}
};

Noting the Validation Error

The output of an invalid data input will result in a message being return.

Validation Messages
1
2
3
4
5
6
7
8
9
//Determine which message type should be used
var validationMessage = {
number: {
//Generate the message for a specific attrRule
greaterThan: function() {
return inputData + ' is not greater than ' + againstValue;
}
}
};

All Together

The validate function is complete on the bottom. More validation definitions and messages be added in the proper blocks.

Complete Validation Function
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
var validate = (function() {  

//Make these variables accessible to all functions inside validator
var inputData, againstValue, attr, attrRule;

//Determine which message type should be used
var validationMessage = {
number: {
//Generate the message for a specific attrRule
greaterThan: function() {
return inputData + ' is not greater than ' + againstValue;
}
/*
lessThan: function() {
return inputData + ' is not less than ' + againstValue;
}
//More messages here
*/

}
};

//Validation patterns
//Passed-in data are compared against the desired value (againstValue)
var validationDefinitions = {
number: {
greaterThan: function() {
return inputData > againstValue;
}
/*
,lessThan: function() {
return inputData < againstValue;
}
//More rules here
*/

}
};

//Run the checks against the validation patterns
var validationCheck = function(passedCheck) {
//Only generate a message value when not validated
if(!passedCheck) {
return validationMessage[attr][attrRule]();
}
};

//Start off the validation
return function() {
var args = arguments;
attr = args[1];
attrRule = args[2];
inputData = args[0];
againstValue = args[3];

//Kicks off the validation check with supplied data
return validationCheck(validationDefinitions[attr][attrRule]());
};
})();

//Usage: validate(inputData, 'number', 'greaterThan', 5);
console.log(validate(4, 'number', 'greaterThan', 5));
//Output: '4 is not greater than 5'

Online Demo