Skip to content

Commit 82d4cdc

Browse files
authored
Merge pull request #46 from scottnath/hotfix/val
remove old validation
2 parents 19ed16c + a595809 commit 82d4cdc

File tree

4 files changed

+66
-40
lines changed

4 files changed

+66
-40
lines changed

index.js

+4-19
Original file line numberDiff line numberDiff line change
@@ -19,30 +19,15 @@ module.exports = {
1919
datetimeValidation: validation,
2020
},
2121
inputs: {
22-
datetimeDate: {
22+
datetime: {
2323
validation: {
2424
function: 'datetimeValidation',
2525
on: 'blur',
2626
},
2727
label: 'Date',
28-
type: 'date',
29-
placeholder: 'Datetime',
30-
settings: {
31-
empty: true,
32-
},
33-
},
34-
datetimeTime: {
35-
validation: {
36-
function: 'datetimeValidation',
37-
on: 'blur',
38-
},
39-
label: 'Time',
40-
type: 'time',
41-
placeholder: 'Datetime',
42-
settings: {
43-
empty: true,
44-
},
28+
type: 'datetime-local',
29+
placeholder: new Date().toLocaleDateString(),
4530
},
4631
},
47-
html: '<label for="{{datetimeDate.id}}">{{datetimeDate.label}}</label><input type="{{datetimeDate.type}}" id="{{datetimeDate.id}}" name="{{datetimeDate.name}}" value="{{datetimeDate.value}}" placeholder="{{datetimeDate.placeholder}}" /><label for="{{datetimeTime.id}}">{{datetimeTime.label}}</label><input type="{{datetimeTime.type}}" id="{{datetimeTime.id}}" name="{{datetimeTime.name}}" value="{{datetimeTime.value}}" placeholder="{{datetimeTime.placeholder}}" />',
32+
html: '<label for="{{datetime.id}}">{{datetime.label}}</label><input type="{{datetime.type}}" id="{{datetime.id}}" name="{{datetime.name}}" value="{{datetime.value}}" placeholder="{{datetime.placeholder}}" />',
4833
};

lib/validation.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,12 @@
1818
*
1919
* @module datetimeValidation
2020
*/
21+
const isDate = require('validator/lib/isDate');
2122

22-
module.exports = function datetimeValidation(input, settings) {
23-
if (input.target.value === '' && !settings.target.empty) {
24-
return `${input.target.name} cannot be left blank!`;
23+
24+
module.exports = function datetimeValidation(input) {
25+
if (input.target.value && !isDate(input.target.value)) {
26+
return `${input.target.name} must be a date!`;
2527
}
2628

2729
return true;

package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@
2121
"Rachel White <whiter@us.ibm.com>"
2222
],
2323
"license": "Apache-2",
24-
"dependencies": {},
24+
"dependencies": {
25+
"validator": "^5.7.0"
26+
},
2527
"devDependencies": {
2628
"ava": "^0.14.0",
2729
"coveralls": "^2.11.9",

tests/validation.js

+54-17
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,74 @@
11
import test from 'ava';
22
import validation from '../lib/validation';
33

4+
const empty = {
5+
target: {
6+
name: 'empty',
7+
value: '',
8+
},
9+
};
10+
411
const input = {
512
target: {
6-
name: 'datetime',
7-
value: 'foo bar baz',
13+
name: 'good',
14+
value: '1972-08-21',
15+
},
16+
};
17+
18+
const bad = {
19+
target: {
20+
name: 'bad',
21+
value: '1972-08-foo',
822
},
9-
all: {
10-
datetime: 'foo bar baz',
23+
};
24+
25+
const time = {
26+
target: {
27+
name: 'with time',
28+
value: '1972-08-21T13:00',
1129
},
1230
};
1331

14-
const settings = {
32+
const timeSpace = {
1533
target: {
16-
empty: false,
34+
name: 'space in time',
35+
value: '1972-08-21 13:00',
1736
},
18-
all: {
19-
datetime: {
20-
empty: false,
21-
},
37+
};
38+
39+
const reverse = {
40+
target: {
41+
name: 'reverse time to date',
42+
value: '13:00 1972-08-21',
2243
},
2344
};
2445

46+
// Empty input
47+
test('empty input', t => {
48+
t.true(validation(empty), 'Empty input returns true');
49+
});
2550

26-
// Valid input
51+
// Valid date-only input
2752
test('valid input', t => {
28-
t.true(validation(input, settings), 'Valid input returns true');
53+
t.true(validation(input), 'Valid input returns true');
2954
});
3055

31-
// Invalid input
32-
test('validate correct input', t => {
33-
const ip = input;
34-
ip.target.value = '';
56+
// Bad input
57+
test('bad input', t => {
58+
t.is(validation(bad), 'bad must be a date!', 'Bad input returns string');
59+
});
60+
61+
// Valid date/time input
62+
test('valid with time input', t => {
63+
t.true(validation(time), 'Valid input returns true');
64+
});
65+
66+
// Valid date/time input
67+
test('valid with time input', t => {
68+
t.true(validation(timeSpace), 'Valid input returns true');
69+
});
3570

36-
t.is(validation(ip, settings), 'datetime cannot be left blank!', 'Return string if not valid');
71+
// Revers date/time input
72+
test('reverse input', t => {
73+
t.true(validation(reverse), 'Reverse time/date input returns true');
3774
});

0 commit comments

Comments
 (0)