Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ module.exports = {
type: 'file',
label: 'Upload a file',
settings: {
extensions: [
types: [
'*',
],
},
},
},
html: '<label for="{{file.id}}">{{file.label}}</label><input type="{{file.type}}" id="{{file.id}}" name="{{file.name}}" value="{{file.value}}" placeholder="{{file.placeholder}}" />',
html: '<label for="{{file.id}}">{{file.label}}</label><input type="{{file.type}}" id="{{file.id}}" name="{{file.name}}" value="{{file.value}}" {% if settings.types %}{%set comma = joiner() %}accept="{% for type in settings.type %}{{comma()}}{{type}}{% endfor %}{% endif %}" />',
};
14 changes: 13 additions & 1 deletion lib/validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
199 changes: 175 additions & 24 deletions tests/validation.js
Original file line number Diff line number Diff line change
@@ -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');
});