diff --git a/index.js b/index.js
index 1da4f17..69ce5f9 100644
--- a/index.js
+++ b/index.js
@@ -27,11 +27,11 @@ module.exports = {
type: 'file',
label: 'Upload a file',
settings: {
- extensions: [
+ types: [
'*',
],
},
},
},
- html: '',
+ html: '',
};
diff --git a/lib/validation.js b/lib/validation.js
index 68ec234..77a0966 100644
--- a/lib/validation.js
+++ b/lib/validation.js
@@ -19,6 +19,18 @@
* @module fileValidation
*/
-module.exports = function fileValidation() {
+module.exports = function fileValidation(input, settings) {
+ let extension = '.';
+
+ if (input.target.value && settings.target.types && Array.isArray(settings.target.types)) {
+ if (settings.target.types.length > 1 || settings.target.types[0] !== '*') {
+ extension += input.target.value.split('.').pop();
+
+ if (settings.target.types.indexOf(extension) < 0) {
+ return `Invalid extension '${extension}'. Valid extensions are '${settings.target.types.join(', ')}'`;
+ }
+ }
+ }
+
return true;
};
diff --git a/tests/validation.js b/tests/validation.js
index bda25ec..1a8aa5d 100644
--- a/tests/validation.js
+++ b/tests/validation.js
@@ -1,29 +1,180 @@
import test from 'ava';
import validation from '../lib/validation';
-const input = {
- target: {
- name: 'boolean',
- value: 'foo bar baz',
- },
- all: {
- boolean: 'foo bar baz',
- },
-};
-
-const settings = {
- target: {
- empty: false,
- },
- all: {
- boolean: {
- empty: false,
- },
- },
-};
-
-
-// Valid input
-test('valid input', t => {
+test('Empty Input', t => {
+ const input = {
+ target: {
+ name: 'file',
+ value: '',
+ },
+ all: {
+ file: '',
+ },
+ };
+
+ const settings = {
+ target: {
+ types: [
+ '*',
+ ],
+ },
+ all: {
+ file: {
+ types: [
+ '*',
+ ],
+ },
+ },
+ };
+
+ t.true(validation(input, settings), 'Empty input returns true');
+});
+
+test('Valid Input - All Accepted', t => {
+ const input = {
+ target: {
+ name: 'file',
+ value: 'punchcard.svg',
+ },
+ all: {
+ file: 'punchcard.svg',
+ },
+ };
+
+ const settings = {
+ target: {
+ types: [
+ '*',
+ ],
+ },
+ all: {
+ file: {
+ types: [
+ '*',
+ ],
+ },
+ },
+ };
+
+ t.true(validation(input, settings), 'Valid input returns true');
+});
+
+test('Valid Input - Single Type Accepted', t => {
+ const input = {
+ target: {
+ name: 'file',
+ value: 'punchcard.svg',
+ },
+ all: {
+ file: 'punchcard.svg',
+ },
+ };
+
+ const settings = {
+ target: {
+ types: [
+ '.svg',
+ ],
+ },
+ all: {
+ file: {
+ types: [
+ '.svg',
+ ],
+ },
+ },
+ };
+
+ t.true(validation(input, settings), 'Valid input returns true');
+});
+
+test('Valid Input - Multiple Types Accepted', t => {
+ const input = {
+ target: {
+ name: 'file',
+ value: 'punchcard.svg',
+ },
+ all: {
+ file: 'punchcard.svg',
+ },
+ };
+
+ const settings = {
+ target: {
+ types: [
+ '.svg',
+ '.jpeg',
+ ],
+ },
+ all: {
+ file: {
+ types: [
+ '.svg',
+ '.jpeg',
+ ],
+ },
+ },
+ };
+
t.true(validation(input, settings), 'Valid input returns true');
});
+
+test('Invalid Input - Single Types Accepted', t => {
+ const input = {
+ target: {
+ name: 'file',
+ value: 'punchcard.png',
+ },
+ all: {
+ file: 'punchcard.png',
+ },
+ };
+
+ const settings = {
+ target: {
+ types: [
+ '.svg',
+ ],
+ },
+ all: {
+ file: {
+ types: [
+ '.svg',
+ ],
+ },
+ },
+ };
+
+ t.is(validation(input, settings), 'Invalid extension \'.png\'. Valid extensions are \'.svg\'', 'Invalid input returns with string');
+});
+
+test('Invalid Input - Multiple Types Accepted', t => {
+ const input = {
+ target: {
+ name: 'file',
+ value: 'punchcard.png',
+ },
+ all: {
+ file: 'punchcard.png',
+ },
+ };
+
+ const settings = {
+ target: {
+ types: [
+ '.svg',
+ '.jpeg',
+ ],
+ },
+ all: {
+ file: {
+ types: [
+ '.svg',
+ '.jpeg',
+ ],
+ },
+ },
+ };
+
+ t.is(validation(input, settings), 'Invalid extension \'.png\'. Valid extensions are \'.svg, .jpeg\'', 'Invalid input returns with string');
+});