PageRenderTime 42ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/ext-4.1.0_b3/src/data/validations.js

https://bitbucket.org/srogerf/javascript
JavaScript | 148 lines | 41 code | 16 blank | 91 comment | 14 complexity | 522815e7410628796bb8216114a4892d MD5 | raw file
  1. /**
  2. * @author Ed Spencer
  3. *
  4. * This singleton contains a set of validation functions that can be used to validate any type of data. They are most
  5. * often used in {@link Ext.data.Model Models}, where they are automatically set up and executed.
  6. */
  7. Ext.define('Ext.data.validations', {
  8. singleton: true,
  9. /**
  10. * @property {String} presenceMessage
  11. * The default error message used when a presence validation fails.
  12. */
  13. presenceMessage: 'must be present',
  14. /**
  15. * @property {String} lengthMessage
  16. * The default error message used when a length validation fails.
  17. */
  18. lengthMessage: 'is the wrong length',
  19. /**
  20. * @property {Boolean} formatMessage
  21. * The default error message used when a format validation fails.
  22. */
  23. formatMessage: 'is the wrong format',
  24. /**
  25. * @property {String} inclusionMessage
  26. * The default error message used when an inclusion validation fails.
  27. */
  28. inclusionMessage: 'is not included in the list of acceptable values',
  29. /**
  30. * @property {String} exclusionMessage
  31. * The default error message used when an exclusion validation fails.
  32. */
  33. exclusionMessage: 'is not an acceptable value',
  34. /**
  35. * @property {String} emailMessage
  36. * The default error message used when an email validation fails
  37. */
  38. emailMessage: 'is not a valid email address',
  39. /**
  40. * @property {RegExp} emailRe
  41. * The regular expression used to validate email addresses
  42. */
  43. emailRe: /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/,
  44. /**
  45. * Validates that the given value is present.
  46. * For example:
  47. *
  48. * validations: [{type: 'presence', field: 'age'}]
  49. *
  50. * @param {Object} config Config object
  51. * @param {Object} value The value to validate
  52. * @return {Boolean} True if validation passed
  53. */
  54. presence: function(config, value) {
  55. if (value === undefined) {
  56. value = config;
  57. }
  58. //we need an additional check for zero here because zero is an acceptable form of present data
  59. return !!value || value === 0;
  60. },
  61. /**
  62. * Returns true if the given value is between the configured min and max values.
  63. * For example:
  64. *
  65. * validations: [{type: 'length', field: 'name', min: 2}]
  66. *
  67. * @param {Object} config Config object
  68. * @param {String} value The value to validate
  69. * @return {Boolean} True if the value passes validation
  70. */
  71. length: function(config, value) {
  72. if (value === undefined || value === null) {
  73. return false;
  74. }
  75. var length = value.length,
  76. min = config.min,
  77. max = config.max;
  78. if ((min && length < min) || (max && length > max)) {
  79. return false;
  80. } else {
  81. return true;
  82. }
  83. },
  84. /**
  85. * Validates that an email string is in the correct format
  86. * @param {Object} config Config object
  87. * @param {String} email The email address
  88. * @return {Boolean} True if the value passes validation
  89. */
  90. email: function(config, email) {
  91. return Ext.data.validations.emailRe.test(email);
  92. },
  93. /**
  94. * Returns true if the given value passes validation against the configured `matcher` regex.
  95. * For example:
  96. *
  97. * validations: [{type: 'format', field: 'username', matcher: /([a-z]+)[0-9]{2,3}/}]
  98. *
  99. * @param {Object} config Config object
  100. * @param {String} value The value to validate
  101. * @return {Boolean} True if the value passes the format validation
  102. */
  103. format: function(config, value) {
  104. return !!(config.matcher && config.matcher.test(value));
  105. },
  106. /**
  107. * Validates that the given value is present in the configured `list`.
  108. * For example:
  109. *
  110. * validations: [{type: 'inclusion', field: 'gender', list: ['Male', 'Female']}]
  111. *
  112. * @param {Object} config Config object
  113. * @param {String} value The value to validate
  114. * @return {Boolean} True if the value is present in the list
  115. */
  116. inclusion: function(config, value) {
  117. return config.list && Ext.Array.indexOf(config.list,value) != -1;
  118. },
  119. /**
  120. * Validates that the given value is present in the configured `list`.
  121. * For example:
  122. *
  123. * validations: [{type: 'exclusion', field: 'username', list: ['Admin', 'Operator']}]
  124. *
  125. * @param {Object} config Config object
  126. * @param {String} value The value to validate
  127. * @return {Boolean} True if the value is not present in the list
  128. */
  129. exclusion: function(config, value) {
  130. return config.list && Ext.Array.indexOf(config.list,value) == -1;
  131. }
  132. });