Venciity

validations and extends

Nov 14th, 2014
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. // ---------- extends ----------
  3. Object.prototype.extends = function (parent) {
  4.     this.prototype = Object.create(parent.prototype);
  5.     this.prototype.constructor = this;
  6. }
  7.  
  8. // ---------- is Number ----------
  9. Number.prototype.validateIsNumber = function (value, variableName, minValue, maxValue) {
  10.     if (typeof (value) != 'number') {
  11.         throw new Error(variableName + " should be a number!");
  12.     }
  13. }
  14.  
  15. // ---------- is Integer ----------
  16. Number.prototype.isInteger = function isInteger(x) {
  17.     return ((x ^ 0) === x);
  18. }
  19.  
  20. // ---------- validate integer range ----------
  21. Number.prototype.validateIntegerRange = function (value, variableName, minValue, maxValue) {
  22.     if (typeof (value) != 'number') {
  23.         throw new Error(variableName + " should be a number!");
  24.     }
  25.  
  26.     if (value !== parseInt(value, 10)) {
  27.         throw new Error(variableName + " should be integer!");
  28.     }
  29.  
  30.     if (value < minValue || value > maxValue) {
  31.         throw new Error(variableName + " should be integer in the range [" + minValue + "..." + maxValue + "]!");
  32.     }
  33. }
  34.  
  35. // ---------- validate non-empty string ----------
  36. String.prototype.validateNonEmptyString = function (value, variableName) {
  37.     if (typeof (value) != 'string' || !value) {
  38.         throw new Error(variableName + " should be non-empty string!");
  39.     }
  40. }
  41.  
  42. // ---------- validate boolean ----------
  43. Boolean.prototype.validateBoolean = function (value, variableName) {
  44.     if (typeof (value) != 'boolean') {
  45.         throw new Error(variableName + " should be a boolean value!");
  46.     }
  47. }
  48.  
  49. // ---------- validate non-empty object ----------
  50. Object.prototype.validateNonEmptyObject = function (value, className, variableName) {
  51.     if (! (value instanceof className)) {
  52.         throw new Error(variableName + " should be non-empty " + className.prototype.constructor.name + "!");
  53.     }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment