Skip to content

Commit

Permalink
🆕 validation checks for date input
Browse files Browse the repository at this point in the history
  • Loading branch information
scottnath committed Sep 12, 2016
1 parent c8b74a9 commit 29e1a01
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 15 deletions.
8 changes: 7 additions & 1 deletion lib/validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@
* @module dateValidation
*/

module.exports = function dateValidation() {
const isDate = require('validator/lib/isDate');

module.exports = function dateValidation(input) {
if (input.target.value && !isDate(input.target.value)) {
return `${input.target.name} must be a date!`;
}

return true;
};
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
"Rachel White <whiter@us.ibm.com>"
],
"license": "Apache-2",
"dependencies": {},
"dependencies": {
"validator": "^5.7.0"
},
"devDependencies": {
"ava": "^0.14.0",
"coveralls": "^2.11.9",
Expand Down
35 changes: 22 additions & 13 deletions tests/validation.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,38 @@
import test from 'ava';
import validation from '../lib/validation';

const input = {
const empty = {
target: {
name: 'date',
value: 'foo bar baz',
},
all: {
date: 'foo bar baz',
name: 'empty',
value: '',
},
};

const settings = {
const input = {
target: {
empty: false,
name: 'good',
value: '1972-08-21',
},
all: {
date: {
empty: false,
},
};

const bad = {
target: {
name: 'bad',
value: '1972-08-foo',
},
};

// Empty input
test('empty input', t => {
t.true(validation(empty), 'Empty input returns true');
});

// Valid input
test('valid input', t => {
t.true(validation(input, settings), 'Valid input returns true');
t.true(validation(input), 'Valid input returns true');
});

// Valid input
test('bad input', t => {
t.is(validation(bad), 'bad must be a date!', 'Bad input returns true');
});

0 comments on commit 29e1a01

Please sign in to comment.