Skip to content

Commit f27be5c

Browse files
committed
feat: Validate template and cohort directories
1 parent add1568 commit f27be5c

File tree

1 file changed

+23
-11
lines changed

1 file changed

+23
-11
lines changed

src/validators/filenameValidate.ts

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -281,17 +281,29 @@ async function invalidLocation(
281281
schema: GenericSchema,
282282
context: BIDSContext,
283283
) {
284-
const rule = schema[path]
285-
if (!('entities' in rule)) {
284+
if (context.directory) {
286285
return
287286
}
288-
const sub: string | undefined = context.entities.sub
289-
const ses: string | undefined = context.entities.ses
287+
if (!context.entities.tpl) {
288+
_validateLocation(context, 'sub', 'ses')
289+
}
290+
if (!context.entities.sub) {
291+
_validateLocation(context, 'tpl', 'cohort')
292+
}
293+
}
294+
295+
function _validateLocation(
296+
context: BIDSContext,
297+
topent: string,
298+
subent: string,
299+
) {
300+
const topval: string | undefined = context.entities[topent]
301+
const subval: string | undefined = context.entities[subent]
290302

291-
if (sub) {
292-
let pattern = `/sub-${sub}/`
293-
if (ses) {
294-
pattern += `ses-${ses}/`
303+
if (topval) {
304+
let pattern = `/${topent}-${topval}/`
305+
if (subval) {
306+
pattern += `${subent}-${subval}/`
295307
}
296308
if (!context.path.startsWith(pattern)) {
297309
context.dataset.issues.add({
@@ -302,18 +314,18 @@ async function invalidLocation(
302314
}
303315
}
304316

305-
if (!sub && context.path.match(/^\/sub-/)) {
317+
if (!topval && context.path.match(new RegExp(`^/${topent}-`))) {
306318
context.dataset.issues.add({
307319
code: 'INVALID_LOCATION',
308320
location: context.path,
309321
issueMessage: `Expected location: /${context.file.name}`,
310322
})
311323
}
312-
if (!ses && context.path.match(/\/ses-/)) {
324+
if (!subval && context.path.match(new RegExp(`/${subent}-`))) {
313325
context.dataset.issues.add({
314326
code: 'INVALID_LOCATION',
315327
location: context.path,
316-
issueMessage: `Unexpected session directory`,
328+
issueMessage: `Unexpected ${subent} directory`,
317329
})
318330
}
319331
}

0 commit comments

Comments
 (0)