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

add unequalTo numericality constrainst #308

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -1658,6 +1658,8 @@ <h2>Validators</h2>
<dd>The input has to be at least this value. The error message is <i>must be greater than or equal to %{count}</i></dd>
<dt>equalTo</dt>
<dd>The input has to be exactly this value. The error message is <i>must be equal to %{count}</i></dd>
<dt>unequalTo</dt>
<dd>The input has to be unequal to this value. The error message is <i>must be unequal to %{count}</i></dd>
<dt>lessThanOrEqualTo</dt>
<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>
<dt>lessThan</dt>
Expand All @@ -1680,6 +1682,7 @@ <h2>Validators</h2>
<li><code>notGreaterThan</code></li>
<li><code>notGreaterThanOrEqualTo</code></li>
<li><code>notEqualTo</code></li>
<li><code>notUnequalTo</code></li>
<li><code>notLessThan</code></li>
<li><code>notLessThanOrEqualTo</code></li>
<li><code>notDivisibleBy</code></li>
Expand Down
25 changes: 25 additions & 0 deletions specs/validators/numericality-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ describe("validators.numericality", function() {
delete n.notGreaterThan;
delete n.notGreaterThanOrEqualTo;
delete n.notEqualTo;
delete n.notUnequalTo;
delete n.notLessThan;
delete n.notLessThanOrEqualTo;
delete n.notDivisibleBy;
Expand Down Expand Up @@ -160,6 +161,30 @@ describe("validators.numericality", function() {
});
});

describe("unequalTo", function() {
it("allows numbers that are unequal to", function() {
expect(numericality(2.72, {unequalTo: 2.73})).not.toBeDefined();
});

it("doesn't allow numbers that are equal", function() {
var expected = ["must be unequal to 2.72"];
expect(numericality(2.72, {unequalTo: 2.72})).toEqual(expected);
});

it("allows for a default message", function() {
validate.validators.numericality.message = "default generic message";
expect(numericality(3.13, {unequalTo: 3.13})).toEqual(["default generic message"]);

validate.validators.numericality.notUnequalTo = "default message";
expect(numericality(3.13, {unequalTo: 3.13})).toEqual(["default message"]);
});

it("allows for a custom message", function() {
var expected = "custom message";
expect(numericality(3.13, {unequalTo: 3.13, notUnequalTo:expected})).toEqual([expected]);
});
});

describe("lessThan", function() {
it("allows numbers that are less than", function() {
expect(numericality(2.72, {lessThan: 3.14})).not.toBeDefined();
Expand Down
1 change: 1 addition & 0 deletions validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -836,6 +836,7 @@
greaterThan: function(v, c) { return v > c; },
greaterThanOrEqualTo: function(v, c) { return v >= c; },
equalTo: function(v, c) { return v === c; },
unequalTo: function(v, c) { return v !== c; },
lessThan: function(v, c) { return v < c; },
lessThanOrEqualTo: function(v, c) { return v <= c; },
divisibleBy: function(v, c) { return v % c === 0; }
Expand Down