The data methods all work in fundamentally the same way - you pass in some
raw input as the first parameter and a default value as the second. If the first
parameter fulfils the criteria of the method, it return that otherwise it returns
the default value.
### _mixed_ setInt(_mixed_ input, _mixed_ def)
Ensures that the input is an integer. This can be either a string, or a
number. In reality, this pushes it to the JavaScript Number object
(which can be made to be a floating point number. However, this function
ensures that value that is returned it an integer. If you pass over
Number(1) or String(3.0), they are returned as Number(1) and Number(3).
However, if you pass in Number(1.2) or String(2.4), the default value
will be returned.
### _mixed_ setString(_mixed_ input, _mixed_ def[, _array_ values])
Ensures that the input is a string. If it is a number, this is cast
to a string. If you wish to specify a series of values, this can be done
by passing in some values.
Example
var values = [
'val1', 'val2', 'val3'
];
val1 = datautils.data.setString('val1', null, values); // 'val1'
val2 = datautils.data.setString('val4', null, values); // null
Validation
The model stuff primarily all works in the same way - if it passes the test it
returns true
, if it fails the test or the wrong input is entered it throws an
error.
Error Object
The error object is an extension of the default JavaScript Error object. It has
a maximum of three parameters - the message
(as the Error object has by default),
value
(the value passed in) and params
(an array of any other paramaters passed
in).
For instance, the function validation.greaterThan(8, 10);
throws an error
(because 8 is less than 10). err.message
equals VALUE_NOT_GREATER_THAN_TARGET,
err.value
= 8 and err.params
= [ 10 ].