Skip to content

Commit

Permalink
Fix assert dependency issue
Browse files Browse the repository at this point in the history
  • Loading branch information
nunofgs committed May 25, 2015
1 parent c561787 commit 8dec7a5
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 10 deletions.
21 changes: 17 additions & 4 deletions lib/asserts/date-diff-greater-than-assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
*/
Expand Down Expand Up @@ -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 });
}

Expand Down
21 changes: 17 additions & 4 deletions lib/asserts/date-diff-less-than-assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
*/
Expand Down Expand Up @@ -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 });
}

Expand Down
6 changes: 5 additions & 1 deletion test/asserts/date-diff-greater-than-assert_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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 {
Expand All @@ -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 */
}
});
});
Expand Down
6 changes: 5 additions & 1 deletion test/asserts/date-diff-less-than-assert_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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 {
Expand All @@ -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 */
}
});
});
Expand Down

0 comments on commit 8dec7a5

Please sign in to comment.