From 44444b12927a68fcd308df6fc2305b7d678137f1 Mon Sep 17 00:00:00 2001 From: Cody Nguyen Date: Thu, 26 Sep 2019 15:14:41 +0700 Subject: [PATCH] add unequalTo numericality constrainst --- index.html | 3 +++ specs/validators/numericality-spec.js | 25 +++++++++++++++++++++++++ validate.js | 1 + 3 files changed, 29 insertions(+) diff --git a/index.html b/index.html index 223de15..21032e3 100644 --- a/index.html +++ b/index.html @@ -1658,6 +1658,8 @@

Validators

The input has to be at least this value. The error message is must be greater than or equal to %{count}
equalTo
The input has to be exactly this value. The error message is must be equal to %{count}
+
unequalTo
+
The input has to be unequal to this value. The error message is must be unequal to %{count}
lessThanOrEqualTo
The input can be this value at the most. The error message is must be less than or equal to %{count}
lessThan
@@ -1680,6 +1682,7 @@

Validators

  • notGreaterThan
  • notGreaterThanOrEqualTo
  • notEqualTo
  • +
  • notUnequalTo
  • notLessThan
  • notLessThanOrEqualTo
  • notDivisibleBy
  • diff --git a/specs/validators/numericality-spec.js b/specs/validators/numericality-spec.js index 6932d0f..1c438cd 100644 --- a/specs/validators/numericality-spec.js +++ b/specs/validators/numericality-spec.js @@ -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; @@ -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(); diff --git a/validate.js b/validate.js index adf023b..1e22f22 100644 --- a/validate.js +++ b/validate.js @@ -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; }