Skip to content
This repository was archived by the owner on Dec 12, 2021. It is now read-only.

Commit fb3246e

Browse files
committed
Add multiplesOf validators
This validator checks if the given number is multiple of a number.
1 parent 6d99563 commit fb3246e

File tree

4 files changed

+36
-2
lines changed

4 files changed

+36
-2
lines changed

docs/validate.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1473,6 +1473,7 @@ <h1>validate.js</h1>
14731473
equalTo: <span class="hljs-function"><span class="hljs-keyword">function</span><span class="hljs-params">(v, c)</span> </span>{ <span class="hljs-keyword">return</span> v === c; },
14741474
lessThan: <span class="hljs-function"><span class="hljs-keyword">function</span><span class="hljs-params">(v, c)</span> </span>{ <span class="hljs-keyword">return</span> v &lt; c; },
14751475
lessThanOrEqualTo: <span class="hljs-function"><span class="hljs-keyword">function</span><span class="hljs-params">(v, c)</span> </span>{ <span class="hljs-keyword">return</span> v &lt;= c; }
1476+
multiplesOf: <span class="hljs-function"><span class="hljs-keyword">function</span><span class="hljs-params">(v, c)</span> </span>{ <span class="hljs-keyword">return</span> v % c === 0; }
14761477
};</pre></div></div>
14771478

14781479
</li>

index.html

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1572,6 +1572,8 @@ <h2>Validators</h2>
15721572
<dd>The input can be this value at the most. The error message is <i>must be less than or equal to %{count}</i></dd>
15731573
<dt>lessThan</dt>
15741574
<dd>The input has to be less than this value. The error message is <i>must be less than %{count}</i></dd>
1575+
<dt>multiplesOf</dt>
1576+
<dd>The input has to be in multiples of this value. The error message is <i>must be multiples of %{count}</i></dd>
15751577
<dt>odd</dt>
15761578
<dd>The input has to be odd. The error message is <i>must be odd</i></dd>
15771579
<dt>even</dt>
@@ -1590,6 +1592,7 @@ <h2>Validators</h2>
15901592
<li><code>notEqualTo</code></li>
15911593
<li><code>notLessThan</code></li>
15921594
<li><code>notLessThanOrEqualTo</code></li>
1595+
<li><code>notMultiplesOf</code></li>
15931596
<li><code>notOdd</code></li>
15941597
<li><code>notEven</code></li>
15951598
</ul>
@@ -1612,6 +1615,9 @@ <h2>Validators</h2>
16121615
validate({duration: "3"}, {duration: {numericality: {noStrings: true}}});
16131616
// =&gt; {"duration": ["Duration is not a number"]}
16141617

1618+
validate({duration: "7"}, {duration: {numericality: {multiplesOf: 3}}});
1619+
// =&gt; {"duration": ["Duration must be multiples of 3"]}
1620+
16151621
var constraints = {
16161622
duration: {
16171623
numericality: {

specs/validators/numericality-spec.js

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,30 @@ describe("validators.numericality", function() {
193193
});
194194
});
195195

196+
describe("multiplesOf", function() {
197+
it("allows multiples of a number", function() {
198+
expect(numericality(0, {multiplesOf: 2})).not.toBeDefined();
199+
expect(numericality(5, {multiplesOf: 5})).not.toBeDefined();
200+
expect(numericality(16, {multiplesOf: 8})).not.toBeDefined();
201+
});
202+
203+
it("doesn't allow numbers that are not multiples of given number", function() {
204+
var expected = ["must be multiples of 100"];
205+
expect(numericality(121, {multiplesOf: 100})).toEqual(expected);
206+
});
207+
208+
it("allows for a default message", function() {
209+
var expected = "default message";
210+
validate.validators.numericality.notMultiplesOf = expected;
211+
expect(numericality(161, {multiplesOf: 200})).toEqual([expected]);
212+
});
213+
214+
it("allows for a custom message", function() {
215+
var expected = "custom message";
216+
expect(numericality(133, {multiplesOf: 4, notMultiplesOf: expected})).toEqual([expected]);
217+
});
218+
});
219+
196220
describe("odd", function() {
197221
it("allows odd numbers", function() {
198222
expect(numericality(1, {odd: true})).not.toBeDefined();
@@ -251,11 +275,12 @@ describe("validators.numericality", function() {
251275
greaterThanOrEqualTo: 10,
252276
lessThan: 5,
253277
lessThanOrEqualTo: 5,
278+
multiplesOf: 10,
254279
equalTo: 20,
255280
odd: true,
256281
even: true
257282
};
258-
expect(numericality(7.2, options)).toHaveLength(7);
283+
expect(numericality(7.2, options)).toHaveLength(8);
259284
});
260285

261286
it("returns options.message only once", function() {
@@ -264,6 +289,7 @@ describe("validators.numericality", function() {
264289
greaterThanOrEqualTo: 10,
265290
lessThan: 5,
266291
lessThanOrEqualTo: 5,
292+
multiplesOf: 10,
267293
equalTo: 20,
268294
odd: true,
269295
even: true,

validate.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -835,7 +835,8 @@
835835
greaterThanOrEqualTo: function(v, c) { return v >= c; },
836836
equalTo: function(v, c) { return v === c; },
837837
lessThan: function(v, c) { return v < c; },
838-
lessThanOrEqualTo: function(v, c) { return v <= c; }
838+
lessThanOrEqualTo: function(v, c) { return v <= c; },
839+
multiplesOf: function(v, c) { return v % c === 0; }
839840
};
840841

841842
// Strict will check that it is a valid looking number

0 commit comments

Comments
 (0)