Skip to content

Commit a9844e7

Browse files
authored
Merge pull request #634 from DaNish808/master
validate IntendedFor filetype
2 parents cbcb0d3 + 2c23a3d commit a9844e7

File tree

2 files changed

+60
-4
lines changed

2 files changed

+60
-4
lines changed

tests/nii.spec.js

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,44 @@ describe('NIFTI', function() {
125125
})
126126
})
127127

128+
it('should generate warning if files listed in IntendedFor of fieldmap json are not of type .nii or .nii.gz', function() {
129+
var file = {
130+
name: 'sub-09_ses-test_run-01_fieldmap.nii.gz',
131+
path:
132+
'/ds114/sub-09/ses-test/fmap/sub-09_ses-test_run-01_fieldmap.nii.gz',
133+
relativePath:
134+
'/sub-09/ses-test/fmap/sub-09_ses-test_run-01_fieldmap.nii.gz',
135+
}
136+
137+
var jsonContentsDict = {
138+
'/sub-09/ses-test/fmap/sub-09_ses-test_run-01_fieldmap.json': {
139+
TaskName: 'Mixed Event Related Probe',
140+
IntendedFor: [
141+
'func/sub-15_task-mixedeventrelatedprobe_run-05_bold.json',
142+
'func/sub-15_task-mixedeventrelatedprobe_run-02_bold.nii.gz',
143+
],
144+
},
145+
}
146+
var fileList = []
147+
fileList.push({
148+
name: 'sub-15_task-mixedeventrelatedprobe_run-01_bold.nii.gz',
149+
path: 'sub-15/func/sub-15_task-mixedeventrelatedprobe_run-01_bold.nii.gz',
150+
relativePath:
151+
'/func/sub-15_task-mixedeventrelatedprobe_run-01_bold.nii.gz',
152+
})
153+
validate.NIFTI(null, file, jsonContentsDict, {}, [], [], function(issues) {
154+
assert(
155+
issues.some(
156+
issue =>
157+
issue.reason ===
158+
'Invalid filetype: IntendedFor should point to the .nii[.gz] files.' &&
159+
issue.evidence ===
160+
'func/sub-15_task-mixedeventrelatedprobe_run-05_bold.json',
161+
),
162+
)
163+
})
164+
})
165+
128166
it('should generate warning if files listed in IntendedFor of fieldmap json do not exist', function() {
129167
var file = {
130168
name: 'sub-09_ses-test_run-01_fieldmap.nii.gz',
@@ -152,7 +190,7 @@ describe('NIFTI', function() {
152190
})
153191
validate.NIFTI(null, file, jsonContentsDict, {}, [], [], function(issues) {
154192
assert(
155-
(issues.length = 2 && issues[0].code == 17 && issues[1].code == 37),
193+
issues.length === 3 && issues[0].code == 17 && issues[1].code == 37,
156194
)
157195
})
158196
})

validators/nifti/nii.js

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,7 @@ module.exports = function NIFTI(
433433
for (let key = 0; key < intendedFor.length; key++) {
434434
const intendedForFile = intendedFor[key]
435435
checkIfIntendedExists(intendedForFile, fileList, issues, file)
436+
checkIfValidFiletype(intendedForFile, issues, file)
436437
}
437438
}
438439
}
@@ -485,9 +486,11 @@ function checkIfIntendedExists(intendedForFile, fileList, issues, file) {
485486
let onTheList = false
486487

487488
for (let key2 in fileList) {
488-
const filePath = fileList[key2].relativePath
489-
if (filePath === intendedForFileFull) {
490-
onTheList = true
489+
if (key2) {
490+
const filePath = fileList[key2].relativePath
491+
if (filePath === intendedForFileFull) {
492+
onTheList = true
493+
}
491494
}
492495
}
493496
if (!onTheList) {
@@ -509,3 +512,18 @@ function checkIfIntendedExists(intendedForFile, fileList, issues, file) {
509512
)
510513
}
511514
}
515+
516+
function checkIfValidFiletype(intendedForFile, issues, file) {
517+
const validFiletype = new RegExp('.nii(.gz)?$')
518+
const isValidFiletype = validFiletype.test(intendedForFile)
519+
if (!isValidFiletype) {
520+
issues.push(
521+
new Issue({
522+
file: file,
523+
code: 37,
524+
reason: `Invalid filetype: IntendedFor should point to the .nii[.gz] files.`,
525+
evidence: intendedForFile,
526+
}),
527+
)
528+
}
529+
}

0 commit comments

Comments
 (0)