Skip to content

Commit a595809

Browse files
committed
🆕 validation added
1 parent a17ec1d commit a595809

4 files changed

Lines changed: 71 additions & 27 deletions

File tree

index.js

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,24 +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-
},
31-
datetimeTime: {
32-
validation: {
33-
function: 'datetimeValidation',
34-
on: 'blur',
35-
},
36-
label: 'Time',
37-
type: 'time',
38-
placeholder: 'Datetime',
28+
type: 'datetime-local',
29+
placeholder: new Date().toLocaleDateString(),
3930
},
4031
},
41-
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}}" />',
4233
};

lib/validation.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,13 @@
1818
*
1919
* @module datetimeValidation
2020
*/
21+
const isDate = require('validator/lib/isDate');
22+
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!`;
27+
}
2128

22-
module.exports = function datetimeValidation() {
2329
return true;
2430
};

package.json

Lines changed: 3 additions & 1 deletion
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

Lines changed: 57 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +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',
815
},
9-
all: {
10-
datetime: 'foo bar baz',
16+
};
17+
18+
const bad = {
19+
target: {
20+
name: 'bad',
21+
value: '1972-08-foo',
1122
},
1223
};
1324

14-
const settings = {
25+
const time = {
1526
target: {
16-
empty: false,
27+
name: 'with time',
28+
value: '1972-08-21T13:00',
1729
},
18-
all: {
19-
datetime: {
20-
empty: false,
21-
},
30+
};
31+
32+
const timeSpace = {
33+
target: {
34+
name: 'space in time',
35+
value: '1972-08-21 13:00',
2236
},
2337
};
2438

39+
const reverse = {
40+
target: {
41+
name: 'reverse time to date',
42+
value: '13:00 1972-08-21',
43+
},
44+
};
45+
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');
54+
});
55+
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+
});
70+
71+
// Revers date/time input
72+
test('reverse input', t => {
73+
t.true(validation(reverse), 'Reverse time/date input returns true');
2974
});

0 commit comments

Comments
 (0)