miscellaneousRules.test.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. var testRules = require('./util/testRules.js');
  2. describe('miscellaneous rules', function() {
  3. describe('isInteger', function() {
  4. it ('should fail for strings', function() {
  5. return testRules({
  6. isInteger: true
  7. }, 2, '2');
  8. });
  9. it ('should fail for floats', function() {
  10. return testRules({
  11. isInteger: true
  12. }, 2, 2.5);
  13. });
  14. it ('should not allow nulls', function() {
  15. return testRules({
  16. isInteger: true
  17. }, 2, null);
  18. });
  19. });
  20. describe('notEmptyString', function() {
  21. it('should no allow nulls', function() {
  22. return testRules({
  23. isNotEmptyString: true
  24. }, 'sfsa', null);
  25. });
  26. });
  27. describe('max/min', function() {
  28. it(' should support "max" rule ', function() {
  29. return testRules({
  30. max: 3
  31. }, 2, 5);
  32. });
  33. it(' should support "min" rule ', function() {
  34. return testRules({
  35. min: 3
  36. }, 5, 2);
  37. });
  38. describe(' should support both "max" and "min" rules at the same time ', function() {
  39. it('with a val < min', function() {
  40. return testRules({
  41. min: 3,
  42. max: 5
  43. }, 4, 2);
  44. });
  45. it('with a val > max', function() {
  46. return testRules({
  47. min: 3,
  48. max: 5
  49. }, 4, 6);
  50. });
  51. });
  52. it('should not allow `null` values', function() {
  53. return testRules({
  54. min: 3,
  55. max: 5
  56. }, 4, null);
  57. });
  58. });
  59. describe('maxLength/minLength', function() {
  60. it(' should support "maxLength" rule ', function() {
  61. return testRules({
  62. maxLength: 3
  63. }, 'abc', 'abcde');
  64. });
  65. it(' should support "minLength" rule ', function() {
  66. return testRules({
  67. minLength: 3
  68. }, 'abc', 'ab');
  69. });
  70. describe(' should support both "maxLength" and "minLength" rules at the same time ', function() {
  71. it('with a val < minLength', function() {
  72. return testRules({
  73. minLength: 3,
  74. maxLength: 5
  75. }, 'abcd', 'ab');
  76. });
  77. it('with a val > maxLength', function() {
  78. return testRules({
  79. minLength: 3,
  80. maxLength: 5
  81. }, 'abcd', 'abcdef');
  82. });
  83. });
  84. it('should not allow null values', function() {
  85. return testRules({
  86. minLength: 3,
  87. maxLength: 5
  88. }, 'abcd', null);
  89. });
  90. it('should allow empty string values', function() {
  91. return testRules({
  92. minLength: 3,
  93. maxLength: 5
  94. }, '', 'abcdef');
  95. });
  96. });
  97. describe('isURL', function() {
  98. it('should support "isURL" rule with no options', function() {
  99. return testRules({
  100. isURL: true
  101. }, 'http://sailsjs.org', 'sailsjs');
  102. });
  103. it('should support "isURL" rule with options', function() {
  104. return testRules({
  105. isURL: {
  106. require_protocol: true//eslint-disable-line camelcase
  107. }
  108. }, 'http://sailsjs.org', 'www.sailsjs.org');
  109. });
  110. });
  111. describe('isBefore/isAfter date', function() {
  112. it(' should support "isBefore" rule when validating Date instances (Date) vs. Date', function() {
  113. return testRules({
  114. isBefore: new Date()
  115. }, new Date(Date.now() - 100000), new Date(Date.now() + 1000000));
  116. });
  117. it(' should support "isBefore" rule when validating Date instances (Date) vs. number', function() {
  118. return testRules({
  119. isBefore: Date.now()
  120. }, new Date(Date.now() - 100000), new Date(Date.now() + 1000000));
  121. });
  122. it(' should support "isBefore" rule when validating js timestamps (number) vs. number', function() {
  123. return testRules({
  124. isBefore: Date.now()
  125. }, (new Date(Date.now() - 100000)).getTime(), (new Date(Date.now() + 1000000)).getTime());
  126. });
  127. it(' should support "isBefore" rule when validating js timestamps (number) vs. Date', function() {
  128. return testRules({
  129. isBefore: new Date()
  130. }, (new Date(Date.now() - 100000)).getTime(), (new Date(Date.now() + 1000000)).getTime());
  131. });
  132. it(' should support "isBefore" rule when validating JSON timestamps (string) vs. string', function() {
  133. return testRules({
  134. isBefore: (new Date()).toJSON()
  135. }, (new Date(Date.now() - 100000)).toJSON(), (new Date(Date.now() + 1000000)).toJSON());
  136. });
  137. it(' should support "isBefore" rule when validating JSON timestamps (string) vs. number', function() {
  138. return testRules({
  139. isBefore: Date.now()
  140. }, (new Date(Date.now() - 100000)).toJSON(), (new Date(Date.now() + 1000000)).toJSON());
  141. });
  142. it(' should support "isAfter" rule when validating Date instances (Date) vs. Date', function() {
  143. return testRules({
  144. isAfter: new Date()
  145. }, new Date(Date.now() + 100000), new Date(Date.now() - 1000000));
  146. });
  147. it(' should support "isAfter" rule when validating js timestamps (number) vs. number', function() {
  148. return testRules({
  149. isAfter: new Date()
  150. }, (new Date(Date.now() + 100000)).getTime(), (new Date(Date.now() - 1000000)).getTime());
  151. });
  152. it(' should support "isAfter" rule when validating JSON timestamps (string) vs. string', function() {
  153. return testRules({
  154. isAfter: (new Date()).toJSON()
  155. }, (new Date(Date.now() + 100000)).toJSON(), (new Date(Date.now() - 1000000)).toJSON());
  156. });
  157. });
  158. describe('custom rule', function() {
  159. it ('should support a custom rule as a function', function() {
  160. return testRules({
  161. custom: function(x) {
  162. return x.indexOf('foo') === 0;
  163. }
  164. }, 'foobar', 'notfoobar');
  165. });
  166. });
  167. describe('isNotIn rule', function() {
  168. it ('should support isNotIn', function() {
  169. return testRules({
  170. isNotIn: ['foo','bar','baz']
  171. }, 'bloop', 'foo');
  172. });
  173. });
  174. describe('regex rule', function() {
  175. it ('should support regex', function() {
  176. return testRules({
  177. regex: /^\w\d{2}!$/
  178. }, 'a12!', 'a1goop!');
  179. });
  180. });
  181. });