diff --git a/tests/nii.spec.js b/tests/nii.spec.js index 35f2e3570..4358aa296 100644 --- a/tests/nii.spec.js +++ b/tests/nii.spec.js @@ -125,6 +125,44 @@ describe('NIFTI', function() { }) }) + it('should generate warning if files listed in IntendedFor of fieldmap json are not of type .nii or .nii.gz', function() { + var file = { + name: 'sub-09_ses-test_run-01_fieldmap.nii.gz', + path: + '/ds114/sub-09/ses-test/fmap/sub-09_ses-test_run-01_fieldmap.nii.gz', + relativePath: + '/sub-09/ses-test/fmap/sub-09_ses-test_run-01_fieldmap.nii.gz', + } + + var jsonContentsDict = { + '/sub-09/ses-test/fmap/sub-09_ses-test_run-01_fieldmap.json': { + TaskName: 'Mixed Event Related Probe', + IntendedFor: [ + 'func/sub-15_task-mixedeventrelatedprobe_run-05_bold.json', + 'func/sub-15_task-mixedeventrelatedprobe_run-02_bold.nii.gz', + ], + }, + } + var fileList = [] + fileList.push({ + name: 'sub-15_task-mixedeventrelatedprobe_run-01_bold.nii.gz', + path: 'sub-15/func/sub-15_task-mixedeventrelatedprobe_run-01_bold.nii.gz', + relativePath: + '/func/sub-15_task-mixedeventrelatedprobe_run-01_bold.nii.gz', + }) + validate.NIFTI(null, file, jsonContentsDict, {}, [], [], function(issues) { + assert( + issues.some( + issue => + issue.reason === + 'Invalid filetype: IntendedFor should point to the .nii[.gz] files.' && + issue.evidence === + 'func/sub-15_task-mixedeventrelatedprobe_run-05_bold.json', + ), + ) + }) + }) + it('should generate warning if files listed in IntendedFor of fieldmap json do not exist', function() { var file = { name: 'sub-09_ses-test_run-01_fieldmap.nii.gz', @@ -152,7 +190,7 @@ describe('NIFTI', function() { }) validate.NIFTI(null, file, jsonContentsDict, {}, [], [], function(issues) { assert( - (issues.length = 2 && issues[0].code == 17 && issues[1].code == 37), + issues.length === 3 && issues[0].code == 17 && issues[1].code == 37, ) }) }) diff --git a/validators/nifti/nii.js b/validators/nifti/nii.js index 6e5b8a4f6..5c652c546 100644 --- a/validators/nifti/nii.js +++ b/validators/nifti/nii.js @@ -433,6 +433,7 @@ module.exports = function NIFTI( for (let key = 0; key < intendedFor.length; key++) { const intendedForFile = intendedFor[key] checkIfIntendedExists(intendedForFile, fileList, issues, file) + checkIfValidFiletype(intendedForFile, issues, file) } } } @@ -485,9 +486,11 @@ function checkIfIntendedExists(intendedForFile, fileList, issues, file) { let onTheList = false for (let key2 in fileList) { - const filePath = fileList[key2].relativePath - if (filePath === intendedForFileFull) { - onTheList = true + if (key2) { + const filePath = fileList[key2].relativePath + if (filePath === intendedForFileFull) { + onTheList = true + } } } if (!onTheList) { @@ -509,3 +512,18 @@ function checkIfIntendedExists(intendedForFile, fileList, issues, file) { ) } } + +function checkIfValidFiletype(intendedForFile, issues, file) { + const validFiletype = new RegExp('.nii(.gz)?$') + const isValidFiletype = validFiletype.test(intendedForFile) + if (!isValidFiletype) { + issues.push( + new Issue({ + file: file, + code: 37, + reason: `Invalid filetype: IntendedFor should point to the .nii[.gz] files.`, + evidence: intendedForFile, + }), + ) + } +}