Skip to content

Commit

Permalink
feat: add intigrity control for files
Browse files Browse the repository at this point in the history
  • Loading branch information
EmmanuelDemey committed Jul 9, 2019
1 parent 1a790cd commit da02f39
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 5 deletions.
1 change: 0 additions & 1 deletion src/js/components/operations/document/edition/edition.js
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,6 @@ class OperationsDocumentationEdition extends Component {
<div
{...getRootProps({
className: 'dropzone',
onDrop: event => event.stopPropagation(),
})}
>
<input
Expand Down
35 changes: 31 additions & 4 deletions src/js/components/operations/document/edition/validation.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,36 @@
import D from 'js/i18n';
import { LINK, DOCUMENT } from '../utils';

/**
* Check if a list of document is correct.
* This list of document should respect the following criterias
* - Should have at least one item
* - The name of each files should respect a regexp
*
* @param {Array<{name}>} files
* @returns {{errorMessage: string, fields: { file: boolean}}}
*/
function verifyFile(files = []) {
const regexp = /^[a-zA-Z1-9-_\.]*$/;
let errorMessage = '';

if (files.length === 0) {
errorMessage = D.requiredFile;
} else {
const wrongFile = files
.map(file => file.name)
.find(fileName => !regexp.test(fileName));
if (wrongFile) {
errorMessage = D.wrongFileName;
}
}
return {
errorMessage,
fields: { file: errorMessage !== '' },
};
}

export function validate(document, type, files) {
console.log(files);
const fields = {};
let errorMessage = '';
if (!document.labelLg1 || !document.labelLg2) {
Expand All @@ -15,12 +43,11 @@ export function validate(document, type, files) {
} else if (type === LINK && !/https*\:\/\//.test(document.url)) {
errorMessage = D.badUrl;
fields.url = true;
} else if (type === DOCUMENT && files.length === 0) {
errorMessage = D.requiredFile;
fields.file = true;
} else if (!document.lang) {
errorMessage = D.requiredLang;
fields.lang = true;
} else if (type === DOCUMENT) {
return verifyFile(files);
}
return {
fields,
Expand Down
39 changes: 39 additions & 0 deletions src/js/components/operations/document/edition/validation.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { validate } from './validation';
import { DOCUMENT } from '../utils';

describe('validate', () => {
describe('documents', () => {
const type = DOCUMENT;
const document = {
labelLg1: '1',
labelLg2: '2',
lang: 'fr',
};
it('should return an error if the files is undefined', () => {
expect(validate(document, type)).toEqual({
errorMessage: 'The file is required',
fields: { file: true },
});
});
it('should return an error if the files is an empty array', () => {
expect(validate(document, type, [])).toEqual({
errorMessage: 'The file is required',
fields: { file: true },
});
});
it('should return an error if ta name is not valid', () => {
expect(validate(document, type, [{ name: 'name with space' }])).toEqual({
errorMessage: 'The name of the file incorrect',
fields: { file: true },
});
});
it('should not return any error', () => {
expect(
validate(document, type, [{ name: 'name_without_space' }])
).toEqual({
errorMessage: '',
fields: { file: false },
});
});
});
});
4 changes: 4 additions & 0 deletions src/js/i18n/dictionary/operations/validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ export default {
fr: 'Le fichier est obligatoire',
en: 'The file is required',
},
wrongFileName: {
fr: 'Le nom du fichier est incorrect',
en: 'The name of the file incorrect',
},
requiredFamily: {
fr: 'La famille est obligatoire',
en: 'The family is required',
Expand Down

0 comments on commit da02f39

Please sign in to comment.