From c2a5d77c3fabc2ab20036e842e5f1f25b8e22718 Mon Sep 17 00:00:00 2001 From: Nuno Sousa Date: Mon, 25 May 2015 16:07:58 +0100 Subject: [PATCH] Fix assert dependency issue --- lib/asserts/date-diff-greater-than-assert.js | 21 +++++++++++++++---- lib/asserts/date-diff-less-than-assert.js | 21 +++++++++++++++---- .../date-diff-greater-than-assert_test.js | 6 +++++- .../date-diff-less-than-assert_test.js | 6 +++++- test/asserts/null-or-date-assert_test.js | 6 +++--- 5 files changed, 47 insertions(+), 13 deletions(-) diff --git a/lib/asserts/date-diff-greater-than-assert.js b/lib/asserts/date-diff-greater-than-assert.js index 697a06d..a31e465 100644 --- a/lib/asserts/date-diff-greater-than-assert.js +++ b/lib/asserts/date-diff-greater-than-assert.js @@ -4,10 +4,19 @@ */ var _ = require('lodash'); -var DateAssert = require('./date-assert'); +var Asserts = require('validator'); +var Validator = require('validator.js').Validator; var Violation = require('validator.js').Violation; var moment = require('moment'); +/** + * Add custom error code for `date` or `string`. + */ + +/* jshint camelcase: false */ +Validator.errorCode.must_be_a_date_or_a_string = 'must_be_a_date_or_a_string'; +/* jshint camelcase: true */ + /** * Export `DateDiffGreaterThanAssert`. */ @@ -50,9 +59,13 @@ module.exports = function(threshold, options) { */ this.validate = function(value) { - try { - new DateAssert().validate(value); - } catch (e) { + if ('string' !== typeof value && !(value instanceof Date)) { + /* jshint camelcase: false */ + throw new Violation(this, value, { value: Validator.errorCode.must_be_a_date_or_a_string }); + /* jshint camelcase: true */ + } + + if (true !== Asserts.isDate(value)) { throw new Violation(this, value, { absolute: this.absolute, asFloat: this.asFloat, fromDate: this.fromDate, threshold: this.threshold, unit: this.unit }); } diff --git a/lib/asserts/date-diff-less-than-assert.js b/lib/asserts/date-diff-less-than-assert.js index a54c43f..0a48c8e 100644 --- a/lib/asserts/date-diff-less-than-assert.js +++ b/lib/asserts/date-diff-less-than-assert.js @@ -4,10 +4,19 @@ */ var _ = require('lodash'); -var DateAssert = require('./date-assert'); +var Asserts = require('validator'); +var Validator = require('validator.js').Validator; var Violation = require('validator.js').Violation; var moment = require('moment'); +/** + * Add custom error code for `date` or `string`. + */ + +/* jshint camelcase: false */ +Validator.errorCode.must_be_a_date_or_a_string = 'must_be_a_date_or_a_string'; +/* jshint camelcase: true */ + /** * Export `DateDiffLessThanAssert`. */ @@ -50,9 +59,13 @@ module.exports = function(threshold, options) { */ this.validate = function(value) { - try { - new DateAssert().validate(value); - } catch (e) { + if ('string' !== typeof value && !(value instanceof Date)) { + /* jshint camelcase: false */ + throw new Violation(this, value, { value: Validator.errorCode.must_be_a_date_or_a_string }); + /* jshint camelcase: true */ + } + + if (true !== Asserts.isDate(value)) { throw new Violation(this, value, { absolute: this.absolute, asFloat: this.asFloat, fromDate: this.fromDate, threshold: this.threshold, unit: this.unit }); } diff --git a/test/asserts/date-diff-greater-than-assert_test.js b/test/asserts/date-diff-greater-than-assert_test.js index 9664139..980f96a 100644 --- a/test/asserts/date-diff-greater-than-assert_test.js +++ b/test/asserts/date-diff-greater-than-assert_test.js @@ -4,6 +4,7 @@ */ var Assert = require('validator.js').Assert; +var Validator = require('validator.js').Validator; var Violation = require('validator.js').Violation; var assert = require('../../lib/asserts/date-diff-greater-than-assert'); var sinon = require('sinon'); @@ -53,7 +54,7 @@ describe('DateDiffGreaterThanAssert', function() { }); it('should throw an error if the input value is not a date', function() { - var choices = [[], {}, '']; + var choices = [[], {}]; choices.forEach(function(choice) { try { @@ -62,6 +63,9 @@ describe('DateDiffGreaterThanAssert', function() { should.fail(); } catch (e) { e.should.be.instanceOf(Violation); + /* jshint camelcase: false */ + e.violation.value.should.equal(Validator.errorCode.must_be_a_date_or_a_string); + /* jshint camelcase: true */ } }); }); diff --git a/test/asserts/date-diff-less-than-assert_test.js b/test/asserts/date-diff-less-than-assert_test.js index ee4755e..41203a9 100644 --- a/test/asserts/date-diff-less-than-assert_test.js +++ b/test/asserts/date-diff-less-than-assert_test.js @@ -4,6 +4,7 @@ */ var Assert = require('validator.js').Assert; +var Validator = require('validator.js').Validator; var Violation = require('validator.js').Violation; var assert = require('../../lib/asserts/date-diff-less-than-assert'); var sinon = require('sinon'); @@ -53,7 +54,7 @@ describe('DateDiffLessThanAssert', function() { }); it('should throw an error if the input value is not a date', function() { - var choices = [[], {}, '']; + var choices = [[], {}]; choices.forEach(function(choice) { try { @@ -62,6 +63,9 @@ describe('DateDiffLessThanAssert', function() { should.fail(); } catch (e) { e.should.be.instanceOf(Violation); + /* jshint camelcase: false */ + e.violation.value.should.equal(Validator.errorCode.must_be_a_date_or_a_string); + /* jshint camelcase: true */ } }); }); diff --git a/test/asserts/null-or-date-assert_test.js b/test/asserts/null-or-date-assert_test.js index 0a0c677..871298b 100644 --- a/test/asserts/null-or-date-assert_test.js +++ b/test/asserts/null-or-date-assert_test.js @@ -18,7 +18,7 @@ describe('NullOrDateAssert', function() { Assert.prototype.NullOrDate = assert; }); - it('should throw an error if the input value is not a `null` or a date', function() { + it('should throw an error if the input value is not a `null` or a Date', function() { var choices = [[], {}, 123]; choices.forEach(function(choice) { @@ -49,11 +49,11 @@ describe('NullOrDateAssert', function() { new Assert().NullOrDate().validate(null); }); - it('should accept a `date`', function() { + it('should accept a Date', function() { new Assert().NullOrDate().validate(new Date()); }); - it('should accept a `string`', function() { + it('should accept a String', function() { new Assert().NullOrDate().validate('2014-10-16'); }); });