From a17ec1db65eb767377af2922da3587829de5dc8a Mon Sep 17 00:00:00 2001 From: Scott Nath Date: Mon, 12 Sep 2016 12:57:01 -0400 Subject: [PATCH 1/2] :unamused: :fire: remove old validation --- index.js | 6 ------ lib/validation.js | 6 +----- tests/validation.js | 8 -------- 3 files changed, 1 insertion(+), 19 deletions(-) diff --git a/index.js b/index.js index e80a5c1..b56567f 100644 --- a/index.js +++ b/index.js @@ -27,9 +27,6 @@ module.exports = { label: 'Date', type: 'date', placeholder: 'Datetime', - settings: { - empty: true, - }, }, datetimeTime: { validation: { @@ -39,9 +36,6 @@ module.exports = { label: 'Time', type: 'time', placeholder: 'Datetime', - settings: { - empty: true, - }, }, }, html: '', diff --git a/lib/validation.js b/lib/validation.js index a76d734..037a951 100644 --- a/lib/validation.js +++ b/lib/validation.js @@ -19,10 +19,6 @@ * @module datetimeValidation */ -module.exports = function datetimeValidation(input, settings) { - if (input.target.value === '' && !settings.target.empty) { - return `${input.target.name} cannot be left blank!`; - } - +module.exports = function datetimeValidation() { return true; }; diff --git a/tests/validation.js b/tests/validation.js index 8e7a74c..e44d917 100644 --- a/tests/validation.js +++ b/tests/validation.js @@ -27,11 +27,3 @@ const settings = { test('valid input', t => { t.true(validation(input, settings), 'Valid input returns true'); }); - -// Invalid input -test('validate correct input', t => { - const ip = input; - ip.target.value = ''; - - t.is(validation(ip, settings), 'datetime cannot be left blank!', 'Return string if not valid'); -}); From a595809d8a344ae8caab68cab27c5460d3bd057f Mon Sep 17 00:00:00 2001 From: Scott Nath Date: Tue, 13 Sep 2016 11:59:57 -0400 Subject: [PATCH 2/2] :new: validation added --- index.js | 17 +++-------- lib/validation.js | 8 +++++- package.json | 4 ++- tests/validation.js | 69 +++++++++++++++++++++++++++++++++++++-------- 4 files changed, 71 insertions(+), 27 deletions(-) diff --git a/index.js b/index.js index b56567f..7e40159 100644 --- a/index.js +++ b/index.js @@ -19,24 +19,15 @@ module.exports = { datetimeValidation: validation, }, inputs: { - datetimeDate: { + datetime: { validation: { function: 'datetimeValidation', on: 'blur', }, label: 'Date', - type: 'date', - placeholder: 'Datetime', - }, - datetimeTime: { - validation: { - function: 'datetimeValidation', - on: 'blur', - }, - label: 'Time', - type: 'time', - placeholder: 'Datetime', + type: 'datetime-local', + placeholder: new Date().toLocaleDateString(), }, }, - html: '', + html: '', }; diff --git a/lib/validation.js b/lib/validation.js index 037a951..862844e 100644 --- a/lib/validation.js +++ b/lib/validation.js @@ -18,7 +18,13 @@ * * @module datetimeValidation */ +const isDate = require('validator/lib/isDate'); + + +module.exports = function datetimeValidation(input) { + if (input.target.value && !isDate(input.target.value)) { + return `${input.target.name} must be a date!`; + } -module.exports = function datetimeValidation() { return true; }; diff --git a/package.json b/package.json index 786477b..e32d996 100644 --- a/package.json +++ b/package.json @@ -21,7 +21,9 @@ "Rachel White " ], "license": "Apache-2", - "dependencies": {}, + "dependencies": { + "validator": "^5.7.0" + }, "devDependencies": { "ava": "^0.14.0", "coveralls": "^2.11.9", diff --git a/tests/validation.js b/tests/validation.js index e44d917..2cd1e17 100644 --- a/tests/validation.js +++ b/tests/validation.js @@ -1,29 +1,74 @@ import test from 'ava'; import validation from '../lib/validation'; +const empty = { + target: { + name: 'empty', + value: '', + }, +}; + const input = { target: { - name: 'datetime', - value: 'foo bar baz', + name: 'good', + value: '1972-08-21', }, - all: { - datetime: 'foo bar baz', +}; + +const bad = { + target: { + name: 'bad', + value: '1972-08-foo', }, }; -const settings = { +const time = { target: { - empty: false, + name: 'with time', + value: '1972-08-21T13:00', }, - all: { - datetime: { - empty: false, - }, +}; + +const timeSpace = { + target: { + name: 'space in time', + value: '1972-08-21 13:00', }, }; +const reverse = { + target: { + name: 'reverse time to date', + value: '13:00 1972-08-21', + }, +}; + +// Empty input +test('empty input', t => { + t.true(validation(empty), 'Empty input returns true'); +}); -// Valid input +// Valid date-only input test('valid input', t => { - t.true(validation(input, settings), 'Valid input returns true'); + t.true(validation(input), 'Valid input returns true'); +}); + +// Bad input +test('bad input', t => { + t.is(validation(bad), 'bad must be a date!', 'Bad input returns string'); +}); + +// Valid date/time input +test('valid with time input', t => { + t.true(validation(time), 'Valid input returns true'); +}); + +// Valid date/time input +test('valid with time input', t => { + t.true(validation(timeSpace), 'Valid input returns true'); +}); + +// Revers date/time input +test('reverse input', t => { + t.true(validation(reverse), 'Reverse time/date input returns true'); });