Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add file validation with functions extensionFile and maxSizeFile #60

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,9 @@ Also available for [AMD](https://github.com/amdjs/amdjs-api/wiki/AMD)
- [x] isDiners
- [x] isAmex
- [x] isCreditCard
- [x] [File](https://github.com/dleitee/valid.js/blob/master/docs/file.md)
- [x] extensionFile
- [x] maxSizeFile

# Contributing

Expand Down
2 changes: 1 addition & 1 deletion dist/validate.js

Large diffs are not rendered by default.

46 changes: 46 additions & 0 deletions docs/file.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Files

```es6
import {file} from 'valid.js'
```

## extensionFile(arrayFileExtensions)(file)

Validates that 'fileExtension' belongs to a specific set of extensions

#### Usage

```es6
import {validate, file} from 'valid.js'

//fileValue type object Object or object File

let fileValue = document.getElementById("file").files[0] //<input type="file" id="file"> selected a image .jpg
let isvalid = validate(file.extensionFile(["jpg","png","pdf"]))
let result = isvalid(fileValue)
// result => true

fileValue = document.getElementById("file").files[0] //<input type="file" id="file"> selected a file .odt
let resultFalse = isvalid(fileValue)
// resultFalse => false
```

## maxSizeFile(maxValue)(file)

Validates the size of the 'file' is less than or equal to (<=) the value

#### Usage
```es6
import {validate, file} from 'valid.js'

let fileValue = document.getElementById("file").files[0] //<input type="file" id="file"> selected a file of 1024 bytes
let isvalid = validate(file.maxSizeFile(1)) //value in Kilobyte
let result = isvalid(fileValue)
// result => true

fileValue = document.getElementById("file").files[0] //<input type="file" id="file"> selected a file of 1025 bytes
let resultFalse = isvalid(1)
// resultFalse => false
```

.
42 changes: 42 additions & 0 deletions src/file.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import {_isString} from './lib/string';
import {_isFile} from './lib/file';
import {_isNumber} from './lib/number';

/*
* Check file extension
* @params exts
* @params value
* @return boolean
*/

const extensionFile = exts => {
return (value=>{
value = _isString(value) ? value: (
_isFile(value) ? value.name : false
);
let ext = (value===false) ? false: value.substring(value.lastIndexOf('.') + 1);
return (ext===false ? false :exts.indexOf(ext)>=0);
});
};

/*
* Check size of file in megabytes
* @params maxSizeFile
* @params file
* @return boolean
*/

const maxSizeFile = (maxSizeFileValue = Number.MAX_VALUE) => {
return (file=>{
if(_isNumber(maxSizeFileValue) && _isFile(file))
return ( (file.size/1024) <= maxSizeFileValue );
});
};

/*
* Export constant file
*/
export const file = {
extensionFile:extensionFile,
maxSizeFile:maxSizeFile
};
1 change: 1 addition & 0 deletions src/lib/file.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions src/validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,18 @@ export * from './number';
export * from './date';
export * from './creditCard';
export * from './ip';
export * from './file';

export const validate =
(...fn) =>
value => {

if (isNil(value)) {
return false;
}

return reduce((acc, x) => {

if (isNil(x)) {
throw new Error('The function for validation is null or undefined.');
} else {
Expand Down
50 changes: 50 additions & 0 deletions test/file.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import expect from 'expect.js';
import {validate, file} from '../src/validate';

describe('Functions file', () => {
describe('#extensionFile(value)', () => {
it('should be true when extension of file is equal jpg/png/pdf', () => {
let result = validate(file.extensionFile(['jpg','png','pdf']));

expect(result('teste.jpg')).to.be.ok();
expect(result('teste.png')).to.be.ok();
expect(result('teste.pdf')).to.be.ok();
});
it('should be false when the file extension is different from jpg', () => {
let result = validate(file.extensionFile(['jpg']));

expect(result('teste.png')).to.not.be.ok();
});
it('should be true when extension of file is equal jpg/png/pdf', () => {
let fileAbstract = {name:'name.jpg',size:250000};
let result = validate(file.extensionFile(['jpg','png','pdf']));

expect(result(fileAbstract)).to.be.ok();
fileAbstract.name='name.png';
expect(result(fileAbstract)).to.be.ok();
fileAbstract.name='name.pdf';
expect(result(fileAbstract)).to.be.ok();
});
it('should be false when the file extension is different from jpg', () => {
let fileAbstract = {name:'name.png',size:250000};
let result = validate(file.extensionFile(['jpg']));

expect(result(fileAbstract)).to.not.be.ok();
});
});

describe('#maxSizeFile(value)', () => {
it('should be true when size of file in byte is <= maxSize in kilobyte ', () => {
let fileAbstract = {name:'name.jpg',size:1024};//size in Byte
let result = validate(file.maxSizeFile(1));//size in Kilobyte

expect(result(fileAbstract)).to.be.ok();
});
it('should be false when size of file in byte is > maxSize in kilobyte ', () => {
let fileAbstract = {name:'name.jpg',size:1025};//size in Byte
let result = validate(file.maxSizeFile(1));//size in Kilobyte

expect(result(fileAbstract)).to.not.be.ok();
});
});
});