Skip to content

Commit

Permalink
Merge pull request #47 from scottnath/hotfix/val
Browse files Browse the repository at this point in the history
remove old validation
  • Loading branch information
Snugug authored Sep 13, 2016
2 parents 1aea474 + 29e1a01 commit 3b90484
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 26 deletions.
3 changes: 0 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@ module.exports = {
label: 'date',
placeholder: 'date',
type: 'date',
settings: {
empty: true,
},
},
},
html: '<label for="{{date.id}}">{{date.label}}</label><input type="{{date.type}}" id="{{date.id}}" name="{{date.name}}" value="{{date.value}}" placeholder="{{date.placeholder}}" />',
Expand Down
8 changes: 5 additions & 3 deletions lib/validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@
* @module dateValidation
*/

module.exports = function dateValidation(input, settings) {
if (input.target.value === '' && !settings.target.empty) {
return `${input.target.name} cannot be left blank!`;
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;
Expand Down
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
39 changes: 20 additions & 19 deletions tests/validation.js
Original file line number Diff line number Diff line change
@@ -1,37 +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');
});

// Invalid input
test('validate correct input', t => {
const ip = input;
ip.target.value = '';

t.is(validation(ip, settings), 'date cannot be left blank!', 'Return string if not valid');
// Valid input
test('bad input', t => {
t.is(validation(bad), 'bad must be a date!', 'Bad input returns true');
});

0 comments on commit 3b90484

Please sign in to comment.